Home >Java >javaTutorial >How Java Fluent Mybatis builds projects and implements code generation
Look at the official feature map
It gives several features that are very comprehensive at first glance, among which they are more attractive. Mine is two points.
1. The syntax given in the picture is very similar to SQL. If you don’t look carefully, you would think that it is a direct SQL statement thrown up. It looks more practical.
2. No xml&mapper, although mybatis-plus has implemented the IService interface to implement most sql operations
The process of building a project with springboot is not too much Without going into details, here is my practical springboot version
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>
The code structure is as follows:
<properties> <fluent-mybatis.version>1.8.7</fluent-mybatis.version> </properties> <dependencies> <!-- 引入fluent-mybatis 运行依赖包, scope为compile --> <dependency> <groupId>com.github.atool</groupId> <artifactId>fluent-mybatis</artifactId> <version>${fluent-mybatis.version}</version> </dependency> <!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 --> <dependency> <groupId>com.github.atool</groupId> <artifactId>fluent-mybatis-processor</artifactId> <scope>provided</scope> <version>${fluent-mybatis.version}</version> </dependency> </dependencies>
Complete maven dependencies are as follows
4.0.0 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>com.hy fluent-mybatis-project 0.0.1-SNAPSHOT fluent-mybatis-project Demo project for Spring Boot 1.8 1.8.7 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org jaudiotagger 2.0.1 com.google.guava guava 30.1.1-jre cn.hutool hutool-all 5.5.2 com.github.atool fluent-mybatis ${fluent-mybatis.version} com.github.atool fluent-mybatis-processor provided ${fluent-mybatis.version} org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
Create a test table in the database. The table is relatively simple, try it first. The sql is as follows:
CREATE TABLE `test_fluent_mybatis` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '自增主键', `name` varchar(255) DEFAULT NULL COMMENT '姓名', `age` int DEFAULT NULL COMMENT '年龄', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `del_flag` int DEFAULT NULL COMMENT '是否删除', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Note: Put it in the test code package. The structure is as follows:
Code generation tool code, first follow the official simple example, as follows:
package com.hy.fmp; import cn.org.atool.generator.FileGenerator; import cn.org.atool.generator.annotation.Table; import cn.org.atool.generator.annotation.Tables; import org.junit.jupiter.api.Test; public class EntityGeneratorDemo { // 数据源 url static final String url = "jdbc:mysql://192.168.0.16:3306/test?useUnicode=true&characterEncoding=utf8"; // 数据库用户名 static final String username = "root"; // 数据库密码 static final String password = "123456"; @Test public void generate() throws Exception { // 引用配置类,build方法允许有多个配置类 FileGenerator.build(Empty.class); } @Tables( // 设置数据库连接信息 url = url, username = username, password = password, // 设置entity类生成src目录, 相对于 user.dir srcDir = "src/main/java", // 设置entity类的package值 basePack = "com.hy.fmp.fluent", // 设置dao接口和实现的src目录, 相对于 user.dir daoDir = "src/main/java", // 设置哪些表要生成Entity文件 tables = {@Table(value = {"test_fluent_mybatis"})}) static class Empty { // 类名随便取, 只是配置定义的一个载体 } }
Execute the code generation tool, see See what is generated.
You can see the generated package as follows.
There is a pit here, see the screenshot below
In fact, the official gave a solution, but did not explain it.
In short, you need to use maven to compile it, so we compile it.
#After compilation, we can find the compiled file in the error package location in the target.
The classes that reported errors before no longer report errors. Perfect.
The above is the detailed content of How Java Fluent Mybatis builds projects and implements code generation. For more information, please follow other related articles on the PHP Chinese website!