Maven(父子模块)+myBatis+分页(pagehelper)项目案例

第一步:搭建Maven项目

  首先是父模块:Demo-parent

  然后是子模块:Demo-entity,Demo-dao,Demo-service,Demo-web

  在Demo-web层添加Web目录。

第二步:父模块的pom.xml文件里,如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.nf</groupId>
 8     <artifactId>mvnBatis-parent</artifactId>
 9     <packaging>pom</packaging>
10     <version>1.0-SNAPSHOT</version>
11     <modules>
12         <module>mvnBatis-entity</module>
13         <module>mvnBatis-dao</module>
14         <module>mvnBatis-service</module>
15         <module>mvnBatis-web</module>
16     </modules>
17     
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
21         <java.version>1.8</java.version>
22         <maven.compiler.source>1.8</maven.compiler.source>
23         <maven.compiler.target>1.8</maven.compiler.target>
24 
25             <javax.servlet.version>3.1.0</javax.servlet.version>
26             <mysql.version>8.0.11</mysql.version>
27             <jstl.version>1.2</jstl.version>
28             <myBatis.version>3.4.6</myBatis.version>
29             <pagehelper.version>5.1.2</pagehelper.version>
30     </properties>
31 
32     <dependencyManagement>
33         <dependencies>
34             <dependency>
35                 <groupId>javax.servlet</groupId>
36                 <artifactId>javax.servlet-api</artifactId>
37                 <version>${javax.servlet.version}</version>
38             </dependency>
39 
40             <dependency>
41                 <groupId>mysql</groupId>
42                 <artifactId>mysql-connector-java</artifactId>
43                 <version>${mysql.version}</version>
44             </dependency>
45 
46             <dependency>
47                 <groupId>javax.servlet</groupId>
48                 <artifactId>jstl</artifactId>
49                 <version>${jstl.version}</version>
50             </dependency>
51 
52             <dependency>
53                 <groupId>org.mybatis</groupId>
54                 <artifactId>mybatis</artifactId>
55                 <version>${myBatis.version}</version>
56             </dependency>
57 
58             <dependency>
59                 <groupId>com.github.pagehelper</groupId>
60                 <artifactId>pagehelper</artifactId>
61                 <version>${pagehelper.version}</version>
62             </dependency>
63 
64 
65         </dependencies>
66     </dependencyManagement>
67 
68     <build>
69         <plugins>
70             <plugin>
71                 <artifactId>maven-war-plugin</artifactId>
72                 <version>3.2.0</version>
73                 <configuration>
74                     <warSourceDirectory>web</warSourceDirectory>
75                 </configuration>
76             </plugin>
77         </plugins>
78     </build>
79 
80 </project>
View parent-pom

第三步:

3.1  写实体层代码

 1 package entity;
 2 
 3 import java.math.BigDecimal;
 4 
 5 public class UserInfo {
 6     private Integer id;
 7     private String username;
 8     private Integer gender;
 9     private BigDecimal salary;
10     private Integer did;
11 
12     @Override
13     public String toString() {
14         return "UserInfo{" +
15                 "id=" + id +
16                 ", username='" + username + '\'' +
17                 ", gender=" + gender +
18                 ", salary=" + salary +
19                 ", did=" + did +
20                 '}';
21     }
22 
23     public Integer getId() {
24         return id;
25     }
26 
27     public void setId(Integer id) {
28         this.id = id;
29     }
30 
31     public String getUsername() {
32         return username;
33     }
34 
35     public void setUsername(String username) {
36         this.username = username;
37     }
38 
39     public Integer getGender() {
40         return gender;
41     }
42 
43     public void setGender(Integer gender) {
44         this.gender = gender;
45     }
46 
47     public BigDecimal getSalary() {
48         return salary;
49     }
50 
51     public void setSalary(BigDecimal salary) {
52         this.salary = salary;
53     }
54 
55     public Integer getDid() {
56         return did;
57     }
58 
59     public void setDid(Integer did) {
60         this.did = did;
61     }
62 }
View Code

3.2  dao层代码

  3.2.1  dao包:接口UserInfoDao  和  实现类UserInfoDaoImpl

 1 package dao;
 2 
 3 import entity.UserInfo;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 public interface UserInfoDao {
10     List<UserInfo> getAll(Map<String,Object> map, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize);
11 }
View UserInfoDao
 1 package dao;
 2 
 3 import entity.UserInfo;
 4 import org.apache.ibatis.session.SqlSession;
 5 import util.SqlSessionFactoryUtil;
 6 
 7 import java.util.List;
 8 import java.util.Map;
 9 
10 public class UserInfoDaoImpl implements UserInfoDao {
11     @Override
12     public List<UserInfo> getAll(Map<String, Object> map, int pageNum, int pageSize) {
13         try(SqlSession sqlSession= SqlSessionFactoryUtil.sqlSession(true)){
14             UserInfoDao userInfoDao=sqlSession.getMapper(UserInfoDao.class);
15             List<UserInfo> userInfos=userInfoDao.getAll(map,pageNum,pageSize);
16             return userInfos;
17         }
18     }
19 }
View UserInfoDaoImpl

  注意:接口里的@ Param是必须的,原因:java代码在idea里编译时,参数名会消失,@Param可解决此问题。参数在sql语句会用,在后面的java代码也是会用到的。

  3.2.2  util包:SqlSessionFactoryUtil辅助类

 1 package util;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 
11 public class SqlSessionFactoryUtil {
12     private static final String RESOURCE="config.xml";
13     private static final SqlSessionFactory FACTORY;
14 
15     static {
16         InputStream inputStream=null;
17         try {
18             inputStream= Resources.getResourceAsStream(RESOURCE);
19         } catch (IOException e) {
20             e.printStackTrace();
21         }
22         FACTORY=new SqlSessionFactoryBuilder().build(inputStream);
23     }
24 
25     public static SqlSessionFactory sqlSessionFactory(){
26         return FACTORY;
27     }
28 
29     public static SqlSession sqlSession(boolean autocommit){
30         return FACTORY.openSession(autocommit);
31     }
32 }
View SqlSessionFactoryUtil

  3.2.3  资源文件resource :config.xml 和 mapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <plugins>
 7         <plugin interceptor="com.github.pagehelper.PageInterceptor">
 8             <property name="helperDialect" value="mysql"/>
 9             <property name="supportMethodsArguments" value="true"/>
10         </plugin>
11     </plugins>
12     <environments default="def">
13         <environment id="def">
14             <transactionManager type="JDBC"></transactionManager>
15             <dataSource type="POOLED">
16                 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
17                 <property name="url" value="jdbc:mysql://localhost:3306/nf?serverTimezone=Asia/Shanghai&amp;useSSL=false"/>
18                 <property name="username" value="root"/>
19                 <property name="password" value="123456"/>
20             </dataSource>
21         </environment>
22     </environments>
23     <mappers>
24         <mapper resource="mapper.xml"/>
25     </mappers>
26 </configuration>
View config
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="dao.UserInfoDao">
6     <select id="getAll" resultType="entity.UserInfo">
7         select * from userinfo
8     </select>
9 </mapper>
View mapper

3.3  pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>Demo-parent</artifactId>
 7         <groupId>com.nf</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>Demo-dao</artifactId>
13 
14     <dependencies>
15         <dependency>
16             <groupId>com.nf</groupId>
17             <artifactId>Demo-entity</artifactId>
18             <version>1.0-SNAPSHOT</version>
19         </dependency>
20 
21         <dependency>
22             <groupId>mysql</groupId>
23             <artifactId>mysql-connector-java</artifactId>
24         </dependency>
25 
26         <dependency>
27             <groupId>org.mybatis</groupId>
28             <artifactId>mybatis</artifactId>
29         </dependency>
30     </dependencies>
31 
32 
33 </project>
View dao-pom

第四步:

写service层代码

4.1  先写接口然后写实现类 UserInfoService   和   UserInfoServiceImpl

1 package service;
2 
3 import entity.UserInfo;
4 import java.util.List;
5 import java.util.Map;
6 
7 public interface UserInfoService {
8     List<UserInfo> getAll(Map<String,Object> map,int pageNum,int pageSize);
9 }
View UserInfoService
 1 package service;
 2 
 3 import dao.UserInfoDao;
 4 import dao.UserInfoDaoImpl;
 5 import entity.UserInfo;
 6 
 7 import java.util.List;
 8 import java.util.Map;
 9 
10 public class UserInfoServiceImpl implements UserInfoService {
11     UserInfoDao uid=new UserInfoDaoImpl();
12     @Override
13     public List<UserInfo> getAll(Map<String, Object> map, int pageNum, int pageSize) {
14         return uid.getAll(map,pageNum,pageSize);
15     }
16 }
View UserInfoServiceImpl

4.2  service里的pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>Demo-parent</artifactId>
 7         <groupId>com.nf</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>Demo-service</artifactId>
13 
14     <dependencies>
15         <dependency>
16             <groupId>com.nf</groupId>
17             <artifactId>Demo-dao</artifactId>
18             <version>1.0-SNAPSHOT</version>
19         </dependency>
20     </dependencies>
21 
22 
23 </project>
View service-pom

第五步:

5.1  写servlet控制类:servlet_getAll

 1 package action;
 2 
 3 import com.github.pagehelper.Page;
 4 import com.github.pagehelper.PageInfo;
 5 import entity.UserInfo;
 6 import service.UserInfoService;
 7 import service.UserInfoServiceImpl;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import java.io.IOException;
15 
16 @WebServlet("/getAll")
17 public class Servlet_getAll extends HttpServlet {
18     @Override
19     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
20         UserInfoService uis=new UserInfoServiceImpl();
21         int pageNum=getPageNum(req);
22 
23         Page<UserInfo> userInfoPage= (Page<UserInfo>) uis.getAll(null,pageNum,4);
24         PageInfo<UserInfo> userInfoPageInfo=new PageInfo<>(userInfoPage,7);
25 
26         req.setAttribute("userinfos",userInfoPageInfo);
27         req.getRequestDispatcher("show.jsp").forward(req,resp);
28     }
29 
30     public static int getPageNum(HttpServletRequest req){
31         int pageNum=1;
32         try{
33             pageNum=Integer.valueOf(req.getParameter("page"));
34         }catch (Exception e){
35             pageNum=1;
36         }
37         return pageNum;
38     }
39 }
View servlet_getAll

5.2  添加web构面

快捷键 Ctrl+Shift+Alt+S 进入项目配置页面,为Demo-web层添加web构面

5.3  在里面添加show.jsp页面

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2018/9/5 0005
 5   Time: 23:08
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
10 <html>
11 <head>
12     <title>Title</title>
13 </head>
14 <body>
15 <table border="1" align="center" width="80%">
16     <tr>
17         <th>编号</th>
18         <th>姓名</th>
19         <th>性别</th>
20         <th>工资</th>
21         <th>部门</th>
22     </tr>
23 <c:forEach items="${userinfos.list}" var="u">
24     <tr>
25         <td>${u.id}</td>
26         <td>${u.username}</td>
27         <td>${u.gender}</td>
28         <td>${u.salary}</td>
29         <td>${u.did}</td>
30     </tr>
31 </c:forEach>
32 </table>
33 <p align="center">
34     <a href="getAll?page=1">首页</a>
35     <a href="getAll?page=${userinfos.prePage}">上一页</a>
36     <c:forEach items="${userinfos.navigatepageNums}" var="pageNum">
37         <a href="getAll?page=${pageNum}">${pageNum}</a>
38     </c:forEach>
39     <a href="getAll?page=${userinfos.nextPage}">下一页</a>
40     <a href="getAll?page=${userinfos.pages}">尾页</a>
41 </p>
42 </body>
43 </html>
View show-jsp

5.4  web层的pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>Demo-parent</artifactId>
 7         <groupId>com.nf</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>Demo-web</artifactId>
13     <packaging>war</packaging>
14 
15     <dependencies>
16         <dependency>
17             <groupId>com.nf</groupId>
18             <artifactId>Demo-service</artifactId>
19             <version>1.0-SNAPSHOT</version>
20         </dependency>
21         <dependency>
22             <groupId>javax.servlet</groupId>
23             <artifactId>jstl</artifactId>
24         </dependency>
25         <dependency>
26             <groupId>com.github.pagehelper</groupId>
27             <artifactId>pagehelper</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>javax.servlet</groupId>
31             <artifactId>javax.servlet-api</artifactId>
32         </dependency>
33     </dependencies>
34 
35 
36 </project>
View web-pom

第六步:

maven项目执行clean 和 package 生命周期,在父模块打包形成war包,

然后编辑运行配置即可。

 

 

 

 

 

  

 

posted @ 2018-09-06 10:05  小汐儿  阅读(960)  评论(0编辑  收藏  举报