先创建spring boot项目,不知道怎么创建项目的 可以看我上一篇文章

用到的环境 JDK8 、maven、lombok、mysql 5.7

swagger 是为了方便接口测试

一、Spring boot 集成mybatis plus

mysql数据库准备

建议创建一个新的数据库测试

执行下面初始化SQL:


  1. -- 创建测试用户表
  2. CREATE TABLE user
  3. (
  4. id BIGINT(20) NOT NULL COMMENT '主键ID',
  5. name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
  6. age INT(11) NULL DEFAULT NULL COMMENT '年龄',
  7. email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
  8. PRIMARY KEY (id)
  9. );
  10. -- 增加测试数据
  11. INSERT INTO user (id, name, age, email) VALUES
  12. (1, 'Jone', 18, 'test1@baomidou.com'),
  13. (2, 'Jack', 20, 'test2@baomidou.com'),
  14. (3, 'Tom', 28, 'test3@baomidou.com'),
  15. (4, 'Sandy', 21, 'test4@baomidou.com'),
  16. (5, 'Billie', 24, 'test5@baomidou.com');

1、添加 maven依赖

  1. <!--mybatis-plus start-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.4.3.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid</artifactId>
  10. <version>1.0.29</version>
  11. </dependency>
  12. <!-- 提供mysql驱动 -->
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. <version>5.1.38</version>
  17. </dependency>
  18. <!--mybatis-plus end-->

2、application.yml配置

  1. server:
  2. port: 9999
  3. spring:
  4. datasource:
  5. type: com.alibaba.druid.pool.DruidDataSource # 使用druid数据源
  6. driver-class-name: com.mysql.jdbc.Driver
  7. url: jdbc:mysql://127.0.0.1:3308/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false
  8. username: root
  9. password: 999999999

3、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @MapperScan("com.example.springbootmybatisplus.dao")
  5. @SpringBootApplication
  6. public class SpringbootMybatisPlusApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
  9. }
  10. }

4、编码,写例子测试

4.1、 编写实体类 User.java

  1. @Data
  2. public class User {
  3. private Long id;
  4. private String name;
  5. private Integer age;
  6. private String email;
  7. }

4.2、 编写Mapper类 UserMapper.java

  1. package com.example.springbootmybatisplus.dao;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.springbootmybatisplus.entity.User;
  4. public interface UserMapper extends BaseMapper<User> {
  5. }

4.3、 编写TestController接口进行测试

  1. import cn.hutool.core.collection.CollUtil;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.example.springbootmybatisplus.dao.UserMapper;
  5. import com.example.springbootmybatisplus.entity.User;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. @Slf4j
  13. @RestController
  14. @RequestMapping("testUser")
  15. public class TestController {
  16. @Autowired
  17. UserMapper userMapper;
  18. //直接分页查询
  19. @RequestMapping(value = "/selectPage",method = RequestMethod.GET)
  20. public IPage<User> selectPage(){
  21. log.info("selectPage start");
  22. IPage<User> page = new Page<>(1,2);
  23. userMapper.selectPage(page,null);
  24. if(CollUtil.isNotEmpty(page.getRecords())){
  25. page.getRecords().forEach(data->{
  26. log.info("{}",data);
  27. });
  28. }
  29. log.info("selectPage end");
  30. return page;
  31. }
  32. @RequestMapping(value = "/selectById",method = RequestMethod.GET)
  33. public User selectById(@RequestParam("id")Integer id){
  34. log.info("selectById start");
  35. User user = userMapper.selectById(id);
  36. log.info("user:{}",user);
  37. log.info("selectById end");
  38. return user;
  39. }
  40. }

4.4、 启动项目,接口进行测试

第一步先访问:http://127.0.0.1:9999/testUser/selectById?id=1

第二步再访问接口:http://127.0.0.1:9999/testUser/selectPage

控制台输出


  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.5.3)
  8. 2021-07-27 22:52:25.127 INFO 23542 --- [ main] c.e.s.SpringbootMybatisPlusApplication : Starting SpringbootMybatisPlusApplication using Java 1.8.0_231 on localhost with PID 23542 (/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/target/classes started by xkq in /Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus)
  9. 2021-07-27 22:52:25.130 INFO 23542 --- [ main] c.e.s.SpringbootMybatisPlusApplication : No active profile set, falling back to default profiles: default
  10. 2021-07-27 22:52:25.770 WARN 23542 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.baomidou.cloud.service.*.mapper*]' package. Please check your configuration.
  11. 2021-07-27 22:52:26.039 INFO 23542 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
  12. 2021-07-27 22:52:26.045 INFO 23542 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2021-07-27 22:52:26.046 INFO 23542 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
  14. 2021-07-27 22:52:26.093 INFO 23542 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2021-07-27 22:52:26.093 INFO 23542 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 918 ms
  16. _ _ |_ _ _|_. ___ _ | _
  17. | | |\/|_)(_| | |_\ |_)||_|_\
  18. / |
  19. 3.4.1
  20. 2021-07-27 22:52:26.902 INFO 23542 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
  21. 2021-07-27 22:52:26.903 INFO 23542 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
  22. 2021-07-27 22:52:26.913 INFO 23542 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
  23. 2021-07-27 22:52:26.922 INFO 23542 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
  24. 2021-07-27 22:52:27.026 INFO 23542 --- [ main] c.e.s.SpringbootMybatisPlusApplication : Started SpringbootMybatisPlusApplication in 2.274 seconds (JVM running for 2.733)
  25. 2021-07-27 22:52:35.289 INFO 23542 --- [nio-9999-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  26. 2021-07-27 22:52:35.289 INFO 23542 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  27. 2021-07-27 22:52:35.290 INFO 23542 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  28. 2021-07-27 22:52:45.023 INFO 23542 --- [nio-9999-exec-7] c.e.s.controller.TestController : selectById start
  29. 2021-07-27 22:52:45.067 INFO 23542 --- [nio-9999-exec-7] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
  30. 2021-07-27 22:52:45.273 INFO 23542 --- [nio-9999-exec-7] c.e.s.controller.TestController : user:User(id=1, name=Jone, age=18, email=test1@baomidou.com)
  31. 2021-07-27 22:52:45.274 INFO 23542 --- [nio-9999-exec-7] c.e.s.controller.TestController : selectById end
  32. 2021-07-27 22:53:38.385 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : selectPage start
  33. 2021-07-27 22:53:38.431 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : User(id=1, name=Jone, age=18, email=test1@baomidou.com)
  34. 2021-07-27 22:53:38.432 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : User(id=2, name=Jack, age=20, email=test2@baomidou.com)
  35. 2021-07-27 22:53:38.432 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : User(id=3, name=Tom, age=28, email=test3@baomidou.com)
  36. 2021-07-27 22:53:38.432 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
  37. 2021-07-27 22:53:38.432 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : User(id=5, name=Billie, age=24, email=test5@baomidou.com)
  38. 2021-07-27 22:53:38.432 INFO 23542 --- [nio-9999-exec-9] c.e.s.controller.TestController : selectPage end

从日志中可以看到,selectById方法能查询数据,但是分页查询没有生效,后面会说怎么整合分页插件。

上面代码都是参考mybaits plus官方整合h2的例子,然后整合成mysql数据库的,mybaits plus详细文档可以访问官方地址

二、mybatis plus 分页插件

上面的分页没有生效,我们可以输出执行的SQL,看下实际执行的SQL

1、application.yml 配置打印SQL日志

  1. mybatis-plus:
  2. configuration:
  3. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置这个 会把输出执行的SQL

2、访问下面接口,查看日志输出

继续:第一步先访问:http://127.0.0.1:9999/testUser/selectById?id=1

第二步再访问接口:http://127.0.0.1:9999/testUser/selectPage


  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.5.3)
  8. 2021-07-27 22:57:56.973 INFO 26426 --- [ main] c.e.s.SpringbootMybatisPlusApplication : Starting SpringbootMybatisPlusApplication using Java 1.8.0_231 on localhost with PID 26426 (/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/target/classes started by xkq in /Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus)
  9. 2021-07-27 22:57:56.976 INFO 26426 --- [ main] c.e.s.SpringbootMybatisPlusApplication : No active profile set, falling back to default profiles: default
  10. 2021-07-27 22:57:57.620 WARN 26426 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.baomidou.cloud.service.*.mapper*]' package. Please check your configuration.
  11. 2021-07-27 22:57:57.895 INFO 26426 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
  12. 2021-07-27 22:57:57.902 INFO 26426 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2021-07-27 22:57:57.902 INFO 26426 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
  14. 2021-07-27 22:57:57.966 INFO 26426 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2021-07-27 22:57:57.966 INFO 26426 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 941 ms
  16. Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
  17. Parsed mapper file: 'file [/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/target/classes/mapper/TestTableMapper.xml]'
  18. _ _ |_ _ _|_. ___ _ | _
  19. | | |\/|_)(_| | |_\ |_)||_|_\
  20. / |
  21. 3.4.1
  22. 2021-07-27 22:57:58.826 INFO 26426 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
  23. 2021-07-27 22:57:58.827 INFO 26426 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
  24. 2021-07-27 22:57:58.836 INFO 26426 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
  25. 2021-07-27 22:57:58.846 INFO 26426 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
  26. 2021-07-27 22:57:58.968 INFO 26426 --- [ main] c.e.s.SpringbootMybatisPlusApplication : Started SpringbootMybatisPlusApplication in 2.363 seconds (JVM running for 2.816)
  27. 2021-07-27 22:58:04.020 INFO 26426 --- [nio-9999-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  28. 2021-07-27 22:58:04.020 INFO 26426 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  29. 2021-07-27 22:58:04.021 INFO 26426 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  30. 2021-07-27 22:58:04.044 INFO 26426 --- [nio-9999-exec-1] c.e.s.controller.TestController : selectById start
  31. Creating a new SqlSession
  32. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ca5a623] was not registered for synchronization because synchronization is not active
  33. 2021-07-27 22:58:04.087 INFO 26426 --- [nio-9999-exec-1] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
  34. JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a83b478] will not be managed by Spring
  35. ==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
  36. ==> Parameters: 1(Integer)
  37. <== Columns: id, name, age, email
  38. <== Row: 1, Jone, 18, test1@baomidou.com
  39. <== Total: 1
  40. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ca5a623]
  41. 2021-07-27 22:58:04.295 INFO 26426 --- [nio-9999-exec-1] c.e.s.controller.TestController : user:User(id=1, name=Jone, age=18, email=test1@baomidou.com)
  42. 2021-07-27 22:58:04.295 INFO 26426 --- [nio-9999-exec-1] c.e.s.controller.TestController : selectById end
  43. 2021-07-27 22:58:09.794 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : selectPage start
  44. Creating a new SqlSession
  45. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@28b417c9] was not registered for synchronization because synchronization is not active
  46. JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a83b478] will not be managed by Spring
  47. ==> Preparing: SELECT id,name,age,email FROM user
  48. ==> Parameters:
  49. <== Columns: id, name, age, email
  50. <== Row: 1, Jone, 18, test1@baomidou.com
  51. <== Row: 2, Jack, 20, test2@baomidou.com
  52. <== Row: 3, Tom, 28, test3@baomidou.com
  53. <== Row: 4, Sandy, 21, test4@baomidou.com
  54. <== Row: 5, Billie, 24, test5@baomidou.com
  55. <== Total: 5
  56. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@28b417c9]
  57. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=1, name=Jone, age=18, email=test1@baomidou.com)
  58. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=2, name=Jack, age=20, email=test2@baomidou.com)
  59. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=3, name=Tom, age=28, email=test3@baomidou.com)
  60. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
  61. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=5, name=Billie, age=24, email=test5@baomidou.com)
  62. 2021-07-27 22:58:09.818 INFO 26426 --- [nio-9999-exec-2] c.e.s.controller.TestController : selectPage end

从执行SQL的日志看到selectById方法没问题,主要是selectPage方法没有加limit分页

3、配置mybatis plus分页插件,添加MybatisPlusConfig.java

  1. import com.baomidou.mybatisplus.annotation.DbType;
  2. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  3. import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
  4. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
  7. import org.mybatis.spring.annotation.MapperScan;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. //Spring boot方式
  11. @Configuration
  12. public class MybatisPlusConfig {
  13. // // 旧版
  14. // @Bean
  15. // public PaginationInterceptor paginationInterceptor() {
  16. // PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
  17. // // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
  18. // // paginationInterceptor.setOverflow(false);
  19. // // 设置最大单页限制数量,默认 500 条,-1 不受限制
  20. // // paginationInterceptor.setLimit(500);
  21. // // 开启 count 的 join 优化,只针对部分 left join
  22. // paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
  23. // return paginationInterceptor;
  24. // }
  25. // 最新版
  26. @Bean
  27. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  28. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  29. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  30. return interceptor;
  31. }
  32. }

4、测试MybatisPlusInterceptor分页插件

加完MybatisPlusConfig后重启服务,访问接口: http://127.0.0.1:9999/testUser/selectPage

从SQL执行日志可以看出 先执行COUNT查询总条数,最后在LIMIT分页取数据,说明分页插件配置成功了

  1. 2021-07-27 23:02:40.225 INFO 28656 --- [nio-9999-exec-2] c.e.s.controller.TestController : selectPage start
  2. Creating a new SqlSession
  3. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6acc6ecd] was not registered for synchronization because synchronization is not active
  4. JDBC Connection [com.mysql.jdbc.JDBC4Connection@2320cee0] will not be managed by Spring
  5. ==> Preparing: SELECT COUNT(*) FROM user
  6. ==> Parameters:
  7. <== Columns: COUNT(*)
  8. <== Row: 5
  9. <== Total: 1
  10. ==> Preparing: SELECT id,name,age,email FROM user LIMIT ?
  11. ==> Parameters: 2(Long)
  12. <== Columns: id, name, age, email
  13. <== Row: 1, Jone, 18, test1@baomidou.com
  14. <== Row: 2, Jack, 20, test2@baomidou.com
  15. <== Total: 2
  16. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6acc6ecd]
  17. 2021-07-27 23:02:40.299 INFO 28656 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=1, name=Jone, age=18, email=test1@baomidou.com)
  18. 2021-07-27 23:02:40.299 INFO 28656 --- [nio-9999-exec-2] c.e.s.controller.TestController : User(id=2, name=Jack, age=20, email=test2@baomidou.com)
  19. 2021-07-27 23:02:40.299 INFO 28656 --- [nio-9999-exec-2] c.e.s.controller.TestController : selectPage end

三、 mybatis plus 代码生成器

我对 mybatis plus 代码生成器的需求就是:

1、生成的代码 需要支持单表的crud

2、代码生成器通常默认生成的文件是controller、service、servieImpl、mapper(dao)、xml、entity;但是我还想多生成一个Vo类(多生成一个vo,主要是为了后期业务熟练了,想对已有的模版 进行修改 或者 增加生成新的生成文件,可以做一个参考)

3、xml文件 我要生成到 /src/main/resources/mapper文件夹下面

1、这是代码生成器需要生成的表,及测试数据

  1. -- 创建表
  2. CREATE TABLE `test_table` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `name` varchar(100) DEFAULT NULL COMMENT '名称',
  5. `start_date` date DEFAULT NULL COMMENT '开始日期',
  6. `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='测试-表';
  9. -- 创建一条测试数据
  10. INSERT INTO `test_table` VALUES (1, '你好', '2021-07-27', '2021-07-27 23:10:02');

2、Vo类模版文件

路径及文件名是:/my_template/my_entity_vo.java.ftl

把mybatis-plus-generator.jar包的entity模版文件复制出来,模版内容稍微改了下类名和package,实际生成的文件跟entity差不多

下图就是我复制模版的位置,因为我选择的是freemarker 模版,就复制了后缀是ftl的文件

最终my_entity_vo.java.ftl模版内容如下:

  1. package com.example.springbootmybatisplus.vo;
  2. <#list table.importPackages as pkg>
  3. import ${pkg};
  4. </#list>
  5. <#if swagger2>
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. </#if>
  9. <#if entityLombokModel>
  10. import lombok.Data;
  11. import lombok.EqualsAndHashCode;
  12. <#if chainModel>
  13. import lombok.experimental.Accessors;
  14. </#if>
  15. </#if>
  16. /**
  17. * <p>
  18. * ${table.comment!}
  19. * </p>
  20. *
  21. * @author ${author}
  22. * @since ${date}
  23. */
  24. <#if entityLombokModel>
  25. @Data
  26. <#if superEntityClass??>
  27. @EqualsAndHashCode(callSuper = true)
  28. <#else>
  29. @EqualsAndHashCode(callSuper = false)
  30. </#if>
  31. <#if chainModel>
  32. @Accessors(chain = true)
  33. </#if>
  34. </#if>
  35. <#if table.convert>
  36. @TableName("${table.name}")
  37. </#if>
  38. <#if swagger2>
  39. @ApiModel(value="${entity}对象", description="${table.comment!}")
  40. </#if>
  41. <#if superEntityClass??>
  42. public class ${entity}Vo extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
  43. <#elseif activeRecord>
  44. public class ${entity}Vo extends Model<${entity}> {
  45. <#else>
  46. public class ${entity}Vo implements Serializable {
  47. </#if>
  48. <#if entitySerialVersionUID>
  49. private static final long serialVersionUID = 1L;
  50. </#if>
  51. <#-- ---------- BEGIN 字段循环遍历 ---------->
  52. <#list table.fields as field>
  53. <#if field.keyFlag>
  54. <#assign keyPropertyName="${field.propertyName}"/>
  55. </#if>
  56. <#if field.comment!?length gt 0>
  57. <#if swagger2>
  58. @ApiModelProperty(value = "${field.comment}")
  59. <#else>
  60. /**
  61. * ${field.comment}
  62. */
  63. </#if>
  64. </#if>
  65. <#if field.keyFlag>
  66. <#-- 主键 -->
  67. <#if field.keyIdentityFlag>
  68. @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
  69. <#elseif idType??>
  70. @TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
  71. <#elseif field.convert>
  72. @TableId("${field.annotationColumnName}")
  73. </#if>
  74. <#-- 普通字段 -->
  75. <#elseif field.fill??>
  76. <#-- ----- 存在字段填充设置 ----->
  77. <#if field.convert>
  78. @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
  79. <#else>
  80. @TableField(fill = FieldFill.${field.fill})
  81. </#if>
  82. <#elseif field.convert>
  83. @TableField("${field.annotationColumnName}")
  84. </#if>
  85. <#-- 乐观锁注解 -->
  86. <#if (versionFieldName!"") == field.name>
  87. @Version
  88. </#if>
  89. <#-- 逻辑删除注解 -->
  90. <#if (logicDeleteFieldName!"") == field.name>
  91. @TableLogic
  92. </#if>
  93. private ${field.propertyType} ${field.propertyName};
  94. </#list>
  95. <#------------ END 字段循环遍历 ---------->
  96. <#if !entityLombokModel>
  97. <#list table.fields as field>
  98. <#if field.propertyType == "boolean">
  99. <#assign getprefix="is"/>
  100. <#else>
  101. <#assign getprefix="get"/>
  102. </#if>
  103. public ${field.propertyType} ${getprefix}${field.capitalName}() {
  104. return ${field.propertyName};
  105. }
  106. <#if chainModel>
  107. public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  108. <#else>
  109. public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  110. </#if>
  111. this.${field.propertyName} = ${field.propertyName};
  112. <#if chainModel>
  113. return this;
  114. </#if>
  115. }
  116. </#list>
  117. </#if>
  118. <#if entityColumnConstant>
  119. <#list table.fields as field>
  120. public static final String ${field.name?upper_case} = "${field.name}";
  121. </#list>
  122. </#if>
  123. <#if activeRecord>
  124. @Override
  125. protected Serializable pkVal() {
  126. <#if keyPropertyName??>
  127. return this.${keyPropertyName};
  128. <#else>
  129. return null;
  130. </#if>
  131. }
  132. </#if>
  133. <#if !entityLombokModel>
  134. @Override
  135. public String toString() {
  136. return "${entity}{" +
  137. <#list table.fields as field>
  138. <#if field_index==0>
  139. "${field.propertyName}=" + ${field.propertyName} +
  140. <#else>
  141. ", ${field.propertyName}=" + ${field.propertyName} +
  142. </#if>
  143. </#list>
  144. "}";
  145. }
  146. </#if>
  147. }

3、添加maven依赖

我选择使用freemarker作为模版引擎,所以需要引入freemarker依赖

  1. <!--mybatis-plus 代码生成器 start-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-generator</artifactId>
  5. <version>3.4.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.freemarker</groupId>
  9. <artifactId>freemarker</artifactId>
  10. <version>2.3.28</version>
  11. </dependency>
  12. <!--mybatis-plus 代码生成器 end-->

4、配置文件添加扫描xml位置以及实体类的位置

这个主要是为了让mapper(dao)跟xml文件里面的方法关联起来

  1. mybatis-plus:
  2. #xml文件路径,多个路径有xml文件用逗号分隔
  3. mapper-locations: classpath*:/mapper/**/*.xml
  4. #实体扫描,多个package用逗号或者分号分隔
  5. typeAliasesPackage: com.example.springbootmybatisplus.entity

5、代码生成器启动类

  1. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  2. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  3. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  4. import com.baomidou.mybatisplus.generator.AutoGenerator;
  5. import com.baomidou.mybatisplus.generator.InjectionConfig;
  6. import com.baomidou.mybatisplus.generator.config.*;
  7. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  8. import com.baomidou.mybatisplus.generator.config.rules.DateType;
  9. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  10. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  11. import lombok.extern.slf4j.Slf4j;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Scanner;
  15. // 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
  16. @Slf4j
  17. public class CodeGenerator {
  18. /**
  19. * <p>
  20. * 读取控制台内容
  21. * </p>
  22. */
  23. public static String scanner(String tip) {
  24. Scanner scanner = new Scanner(System.in);
  25. StringBuilder help = new StringBuilder();
  26. help.append("请输入" + tip + ":");
  27. System.out.println(help.toString());
  28. if (scanner.hasNext()) {
  29. String ipt = scanner.next();
  30. if (StringUtils.isNotBlank(ipt)) {
  31. return ipt;
  32. }
  33. }
  34. throw new MybatisPlusException("请输入正确的" + tip + "!");
  35. }
  36. public static void main(String[] args) {
  37. // 代码生成器
  38. AutoGenerator mpg = new AutoGenerator();
  39. // 全局配置
  40. GlobalConfig gc = new GlobalConfig();
  41. String projectPath = System.getProperty("user.dir");
  42. log.info("projectPath:{}",projectPath);
  43. gc.setOutputDir(projectPath + "/src/main/java");
  44. gc.setAuthor("小旋风");//开发人员
  45. gc.setOpen(false);//代码生成后,是否打开文件夹
  46. gc.setSwagger2(true);// 实体属性 Swagger2 注解
  47. gc.setFileOverride(false);//是否覆盖已有文件(true会覆盖 已经存在的文件)
  48. /**
  49. * 只使用 java.util.date 代替
  50. */
  51. gc.setDateType(DateType.ONLY_DATE);
  52. gc.setBaseColumnList(true);//开启 baseColumnList
  53. gc.setBaseResultMap(true);//开启 BaseResultMap
  54. mpg.setGlobalConfig(gc);
  55. // 数据源配置
  56. DataSourceConfig dsc = new DataSourceConfig();
  57. dsc.setUrl("jdbc:mysql://127.0.0.1:3308/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false");
  58. // dsc.setSchemaName("public");
  59. dsc.setDriverName("com.mysql.jdbc.Driver");
  60. dsc.setUsername("root");
  61. dsc.setPassword("999999999");
  62. mpg.setDataSource(dsc);
  63. // 包配置
  64. PackageConfig pc = new PackageConfig();
  65. // pc.setModuleName(scanner("模块名"));
  66. pc.setParent("com.example.springbootmybatisplus");
  67. pc.setMapper("dao");//生产的mapper 放到dao目录下
  68. mpg.setPackageInfo(pc);
  69. // 自定义配置
  70. InjectionConfig cfg = new InjectionConfig() {
  71. @Override
  72. public void initMap() {
  73. // to do nothing
  74. }
  75. };
  76. // 如果模板引擎是 freemarker
  77. String templatePath = "/my_template/my_entity_req.java.ftl";//多生成一个实体类,仅仅做参考,没有这个需求的,可以去掉
  78. // 如果模板引擎是 velocity
  79. // String templatePath = "/templates/mapper.xml.vm";
  80. String templatePathXml = "/templates/mapper.xml.ftl";//下面templateConfig.setXml(null); 自定义生成xml,默认的xml设置为null
  81. // 自定义输出配置
  82. List<FileOutConfig> focList = new ArrayList<>();
  83. // 自定义配置会被优先输出
  84. focList.add(new FileOutConfig(templatePathXml) {
  85. @Override
  86. public String outputFile(TableInfo tableInfo) {
  87. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  88. return projectPath + "/src/main/resources/mapper/"
  89. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  90. }
  91. });
  92. focList.add(new FileOutConfig(templatePath) {
  93. @Override
  94. public String outputFile(TableInfo tableInfo) {
  95. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  96. return projectPath + "/src/main/java/com/example/springbootmybatisplus/vo/" +tableInfo.getEntityName() + "Vo" + StringPool.DOT_JAVA;
  97. }
  98. });
  99. /*
  100. cfg.setFileCreate(new IFileCreate() {
  101. @Override
  102. public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  103. // 判断自定义文件夹是否需要创建
  104. checkDir("调用默认方法创建的目录,自定义目录用");
  105. if (fileType == FileType.MAPPER) {
  106. // 已经生成 mapper 文件判断存在,不想重新生成返回 false
  107. return !new File(filePath).exists();
  108. }
  109. // 允许生成模板文件
  110. return true;
  111. }
  112. });
  113. */
  114. cfg.setFileOutConfigList(focList);
  115. mpg.setCfg(cfg);
  116. // 配置模板
  117. TemplateConfig templateConfig = new TemplateConfig();
  118. // 配置自定义输出模板
  119. //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  120. // templateConfig.setEntity("templates/entity2.java");
  121. // templateConfig.setService();
  122. // templateConfig.setController();
  123. /**
  124. * 设置不生成 默认的xml,因为生成的位置会在 java文件夹下面,
  125. * 我想生成的xml放到resources.mapper文件夹下面,
  126. * 所以使用设置了自定义【templatePathXml】的模版去生成
  127. */
  128. templateConfig.setXml(null);
  129. mpg.setTemplate(templateConfig);
  130. // 策略配置
  131. StrategyConfig strategy = new StrategyConfig();
  132. strategy.setNaming(NamingStrategy.underline_to_camel);
  133. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  134. // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
  135. strategy.setEntityLombokModel(true);
  136. strategy.setRestControllerStyle(true);
  137. // 公共父类
  138. // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
  139. // 写于父类中的公共字段
  140. // strategy.setSuperEntityColumns("id"); //没有父类公共字段的去掉
  141. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  142. strategy.setControllerMappingHyphenStyle(true);
  143. strategy.setTablePrefix(pc.getModuleName() + "_");
  144. mpg.setStrategy(strategy);
  145. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  146. mpg.execute();
  147. }
  148. }

5、运行CodeGenerator类的main方法

在控制台输入要生成的表名【test_table】,下面是输出日志:

  1. 23:43:45.919 [main] INFO com.example.springbootmybatisplus.generator.CodeGenerator - projectPath:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus
  2. 请输入表名,多个英文逗号分割:
  3. test_table
  4. 23:43:55.961 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
  5. 23:43:56.555 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.xml.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/resources/mapper//TestTableMapper.xml
  6. 23:43:56.641 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/entity.java.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/java/com/example/springbootmybatisplus/entity/TestTable.java
  7. 23:43:56.643 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.java.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/java/com/example/springbootmybatisplus/dao/TestTableMapper.java
  8. 23:43:56.645 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/service.java.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/java/com/example/springbootmybatisplus/service/ITestTableService.java
  9. 23:43:56.646 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/java/com/example/springbootmybatisplus/service/impl/TestTableServiceImpl.java
  10. 23:43:56.648 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.ftl; 文件:/Users/xkq/Downloads/it/xkq_git项目/springboot-example/springboot-mybatis-plus/src/main/java/com/example/springbootmybatisplus/controller/TestTableController.java
  11. 23:43:56.648 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================

6、最终生成的效果图如下

7、测试生成的代码

写个查询方法,来测试一下

7.1、TestTableMapper.xml 添加getCustomOne方法

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.springbootmybatisplus.dao.TestTableMapper">
  4. <!-- 通用查询映射结果 -->
  5. <resultMap id="BaseResultMap" type="com.example.springbootmybatisplus.entity.TestTable">
  6. <id column="id" property="id" />
  7. <result column="name" property="name" />
  8. <result column="start_date" property="startDate" />
  9. <result column="create_time" property="createTime" />
  10. </resultMap>
  11. <!-- 通用查询结果列 -->
  12. <sql id="Base_Column_List">
  13. id, name, start_date, create_time
  14. </sql>
  15. <select id="getCustomOne" resultMap="BaseResultMap" >
  16. select id, name, start_date, create_time from test_table limit 1
  17. </select>
  18. </mapper>

7.2、TestTableMapper.java 添加getCustomOne方法

  1. import com.example.springbootmybatisplus.entity.TestTable;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. /**
  4. * <p>
  5. * 测试-表 Mapper 接口
  6. * </p>
  7. *
  8. * @author 小旋风
  9. * @since 2021-07-27
  10. */
  11. public interface TestTableMapper extends BaseMapper<TestTable> {
  12. public TestTable getCustomOne();
  13. }

7.3、编写接口进行测试

  1. import com.baomidou.mybatisplus.core.metadata.IPage;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.example.springbootmybatisplus.dao.TestTableMapper;
  4. import com.example.springbootmybatisplus.entity.TestTable;
  5. import com.example.springbootmybatisplus.service.ITestTableService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. /**
  11. * <p>
  12. * 测试-表 前端控制器
  13. * </p>
  14. *
  15. * @author 小旋风
  16. * @since 2021-07-27
  17. */
  18. @RestController
  19. @RequestMapping("/test-table")
  20. public class TestTableController {
  21. @Autowired
  22. private ITestTableService testTableService;
  23. @Autowired
  24. private TestTableMapper testTableMapper;
  25. @RequestMapping(value = "/getCustomOne",method = RequestMethod.GET)
  26. public TestTable getCustomOne(){
  27. //这个方法主要是验证 mapper跟xml文件 关联上了,mapper-locations生效了
  28. return testTableMapper.getCustomOne();
  29. }
  30. @RequestMapping(value = "/page",method = RequestMethod.GET)
  31. public IPage<TestTable> page(){
  32. Page<TestTable> page = new Page();
  33. page.setSize(10);
  34. return testTableService.page(page);
  35. }
  36. }

7.4、访问接口

访问接口:http://127.0.0.1:9999/test-table/getCustomOne

日志输出如下:

  1. Creating a new SqlSession
  2. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@262025d3] was not registered for synchronization because synchronization is not active
  3. 2021-07-27 23:53:18.981 INFO 55251 --- [nio-9999-exec-1] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
  4. JDBC Connection [com.mysql.jdbc.JDBC4Connection@30c4c95c] will not be managed by Spring
  5. ==> Preparing: select id, name, start_date, create_time from test_table limit 1
  6. ==> Parameters:
  7. <== Columns: id, name, start_date, create_time
  8. <== Row: 1, 你好, 2021-07-27, 2021-07-27 23:10:02.0
  9. <== Total: 1
  10. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@262025d3]



日志输出无报错,浏览器上也看到返回结果了,说明代码生成器整合成功了~

三、集成swaager

上面的例子,不放整合swagger的代码了,毕竟spring boot整合mybaits plus需要配置的东西不多,放在一起有点乱,就单独拎出来写,集成swagger 是为了方便测试接口用。

1、pom.xml添加依赖:

  1. <!--测试接口 添加swagger start-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger-ui</artifactId>
  5. <version>2.6.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger2</artifactId>
  10. <version>2.6.1</version>
  11. </dependency>
  12. <!--测试接口 添加swagger end-->

2、配置SwaggerConfig

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.ApiInfoBuilder;
  4. import springfox.documentation.builders.PathSelectors;
  5. import springfox.documentation.builders.RequestHandlerSelectors;
  6. import springfox.documentation.service.ApiInfo;
  7. import springfox.documentation.spi.DocumentationType;
  8. import springfox.documentation.spring.web.plugins.Docket;
  9. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  10. @Configuration
  11. @EnableSwagger2
  12. public class SwaggerConfig {
  13. private Boolean swaggerEnabled = true;//是否启用swagger 可以把配置放到配置文件
  14. @Bean
  15. public Docket createRestApi() {
  16. return new Docket(DocumentationType.SWAGGER_2)
  17. .apiInfo(apiInfo())
  18. .enable(swaggerEnabled)
  19. .select()
  20. .apis(RequestHandlerSelectors.basePackage("com.example.springbootmybatisplus"))
  21. .paths(PathSelectors.any())
  22. .build();
  23. }
  24. private ApiInfo apiInfo() {
  25. return new ApiInfoBuilder()
  26. .title("接口文档")
  27. .description("spring boot整合mybatis plus~")
  28. .termsOfServiceUrl("https://www.cnblogs.com/x-kq/p/15068023.html")
  29. .version("6.6.6")
  30. .build();
  31. }
  32. }

3、swagger访问地址

访问这个地址测试接口比较方便

http://127.0.0.1:9999/swagger-ui.html


代码我已经传到gitee了,有兴趣的可以clone,传送门


spring boot(二)整合mybatis plus+ 分页插件 + 代码生成的更多相关文章

  1. Spring Boot:整合MyBatis框架

    综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  2. spring boot 2整合mybatis

    mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...

  3. Spring整合Mybatis 之分页插件使用

    [分页插件项目中的正式代码一共有个5个Java文件,这5个文件的说明如下] Page<E>[必须]:分页参数类,该类继承ArrayList,虽然分页查询返回的结果实际类型是Page< ...

  4. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  5. Spring Boot 应用系列 3 -- Spring Boot 2 整合MyBatis和Druid,多数据源

    本文演示多数据源(MySQL+SQL Server)的配置,并且我引入了分页插件pagehelper. 1. 项目结构 (1)db.properties存储数据源和连接池配置. (2)两个数据源的ma ...

  6. Springboot整合Mybatis 之分页插件使用

    1: 引入jar包 <!-- 引入MyBatis分页插件--> <dependency> <groupId>com.github.pagehelper</gr ...

  7. Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid

    本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置. 1. pom.xml文件配置 需要在dependencies节点添加: <!-- MySQL --> ...

  8. Spring Boot (二) 整合 Redis

    前言 本文将会基于 springboot 2.1.8.RELEASE 简单整合 Redis ,适合新手小白入门 Spring Boot 整合 Redis 入门 1.pom.xml 中引入 redis ...

  9. SpringBoot整合MyBatis的分页插件PageHelper

    1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...

随机推荐

  1. 10:ValueError: Cannot assign "'2'": "Comment.article" must be a "Article" instance

    报错中出现类似ValueError: Cannot assign "'XXX'": "Comment.article" must be a "XXX& ...

  2. 可微渲染 SoftRas 实践

    SoftRas 是目前主流三角网格可微渲染器之一. 可微渲染通过计算渲染过程的导数,使得从单张图片学习三维结构逐渐成为现实.可微渲染目前被广泛地应用于三维重建,特别是人体重建.人脸重建和三维属性估计等 ...

  3. JVM 内存溢出 实战 (史上最全)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  4. java处理方法的多个返回值

    我第一次接触到元组是在java编程思想这本书中,当时我正为方法的多个返回值苦恼.因为我之前处理多个返回值方法的时候,要不建一个新的实体类,要不在接收的方法中建立一个对象,返回值之前给其赋值,要不通过异 ...

  5. csp-s模拟测试55(9.29)联「线段树」·赛「??」题「神仙DP」

    T1 联 考试两个小时终于调过了,话说一个傻逼错最后还是静态查出错的..... 大概维护两个懒标记,一个区间覆盖,一个区间异或,然后保证每个区间只会存在一种懒标记. 然后维护区间0的个数,查询时查询那 ...

  6. 20201123 实验二《Python程序设计》实验报告

    20201123 2020-2021-2 <Python程序设计>实验报告课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验日期 ...

  7. 如果给IIS添加防火墙入站配置,支持外部或者局域网访问

    背景简介 也许你试着在本机IIS运行了一些网站,但是奇怪的是,同网络的终端却无法访问你,这时候极有可能被防火墙拦截了,所以我们要找到正确的姿势来开启魔法了. 找到入站规则设置 不管你是Win7还是Wi ...

  8. 7.1、controller节点配置

    0.配置openstack版本yum源: yum install centos-release-openstack-rocky 1.安装 OpenStack 客户端: yum install pyth ...

  9. layui 点击按钮 界面会刷新问题

    将button 改为input: <input class="layui-btn" type="button" style="border:so ...

  10. 案例分享:Qt西门子机床人机界面以及数据看板定制(西门子通讯,mysql数据库,生产信息,参数信息,信息化看板,权限控制,播放器,二维图表,参数调试界面)

    若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/118685521 长期持续带来更多项目与技术分享 ...