目录
三层架构就是把整个软件系统分为三个层次
三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer)、业务逻辑层(Business Logic Layer)、数据访问层(Data access layer)。区分层次的目的即为了“高内聚低耦合”的思想。在软件体系架构设计中,分层式结构是最常见,也是最重要的一种结构。微软推荐的分层式结构一般分为三层,从下至上分别为:数据访问层(又称为持久层)、业务逻辑层(又或称为领域层)、表示层。
大型软件需要团队配合的时候问题就来了,由于每个程序员风格不一样,而开发软件大量的代码风格不统一就会造成后期调试和维护出现问题,然而软件分层后,每个层合理分工这样的问题便迎刃而解
上一层依赖于下一层,如果测试下一层没有问题,那么问题就只有可能发现在本层了,便于发现和改正BUG。体现了“高内聚,低耦合”的思想。比如楼房是分层的,我们要到哪一层楼非常方便,只需在电梯里按下那个楼层的层号即可。而三层架构就好比开发的软件“楼”,哪层出现Bug,哪层有问题,我们作为开发人员能够随时找到,并修正。 各个层次分工明确,将一个复杂问题简单拆分了。
?分层的根本在于代码的复用和劳动成本的减少。分层的最理想化的结果是实现层与层之间的互不依赖的内部实现,所谓的即插即用!
实现"高内聚、低耦合"。把问题划分开来各个解决,易于控制,易于延展,易于分配资源。
表现层可以说是距离用户最近的层,主要是用于接收用户输入的数据和显示处理后用户需要的数据。一般表现为界面,用户通过界面输入查询数据和得到需要的数据。
com.by.servlet: servlet包,接受请求控制跳转页面
业务逻辑层是处于表现层和数据访问层之间,主要是从数据库中得到数据然后对数据进行逻辑处理。
com.by.service:Service接口包
com.by.service.impl:Service接口实现类,处理业务
数据访问层是直接和数据库打交道的,对数据进行“增、删、改、查”等基本的操作。
com.by.dao:Dao接口包
com.by.dao.impl: Dao接口实现类,访问数据库
具体目录如图所示:
首先先建立User实体层:
package com.by.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
再设置连接数据库db.properties:
jdbc.driver=com.mysql.jdbc.Driver
#mysql8
#jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://127.0.0.1:3305/servlet?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases>
<!--<typeAlias type="com.by.pojo.User" alias="User"></typeAlias>-->
<!--批量给pojo定义别名,推荐使用小写-->
<package name="com.by.pojo"/>
</typeAliases>
<!--使用dev环境-->
<environments default="dev">
<environment id="dev">
<!--事务-->
<transactionManager type="JDBC"></transactionManager>
<!--
type="POOLED":连接池
-->
<dataSource type="POOLED">
<!--mysql8-->
<!--
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&serverTimezone=Asia/Shanghai"/>
-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载mapper映射文件-->
<mappers>
<!--直接引入映射文件-->
<!--<mapper resource="com/by/mapper/UserMapper.xml"></mapper>-->
<!--按mapper接口的名称引入映射文件,要求 mapper 接口名称和 mapper 映射文件名称相同-->
<!--<mapper class="com.by.mapper.UserMapper"></mapper>-->
<!--批量按mapper接口的名称引入映射文件-->
<package name="com.by.mapper"/>
</mappers>
</configuration>
设置公用代码MyServletContextListener:
package com.by.listener;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
//作用:监听servletContext对象的创建和销毁
public class MyServletContextListener implements ServletContextListener {
//servletContext对象创建后会调用此方法
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
//加载mybatis的运行环境
//加载mybatis-config.xml
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建sqlSession
SqlSession sqlSession = sessionFactory.openSession();
System.out.println("tomcat启动并加载mybatis环境:" + sqlSession);
//把sqlSession对象撞到servletContext中
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("sqlSession", sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
编写jsp代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>用户管理系统</h1>
<form action="login" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>人,总得有个活着的理由。</h1>
</body>
</html>
编写serviet的代码:
package com.by.servlet;
import com.by.pojo.User;
import com.by.service.UserService;
import com.by.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//表现层
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//根据input标签的name属性获取前端提交的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//调用service
UserService userService = new UserServiceImpl(this);
User user = userService.login(username, password);
if(user != null){
//如果登录成功,则跳转到select_list.jsp
req.getRequestDispatcher("select_list.jsp").forward(req, resp);
}else{
//如果登录失败,则跳转到login.jsp
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}
实现service下的接口和实现类:
UserService接口:
package com.by.service;
import com.by.pojo.User;
public interface UserService {
User login(String username, String password);
}
UserServicelmpl.java:
package com.by.service.impl;
import com.by.mapper.UserMapper;
import com.by.pojo.User;
import com.by.service.UserService;
import com.by.servlet.LoginServlet;
import org.apache.ibatis.session.SqlSession;
import javax.servlet.ServletContext;
//业务层
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public UserServiceImpl(LoginServlet loginServlet) {
System.out.println(loginServlet);
ServletContext servletContext = loginServlet.getServletContext();
SqlSession sqlSession = (SqlSession) servletContext.getAttribute("sqlSession");
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Override
public User login(String username, String password) {
//业务逻辑...
//调用dao层
return userMapper.login(username, password);
}
}
书写sql语句:
UserMapper:
public interface UserMapper {
User login(@Param("username") String username, @Param("password") String password);
}
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.by.mapper.UserMapper">
<select id="login" resultType="com.by.pojo.User">
SELECT * FROM user WHERE username=#{username} AND password=#{password}
</select>
</mapper>
wab.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置监听器-->
<listener>
<listener-class>com.by.listener.MyServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.by.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
?
数据库:?
?输入账号密码进行跳转:
?