5SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件

Posted holly8

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件相关的知识,希望对你有一定的参考价值。

1、为了我们平时方便开发,我们可以在同一个idea窗口创建多个项目模块,创建方式如下

技术图片

技术图片

技术图片

技术图片

技术图片

技术图片

 

2、项目中pom.xm文件的内容如下

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.3.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>cn.kgc</groupId>
12     <artifactId>springbootmybatis</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>springbootmybatis</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-jdbc</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-thymeleaf</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-web</artifactId>
33         </dependency>
34         <dependency>
35             <groupId>org.mybatis.spring.boot</groupId>
36             <artifactId>mybatis-spring-boot-starter</artifactId>
37             <version>2.0.0</version>
38         </dependency>
39         <!--一定注意版本号,此处需要修改,把默认score的删掉哦-->
40         <dependency>
41             <groupId>mysql</groupId>
42             <artifactId>mysql-connector-java</artifactId>
43             <version>5.1.38</version>
44         </dependency>
45 
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-test</artifactId>
49             <scope>test</scope>
50         </dependency>
51        <!--spring boot 分页查询,需要手动配置-->
52         <dependency>
53             <groupId>com.github.pagehelper</groupId>
54             <artifactId>pagehelper-spring-boot-starter</artifactId>
55             <version>1.2.3</version>
56         </dependency>
57 
58     </dependencies>
59 
60     <build>
61         <plugins>
62             <plugin>
63                 <groupId>org.springframework.boot</groupId>
64                 <artifactId>spring-boot-maven-plugin</artifactId>
65             </plugin>
66         </plugins>
67     </build>
68 
69 </project>
pom.xml

 

3、项目结构如下

技术图片

 

4、在pojo包下创建Classes.java

技术图片
 1 package cn.kgc.pojo;
 2 import java.io.Serializable;
 3 /**
 4  * @author:南京课工场大数据学院 Holly老师
 5  * QQ/微信:864918306
 6  * Created by Holly on 2019/4/2.
 7  */
 8 public class Classes implements Serializable{
 9     private Integer cid;
10     private String cname;
11 
12     public Classes() {
13     }
14 
15     public Classes(Integer cid, String cname) {
16         this.cid = cid;
17         this.cname = cname;
18     }
19 
20     public Integer getCid() {
21         return cid;
22     }
23 
24     public void setCid(Integer cid) {
25         this.cid = cid;
26     }
27 
28     public String getCname() {
29         return cname;
30     }
31 
32     public void setCname(String cname) {
33         this.cname = cname;
34     }
35 
36     @Override
37     public String toString() {
38         return "Classes{" +
39                 "cid=" + cid +
40                 ", cname=‘" + cname + ‘\\‘‘ +
41                 ‘}‘;
42     }
43 }
Classes.java

 

5、在pojo包下创建Student.java

技术图片
 1 package cn.kgc.pojo;
 2 import java.io.Serializable;
 3 /**
 4  * @author:南京课工场大数据学院 Holly老师
 5  * QQ/微信:864918306
 6  * Created by Holly on 2019/4/2.
 7  */
 8 public class Student implements Serializable{
 9     private Integer sid;
10     private String sname;
11     private String password;
12     private String subject;
13     private Double result;
14     private Classes classes;
15 
16     public Student() {
17     }
18 
19     public Student(Classes classes) {
20         this.classes = classes;
21     }
22 
23     public Student(Integer sid, String sname, String password, String subject, Double result, Classes classes) {
24         this.sid = sid;
25         this.sname = sname;
26         this.password = password;
27         this.subject = subject;
28         this.result = result;
29         this.classes = classes;
30     }
31 
32     public Integer getSid() {
33         return sid;
34     }
35 
36     public void setSid(Integer sid) {
37         this.sid = sid;
38     }
39 
40     public String getSname() {
41         return sname;
42     }
43 
44     public void setSname(String sname) {
45         this.sname = sname;
46     }
47 
48     public String getPassword() {
49         return password;
50     }
51 
52     public void setPassword(String password) {
53         this.password = password;
54     }
55 
56     public String getSubject() {
57         return subject;
58     }
59 
60     public void setSubject(String subject) {
61         this.subject = subject;
62     }
63 
64     public Double getResult() {
65         return result;
66     }
67 
68     public void setResult(Double result) {
69         this.result = result;
70     }
71 
72     public Classes getClasses() {
73         return classes;
74     }
75 
76     public void setClasses(Classes classes) {
77         this.classes = classes;
78     }
79 
80     @Override
81     public String toString() {
82         return "Student{" +
83                 "sid=" + sid +
84                 ", sname=‘" + sname + ‘\\‘‘ +
85                 ", password=‘" + password + ‘\\‘‘ +
86                 ", subject=‘" + subject + ‘\\‘‘ +
87                 ", result=" + result +
88                 ", classes=" + classes +
89                 ‘}‘;
90     }
91 }
Student.java

 

6、在mapper包下创建ClassesMapper.java

 

技术图片
 1 package cn.kgc.mapper;
 2 
 3 import cn.kgc.pojo.Classes;
 4 
 5 import java.util.List;
 6 /**
 7  * @author:南京课工场大数据学院 Holly老师
 8  * QQ/微信:864918306
 9  * Created by Holly on 2019/4/2.
10  */
11 public interface ClassesMapper {
12     List<Classes> selectClasses();
13 }
ClassesMapper.java

 

 

7、在mapper包下创建StudentMapper.java 

技术图片
 1 package cn.kgc.mapper;
 2 
 3 import cn.kgc.pojo.Student;
 4 
 5 import java.util.List;
 6 /**
 7  * @author:南京课工场大数据学院 Holly老师
 8  * QQ/微信:864918306
 9  * Created by Holly on 2019/4/2.
10  */
11 public interface StudentMapper {
12     List<Student> selectStudent(Student student);
13 }
StudentMapper.java

 

8、在resources文件夹下的mapper文件夹下创建ClassesMapper.xml

技术图片
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="cn.kgc.mapper.ClassesMapper">
6     <select id="selectClasses" resultType="Classes">
7         select * from classes
8     </select>
9 </mapper>
ClassesMapper.xml

 

9、在resources文件夹下的mapper文件夹下创建StudentMapper.xml

技术图片
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="cn.kgc.mapper.StudentMapper">
 6     <select id="selectStudent" resultMap="studentMap">
 7         select s.*,c.cname from student s,classes c where s.cid=c.cid
 8         <if test="classes!=null and classes.cid!=-1">
 9              and c.cid=#{classes.cid}
10         </if>
11     </select>
12     <resultMap id="studentMap" type="Student">
13         <id property="sid" column="SID"/>
14         <result property="sname" column="SNAME"/>
15         <result property="password" column="PASSWORD"/>
16         <result property="subject" column="SUBJECT"/>
17         <result property="result" column="RESULT"/>
18         <!--
19         <association property="classes" javaType="Classes">
20             <result property="cid" column="CID"/>
21             <result property="cname" column="CNAME"/>
22         </association>
23         -->
24         <association property="classes" javaType="Classes" resultMap="classesMap"/>
25     </resultMap>
26     <resultMap id="classesMap" type="Classes">
27         <id property="cid" column="CID"/>
28         <result property="cname" column="CNAME"/>
29     </resultMap>
30 
31 </mapper>
StudentMapper.xml

 

10、在java文件夹下的cn.kgc.service路径下创建ClassesService.java文件

技术图片
 1 package cn.kgc.service;
 2 
 3 import cn.kgc.pojo.Classes;
 4 import org.springframework.stereotype.Service;
 5 import org.springframework.transaction.annotation.Transactional;
 6 
 7 import java.util.List;
 8 /**
 9  * @author:南京课工场大数据学院 Holly老师
10  * QQ/微信:864918306
11  * Created by Holly on 2019/4/2.
12  */
13 public interface ClassesService {
14     List<Classes> showOption();
15 }
ClassesService.java

 

11、在java文件夹下的cn.kgc.service路径下创建ClassesServiceImpl.java文件

技术图片
 1 package cn.kgc.service;
 2 
 3 import cn.kgc.mapper.ClassesMapper;
 4 import cn.kgc.pojo.Classes;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 import org.springframework.transaction.annotation.Transactional;
 8 
 9 import java.util.List;
10 /**
11  * @author:南京课工场大数据学院 Holly老师
12  * QQ/微信:864918306
13  * Created by Holly on 2019/4/2.
14  */
15 @Service
16 @Transactional
17 public class ClassesServiceImpl implements ClassesService{
18     @Autowired
19     private ClassesMapper classesMapper;
20     @Override
21     public List<Classes> showOption() {
22         return classesMapper.selectClasses();
23     }
24 }
ClassesServiceImpl.java

 

12、在java文件夹下的cn.kgc.service路径下创建StudentService.java文件

技术图片
 1 package cn.kgc.service;
 2 import cn.kgc.pojo.Student;
 3 import com.github.pagehelper.PageInfo;
 4 import java.util.List;
 5 /**
 6  * @author:南京课工场大数据学院 Holly老师
 7  * QQ/微信:864918306
 8  * Created by Holly on 2019/4/2.
 9  */
10 public interface StudentService {
11     List<Student> showData(Student student);
12     //插件做分页数据查询(查询所有和条件查询)
13     PageInfo<Student> showPageData(Integer pageno,Integer pagesize,Student student);
14 }
StudentService.java

 

13、在java文件夹下的cn.kgc.service路径下创建StudentServiceImpl.java文件

技术图片
 1 package cn.kgc.service;
 2 
 3 import cn.kgc.mapper.StudentMapper;
 4 import cn.kgc.pojo.Student;
 5 import com.github.pagehelper.Page;
 6 import com.github.pagehelper.PageHelper;
 7 import com.github.pagehelper.PageInfo;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Service;
10 import org.springframework.transaction.annotation.Transactional;
11 
12 import java.util.List;
13 /**
14  * @author:南京课工场大数据学院 Holly老师
15  * QQ/微信:864918306
16  * Created by Holly on 2019/4/2.
17  */
18 @Service
19 @Transactional
20 public class StudentServiceImpl implements StudentService{
21     @Autowired
22     private StudentMapper studentMapper;
23     @Override
24     public List<Student> showData(Student student) {
25         return studentMapper.selectStudent(student);
26     }
27 
28     //分页查询
29     @Override
30     public PageInfo<Student> showPageData(Integer pageno, Integer pagesize, Student student) {
31         //创建分页工具类
32         Page pageutil= PageHelper.startPage(pageno,pagesize);
33         //将要进行分页的数据先做查询所以
34         studentMapper.selectStudent(student);
35         return new PageInfo<Student>(pageutil);
36     }
37 }
StudentServiceImpl.java

 

14.在java文件夹下的cn.kgc.controller路径下创建CenterController.java文件

技术图片
 1 package cn.kgc.controller;
 2 import cn.kgc.pojo.Classes;
 3 import cn.kgc.pojo.Student;
 4 import cn.kgc.service.ClassesService;
 5 import cn.kgc.service.StudentService;
 6 import com.github.pagehelper.PageInfo;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import java.util.List;
12 /**
13  * @author:南京课工场大数据学院 Holly老师
14  * QQ/微信:864918306
15  * Created by Holly on 2019/4/2.
16  */
17 @RestController
18 public class CenterController{
19     @Autowired
20     private ClassesService classesService;
21 
22     @Autowired
23     private StudentService studentService;
24 
25     @RequestMapping("/option.do")
26     public List<Classes> getOptionData(){
27          return classesService.showOption();
28     }
29 
30     @RequestMapping("/getData.do")
31     public List<Student> getStudentData(Integer cid){
32 
33         return studentService.showData(new Student(new Classes(cid,null)));
34     }
35    //插件分页查询
36     @RequestMapping("/getPageData.do")
37     public PageInfo<Student> findPageData(Integer cid,Integer pageno){
38         Integer pagesize=3;
39         Student student=new Student(new Classes(cid,null));
40         return studentService.showPageData(pageno,pagesize,student);
41     }
42 }
CenterController.java

 

15.编辑java文件夹下的cn.kgc路径下的SpringbootmybatisApplication.java文件

技术图片
 1 package cn.kgc;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 /**
 7  * @author:南京课工场大数据学院 Holly老师
 8  * QQ/微信:864918306
 9  * Created by Holly on 2019/4/2.
10  */
11 @SpringBootApplication
12 @MapperScan("cn.kgc.mapper")
13 public class SpringbootmybatisApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(SpringbootmybatisApplication.class, args);
17     }
18 
19 }
SpringbootmybatisApplication.java

 

16.编辑resources文件夹下的application.properties文件

技术图片
#端口号
server.port=9090

#数据源的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/kgc
spring.datasource.username=root
spring.datasource.password=ok

#给某个包下的类的全路径生成别名
mybatis.type-aliases-package=cn.kgc.pojo

#注册映射xml文件
mybatis.mapper-locations=classpath:mapper/*.xml
application.properties

 

17.在resources文件夹下的static文件夹下创建index.html文件

技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8    <center>
 9        <h1>学员管理系统</h1>
10        <form id="claConditon">
11            <p>
12                班级:
13                <select name="cid">
14                    <option value="-1">--请选择--</option>
15                </select>
16                <input type="button" value="提交"/>
17            </p>
18        </form>
19        <table>
20            <tr>
21                <td>学号</td>
22                <td>班级</td>
23                <td>姓名</td>
24                <td>科目</td>
25                <td>成绩</td>
26            </tr>
27        </table>
28        <p id="pageroot"></p>
29    </center>
30    <script type="text/javascript" src="js/jquery-3.1.1.js"></script>
31    <script type="text/javascript" src="js/index.js"></script>
32 </body>
33 </html>
index.html

 

18.在resources文件夹下的static文件夹下创建js文件夹,并在该文件夹下添加jquery-3.1.1.js文件

19.在resources文件夹下的static文件夹下创建js文件夹,并在该文件夹下创建index.js文件

技术图片
  1 $(function () {
  2     //下拉菜单
  3     getOptionData();
  4     //初始化数据
  5     // initData();
  6     //分页初始化数据
  7      initPageData(1);
  8 
  9     //条件查询
 10     $("[type=button]").click(function () {
 11         initPageData(1);
 12     });
 13 });
 14 
 15 //分页初始化数据
 16 function initPageData(pageno){
 17     var cid=$("[name=cid]").val();
 18     $.ajax({
 19         url:"getPageData.do",
 20         type:"post",
 21         dataType:"json",
 22         data:{"cid":cid,"pageno":pageno},
 23         async:true,
 24         success:function (obj) {
 25             console.log(obj);
 26             $("tr:not(tr:first)").remove();
 27             var str="";
 28             $.each(obj.list,function (i) {
 29                 str+="<tr>";
 30                 str+="<td>"+obj.list[i].sid+"</td>";
 31                 str+="<td>"+obj.list[i].classes.cname+"</td>";
 32                 str+="<td>"+obj.list[i].sname+"</td>";
 33                 str+="<td>"+obj.list[i].subject+"</td>";
 34                 str+="<td>"+obj.list[i].result+"</td>";
 35                 str+="</tr>";
 36             });
 37             $("table").append(str);
 38 
 39             $("#pageroot").html(" ");
 40             var pagestr="第"+obj.pageNum+"/"+obj.pages+"页&nbsp;";
 41             if(obj.pageNum>1){
 42                 pagestr+="<a href=‘javascrpt:void(0);‘ onclick=‘initPageData("+obj.firstPage+")‘>首页</a>&nbsp;";
 43                 pagestr+="<a href=‘javascrpt:void(0);‘ onclick=‘initPageData("+obj.prePage+")‘>上一页</a>&nbsp;";
 44             }
 45             if(obj.pageNum<obj.pages){
 46                 pagestr+="<a href=‘javascrpt:void(0);‘ onclick=‘initPageData("+obj.nextPage+")‘>下一页</a>&nbsp;";
 47                 pagestr+="<a href=‘javascrpt:void(0);‘ onclick=‘initPageData("+obj.lastPage+")‘>末页</a>&nbsp;";
 48             }
 49 
 50             pagestr+="共"+obj.total+"条&nbsp;";
 51             $("#pageroot").append(pagestr);
 52         },
 53         error:function () {
 54             alert("initPageData error");
 55         }
 56     });
 57 }
 58 
 59 
 60 //初始化数据
 61 function initData(){
 62     var cid=$("[name=cid]").val();
 63     $.ajax({
 64         url:"getData.do",
 65         type:"post",
 66         dataType:"json",
 67         data:{"cid":cid},
 68         async:true,
 69         success:function (obj) {
 70             console.log(obj);
 71             $("tr:not(tr:first)").remove();
 72             var str="";
 73             $.each(obj,function (i) {
 74                  str+="<tr>";
 75                  str+="<td>"+obj[i].sid+"</td>";
 76                  str+="<td>"+obj[i].classes.cname+"</td>";
 77                  str+="<td>"+obj[i].sname+"</td>";
 78                  str+="<td>"+obj[i].subject+"</td>";
 79                  str+="<td>"+obj[i].result+"</td>";
 80                  str+="</tr>";
 81             });
 82             $("table").append(str);
 83         },
 84         error:function () {
 85             alert("initData error");
 86         }
 87     });
 88 }
 89 //下拉菜单
 90 function getOptionData() {
 91   $.ajax({
 92       url:"option.do",
 93       type:"post",
 94       dataType:"json",
 95       data:{},
 96       async:true,
 97       success:function (obj) {
 98           var str="";
 99           $.each(obj,function (i) {
100              str+="<option value=‘"+obj[i].cid+"‘>"+obj[i].cname+"</option>";
101           });
102          $("select").append(str);
103       },
104       error:function () {
105           alert("getOptionData error");
106       }
107   });
108 }
index.js

 

20.运行SpringbootmybatisApplication文件,

技术图片

21.浏览器运行

技术图片

技术图片

 

技术图片

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

特此声明:请大家尊重知识版权,该帖本人Holly原创,大家搞技术都不容易,分享为了互相学习和交流,转载请注明出处!谢谢!

大家也可以关注我的微信公众号:跟Holly老师学java

QQ/微信:964918306

 

 

以上是关于5SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件的主要内容,如果未能解决你的问题,请参考以下文章

java springmvc mybaits maven后台框架源码

java springmvc mybaits maven shiro mysql

mybaits的 maven pom.xml文件中配置阿里源

Mybaits 快速入门代码实例(maven代码版)

java springmvc mybaits maven shiro mysql整合

mybaits maven shiro mysql 后台框架源码bootstrap