赞
踩
SpringBoot3.0 开始最低要求 Java 17,虽然目前最新的版本为 JDK22,但是在官网上看到 JDK23 在今年9月又要发布了,感觉这 JDK 也有点太过于给力了
所以我们选择用目前的 LTS 版本 JDK21 就好了,不用追求最新的
从官网的 https://start.spring.io/ 可以看到,目前的 SpringBoot 的最新正式版为 3.2.5,JAVA版本支持 17、21、22
mybatis-plus 针对 springboot3.x 发布了新的依赖库,我们要选择带 spring-boot3 的版本
JDK 选择 21,Java 选择 21
Spring Boot 选择 3.2.5,依赖选择 Spring Web
最后点击 Create 创建项目
引入了 mybatis-plus 依赖包,还有对应生成器所需的依赖包
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>3.2.5</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
-
- <groupId>org.liurb</groupId>
- <artifactId>Springboot3-CRUD</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>Springboot3-CRUD</name>
- <description>Springboot3-CRUD</description>
-
- <properties>
- <java.version>21</java.version>
- <mybatis-plus.version>3.5.6</mybatis-plus.version>
- <freemarker.version>2.3.32</freemarker.version>
- <mysql-connector-java.version>8.4.0</mysql-connector-java.version>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.mysql</groupId>
- <artifactId>mysql-connector-j</artifactId>
- <version>${mysql-connector-java.version}</version>
- <scope>runtime</scope>
- </dependency>
-
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>${freemarker.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
我们创建一张表 demo_user 进行测试
创建代码生成器
- public class CodeGenerator {
-
- // 数据库连接配置
- private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
- private static final String JDBC_URL = "jdbc:mysql://192.168.1.31:3307/my_db?useSSL=false";
- private static final String JDBC_USER_NAME = "test_usr";
- private static final String JDBC_PASSOWRD = "test_usr#Passw0rd";
-
- // 输出目录
- private static final String MAIN_JAVA_PATH = "/src/main/java";
- private static final String MAIN_MAPPER_PATH = "/src/main/resources/mapper";
-
- // 包名和模块名
- private static final String PACKAGE_NAME = "org.liurb.springboot3";
- private static final String MODULE_NAME = "crud";
-
-
- // 表名,多个表使用英文逗号分割
- private static final String TBL_NAMES = "demo_user";
-
- // 表名的前缀,从表生成代码时会去掉前缀
- private static final String TABLE_PREFIX = "";
-
-
- // 生成代码入口main方法
- public static void main(String[] args) {
-
- FastAutoGenerator.create(JDBC_URL, JDBC_USER_NAME, JDBC_PASSOWRD)
- .globalConfig(builder -> builder
- .author("liurb") // 设置作者
- .outputDir(Paths.get(System.getProperty("user.dir")) + MAIN_JAVA_PATH) // 输出路径
- .commentDate("yyyy-MM-dd")
- .disableOpenDir() // 禁止打开输出目录
- )
- .packageConfig(builder -> builder
- .parent(PACKAGE_NAME) // 设置需要生成的表名
- .moduleName(MODULE_NAME) // 设置过滤表前缀
- .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir")) + MAIN_MAPPER_PATH)) // 设置mapperXml生成路径
- )
- .strategyConfig(builder -> builder
- .addInclude(TBL_NAMES) // 设置需要生成的表名
- .addTablePrefix(TABLE_PREFIX) // 设置过滤表前缀
- .entityBuilder() // 设置实体类
- .enableFileOverride() // 实体类覆盖
- .enableTableFieldAnnotation() // 属性加上说明注释
- .enableLombok() // 使用lombok
- .serviceBuilder() // 设置服务类
- .formatServiceFileName("%sService") // 格式化service类
- )
- .templateEngine(new FreemarkerTemplateEngine())
- .execute();
-
- }
-
- }
运行生成 demo_user 表的对应代码
- spring:
- application:
- name: Springboot3-CRUD
-
- #配置数据源
- datasource:
- url: jdbc:mysql://192.168.1.31:3307/my_db?useSSL=false&useUnicode=true&characterEncoding=utf-8
- username: test_usr
- password: test_usr#Passw0rd
- driver-class-name: com.mysql.cj.jdbc.Driver
- hikari:
- minimum-idle: 5
- maximum-pool-size: 10
- max-lifetime: 1800000
- idle-timeout: 180000
- connection-timeout: 30000
- @SpringBootApplication
- @MapperScan("org.liurb.**.mapper") // mapper 类路径
- public class Springboot3CrudApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(Springboot3CrudApplication.class, args);
- }
-
- }
我们通过单元测试保存一条记录
- @SpringBootTest
- class Springboot3CrudApplicationTests {
-
- @Resource
- DemoUserService demoUserService;
-
- @Test
- void contextLoads() {
-
- DemoUser record = new DemoUser();
- record.setName("李四");
- record.setAge(18);
- demoUserService.save(record);
-
- }
-
- }
记录保存成功
分页拦截器配置
- @Configuration
- public class MybatisPlusConfig {
-
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
- MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
- mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
- return mybatisPlusInterceptor;
- }
- }
分页查询大于等于12岁的用户,获取第一页的十条数据
- @Test
- void contextLoads() {
-
- Page page = new Page<>(1, 10);
-
- IPage<DemoUser> pageRecords = demoUserService.page(page, new LambdaQueryWrapper<DemoUser>().ge(DemoUser::getAge, 12));
- List<DemoUser> records = pageRecords.getRecords();
- System.out.println(records);
-
- }
运行截图如下:
sql 打印
查询的结果
在控制层加入查询接口,根据 用户id 查询对应的记录
- @RestController
- @RequestMapping("/crud/demoUser")
- public class DemoUserController {
-
- @Resource
- DemoUserService demoUserService;
-
- @GetMapping("/{userId}")
- public DemoUser user(@PathVariable Long userId) {
-
- return demoUserService.getById(userId);
- }
-
- }
请求返回
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。