5、SpringBoot+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

原文地址:https://www.cnblogs.com/holly8/p/10514968.html

时间: 2024-09-29 20:10:41

5、SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件的相关文章

SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了.全部自动实现. 话不多说,直接上代码: 第一步pom文件配置添加jar: <!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>

SpringBoot整合Pagehelper分页插件

在web开发中,数据的分页是必不可少的.Pagehelper分页插件很强大,虽说平时我们不需要用到它的很多功能,但是了解下还是有必要的. 官网:https://pagehelper.github.io/ 注:在 MyBatis下使用. 一.Pagehelper分页插件介绍 原文地址:https://www.cnblogs.com/myitnews/p/12349655.html

逆向工程文件example完美结合使用PageHelper分页插件及分页不成功原因

原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化mybatis单表操作. 在PageHelper3.几的版本的时候,使用它对逆向工程生成的查询方法进行分页时出现失效的情况,而PageHelper4开始,亲测能够与mybatis逆向工程生成的方法完美兼容,今天就和大家分享spring+springmvc+mybatis+PageHelper的项目遇

springboot集成pagehelper分页插件

之前写的项目都是在前端进行分页,最近涉及到后台分页查询,回看自己之前练习的项目里发现自己写了分页给忘了,作为初级程序员拿来记录一下 引入pagehelper的pom依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependen

小白的springboot之路(十五)、mybatis的PageHelper分页插件使用

0.前言 用mybatis,那么分页必不可少,基本都是用PageHelper这个分页插件,好用方便: 1.实现 1.1.添加依赖: <!-- 3.集成 mybatis pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version

PageHelper分页插件

在mybatis配置文件(SqlMapConfig.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"> <configurat

PageHelper分页插件及通用分页js

 分页概述 1.物理分页 物理分页依赖的是某一物理实体,这个物理实体就是数据库,比如MySQL数据库提供了limit关键字,程序员只需要编写带有limit关键字的SQL语句,数据库返回的就是分页结果.建议使用. 2.逻辑分页 逻辑分页依赖的是程序员编写的代码.数据库返回的不是分页结果,而是全部数据,然后再由程序员通过代码获取分页数据,常用的操作是一次性从数据库中查询出全部数据并存储到List集合中,因为List集合有序,再根据索引获取指定范围的数据. MyBatis 分页插件 - PageHel

项目中PageHelper分页插件的使用实例(SSM)

分页(英语:Paging),是一种操作系统里存储器管理的一种技术,可以使计算机的主存可以使用存储在辅助存储器中的数据.操作系统会将辅助存储器(通常是磁盘)中的数据分区成固定大小的区块,称为“页”(pages).当不需要时,将分页由主存(通常是内存)移到辅助存储器:当需要时,再将数据取回,加载主存中.相对于分段,分页允许存储器存储于不连续的区块以维持文件系统的整齐.[1]分页是磁盘和内存间传输数据块的最小单位. 分页/虚拟内存能有助“大大地”降低整体及额外非必要的 I/O 次数,提高系统整体运作性

pageHelper分页插件使用总结

1.添加jar包货添加pom文件 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.0.0</version> </dependency>2.添加插件配置在mybatis-config配置文件中添加插件<configuration> <!--