Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)

内容:

Spring Boot2 + Mybatis 整合

Mybatis Generator自动生成代码

Mybatis PageHelper分页插件

创建maven项目

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第1张图片

修改pom.xml 注意springboot、druid、pageHelper的版本号


   
   
   
   
  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. <groupId>com.sid groupId>
  6. <artifactId>springboot artifactId>
  7. <version>1.0-SNAPSHOT version>
  8. <packaging>jar packaging>
  9. <parent>
  10. <groupId>org.springframework.boot groupId>
  11. <artifactId>spring-boot-starter-parent artifactId>
  12. <version>2.0.5.RELEASE version>
  13. <relativePath/>
  14. parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  18. <java.version>1.8 java.version>
  19. properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot groupId>
  23. <artifactId>spring-boot-starter-web artifactId>
  24. dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot groupId>
  27. <artifactId>spring-boot-starter-test artifactId>
  28. <scope>test scope>
  29. dependency>
  30. <dependency>
  31. <groupId>org.apache.commons groupId>
  32. <artifactId>commons-lang3 artifactId>
  33. <version>3.4 version>
  34. dependency>
  35. <dependency>
  36. <groupId>org.mybatis.spring.boot groupId>
  37. <artifactId>mybatis-spring-boot-starter artifactId>
  38. <version>1.3.2 version>
  39. dependency>
  40. <dependency>
  41. <groupId>mysql groupId>
  42. <artifactId>mysql-connector-java artifactId>
  43. <version>5.1.38 version>
  44. dependency>
  45. <dependency>
  46. <groupId>com.alibaba groupId>
  47. <artifactId>druid-spring-boot-starter artifactId>
  48. <version>1.1.9 version>
  49. dependency>
  50. <dependency>
  51. <groupId>com.github.pagehelper groupId>
  52. <artifactId>pagehelper-spring-boot-starter artifactId>
  53. <version>1.2.5 version>
  54. dependency>
  55. dependencies>
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot groupId>
  60. <artifactId>spring-boot-maven-plugin artifactId>
  61. plugin>
  62. <plugin>
  63. <groupId>org.mybatis.generator groupId>
  64. <artifactId>mybatis-generator-maven-plugin artifactId>
  65. <version>1.3.2 version>
  66. <configuration>
  67. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml configurationFile>
  68. <overwrite>true overwrite>
  69. <verbose>true verbose>
  70. configuration>
  71. plugin>
  72. plugins>
  73. build>
  74. project>

创建启动类App.java


   
   
   
   
  1. package com.sid;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan( "com.sid.mapper") //将项目中对应的mapper类的路径加进来就可以了
  7. public class App {
  8. public static void main(String[] args) {
  9. SpringApplication.run(App.class, args);
  10. }
  11. }

创建application.yml


   
   
   
   
  1. server:
  2. port: 8088
  3. servlet:
  4. context-path: /sid
  5. spring:
  6. mvc:
  7. view:
  8. prefix: /
  9. suffix: .html
  10. datasource:
  11. url: jdbc:mysql://localhost:3306/sid
  12. username: root
  13. password: root
  14. # 使用druid数据源
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. driver-class-name: com.mysql.jdbc.Driver
  17. ## 该配置节点为独立的节点,不是在在spring的节点下
  18. mybatis:
  19. mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
  20. type-aliases-package: com.sid.model # 注意:对应实体类的路径
  21. configuration:
  22. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台打印sql
  23. #pagehelper分页插件
  24. pagehelper:
  25. helperDialect: mysql
  26. reasonable: true
  27. supportMethodsArguments: true
  28. params: count=countSql

在resources/generator下创建文件generatorConfig.xml


   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?>
  2. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  4. <generatorConfiguration>
  5. <classPathEntry location="F:\MySQL\Connector.J 5.1\mysql-connector-java-5.1.38-bin.jar"/>
  6. <context id="DB2Tables" targetRuntime="MyBatis3">
  7. <commentGenerator>
  8. <property name="suppressDate" value="true"/>
  9. <property name="suppressAllComments" value="true"/>
  10. commentGenerator>
  11. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sid" userId="root" password="root">
  12. jdbcConnection>
  13. <javaTypeResolver>
  14. <property name="forceBigDecimals" value="false"/>
  15. javaTypeResolver>
  16. <javaModelGenerator targetPackage="com.sid.model" targetProject="src/main/java">
  17. <property name="enableSubPackages" value="true"/>
  18. <property name="trimStrings" value="true"/>
  19. javaModelGenerator>
  20. <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
  21. <property name="enableSubPackages" value="true"/>
  22. sqlMapGenerator>
  23. <javaClientGenerator type="XMLMAPPER" targetPackage="com.sid.mapper" targetProject="src/main/java">
  24. <property name="enableSubPackages" value="true"/>
  25. javaClientGenerator>
  26. <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> table>
  27. context>
  28. generatorConfiguration>

项目目录

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第2张图片

 

数据库中的表


   
   
   
   
  1. CREATE TABLE `user` (
  2. `id` bigint( 32) NOT NULL COMMENT '用户ID',
  3. `name` varchar( 30) NOT NULL COMMENT '用户名',
  4. `password` varchar( 30) NOT NULL COMMENT '密码',
  5. `mobile_phone` varchar( 20) NOT NULL COMMENT '手机号',
  6. PRIMARY KEY ( `id`)
  7. ) ENGINE= InnoDB DEFAULT CHARSET=utf8;

 

mybatis自动生成代码的插件使用方式

run

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第3张图片

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第4张图片

然会就会自动生成UserMapper.xml、 User.java 、UserMapper.java

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第5张图片

 

UserController.java


   
   
   
   
  1. package com.sid.controller;
  2. import com.github.pagehelper.PageInfo;
  3. import com.sid.model.User;
  4. import com.sid.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. @Controller
  10. @RequestMapping(value = "/user")
  11. public class UserController {
  12. @Autowired
  13. private UserService userService;
  14. @ResponseBody
  15. @RequestMapping(value = "/add")
  16. public int addUser(User user){
  17. return userService.addUser(user);
  18. }
  19. @ResponseBody
  20. @RequestMapping(value = "/delete")
  21. public int deleteUser(String id){
  22. return userService.deleteUser(id);
  23. }
  24. @ResponseBody
  25. @RequestMapping(value = "/update")
  26. public int updateUser(User user){
  27. return userService.updateUser(user);
  28. }
  29. @ResponseBody
  30. @RequestMapping(value = "/selectAll")
  31. public PageInfo selectAll(int pageNum, int pageSize){
  32. return userService.selectAll( pageNum, pageSize);
  33. }
  34. }

 

UserService.java


   
   
   
   
  1. package com.sid.service;
  2. import com.github.pagehelper.PageInfo;
  3. import com.sid.model.User;
  4. public interface UserService {
  5. int addUser(User user);
  6. int deleteUser(String id);
  7. int updateUser(User user);
  8. /*
  9. * pageNum 开始页数
  10. * pageSize 每页显示的数据条数
  11. * */
  12. PageInfo selectAll(int pageNum, int pageSize);
  13. }

UserServiceImpl.java


   
   
   
   
  1. package com.sid.service.impl;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.sid.mapper.UserMapper;
  5. import com.sid.model.User;
  6. import com.sid.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. @Service(value = "userService")
  11. public class UserServiceImpl implements UserService {
  12. @Autowired
  13. private UserMapper userMapper;
  14. @Override
  15. public int addUser(User user) {
  16. return userMapper.insertSelective(user);
  17. }
  18. @Override
  19. public int deleteUser(String id) {
  20. return userMapper.deleteByPrimaryKey(id);
  21. }
  22. @Override
  23. public int updateUser(User user) {
  24. return userMapper.updateByPrimaryKey(user);
  25. }
  26. /*
  27. * pageNum 开始页数
  28. * pageSize 每页显示的数据条数
  29. * */
  30. @Override
  31. public PageInfo selectAll(int pageNum, int pageSize) {
  32. PageHelper.startPage(pageNum, pageSize, "id desc"); //开始起始页
  33. List userList = userMapper.selectAll(); // 获取数据
  34. PageInfo page = new PageInfo<>(userList); // 实例化PageInfo
  35. return page;
  36. }
  37. }

UserMapper.java


   
   
   
   
  1. package com.sid.mapper;
  2. import com.sid.model.User;
  3. import org.springframework.stereotype.Component;
  4. import java.util.List;
  5. @Component
  6. public interface UserMapper {
  7. int deleteByPrimaryKey(String id);
  8. int insert(User record);
  9. int insertSelective(User record);
  10. User selectByPrimaryKey(String id);
  11. int updateByPrimaryKeySelective(User record);
  12. int updateByPrimaryKey(User record);
  13. List selectAll();
  14. }

UserMapping.xml


   
   
   
   
  1. xml version="1.0" encoding="UTF-8" ?>
  2. <mapper namespace="com.sid.mapper.UserMapper" >
  3. <resultMap id="BaseResultMap" type="com.sid.model.User" >
  4. <id column="id" property="id" jdbcType="BIGINT" />
  5. <result column="name" property="name" jdbcType="VARCHAR" />
  6. <result column="password" property="password" jdbcType="VARCHAR" />
  7. <result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR" />
  8. resultMap>
  9. <sql id="Base_Column_List" >
  10. id, name, password, mobile_phone
  11. sql>
  12. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  13. select
  14. <include refid="Base_Column_List" />
  15. from user
  16. where id = #{id,jdbcType=BIGINT}
  17. select>
  18. <select id="selectAll" resultMap="BaseResultMap" >
  19. select
  20. <include refid="Base_Column_List" />
  21. from user
  22. select>
  23. <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  24. delete from user
  25. where id = #{id,jdbcType=BIGINT}
  26. delete>
  27. <insert id="insert" parameterType="com.sid.model.User" >
  28. insert into user (id, name, password,
  29. mobile_phone)
  30. values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  31. #{mobilePhone,jdbcType=VARCHAR})
  32. insert>
  33. <insert id="insertSelective" parameterType="com.sid.model.User" >
  34. insert into user
  35. <trim prefix="(" suffix=")" suffixOverrides="," >
  36. <if test="id != null" >
  37. id,
  38. if>
  39. <if test="name != null" >
  40. name,
  41. if>
  42. <if test="password != null" >
  43. password,
  44. if>
  45. <if test="mobilePhone != null" >
  46. mobile_phone,
  47. if>
  48. trim>
  49. <trim prefix="values (" suffix=")" suffixOverrides="," >
  50. <if test="id != null" >
  51. #{id,jdbcType=BIGINT},
  52. if>
  53. <if test="name != null" >
  54. #{name,jdbcType=VARCHAR},
  55. if>
  56. <if test="password != null" >
  57. #{password,jdbcType=VARCHAR},
  58. if>
  59. <if test="mobilePhone != null" >
  60. #{mobilePhone,jdbcType=VARCHAR},
  61. if>
  62. trim>
  63. insert>
  64. <update id="updateByPrimaryKeySelective" parameterType="com.sid.model.User" >
  65. update user
  66. <set >
  67. <if test="name != null" >
  68. name = #{name,jdbcType=VARCHAR},
  69. if>
  70. <if test="password != null" >
  71. password = #{password,jdbcType=VARCHAR},
  72. if>
  73. <if test="mobilePhone != null" >
  74. mobile_phone = #{mobilePhone,jdbcType=VARCHAR},
  75. if>
  76. set>
  77. where id = #{id,jdbcType=BIGINT}
  78. update>
  79. <update id="updateByPrimaryKey" parameterType="com.sid.model.User" >
  80. update user
  81. set name = #{name,jdbcType=VARCHAR},
  82. password = #{password,jdbcType=VARCHAR},
  83. mobile_phone = #{mobilePhone,jdbcType=VARCHAR}
  84. where id = #{id,jdbcType=BIGINT}
  85. update>
  86. mapper>

User.java


   
   
   
   
  1. package com.sid.model;
  2. public class User {
  3. private Long id;
  4. private String name;
  5. private String password;
  6. private String mobilePhone;
  7. public Long getId() {
  8. return id;
  9. }
  10. public void setId(Long id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name == null ? null : name.trim();
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password == null ? null : password.trim();
  24. }
  25. public String getMobilePhone() {
  26. return mobilePhone;
  27. }
  28. public void setMobilePhone(String mobilePhone) {
  29. this.mobilePhone = mobilePhone == null ? null : mobilePhone.trim();
  30. }
  31. }

添加11条数据到数据库中

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第6张图片

查询

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第7张图片

结果

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第8张图片

打开mybatis执行的SQL打印会在后台看见

还是执行了一次select count(0)

Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件)_第9张图片

你可能感兴趣的:(Spring Boot2 + Mybatis 整合(Mybatis自动生成插件、分页插件))