Java Fluent Mybatis实战之构建项目与代码生成篇上
作者:剑客阿良_ALiang 时间:2022-06-10 00:10:33
简述
偶然看到一篇关于阿里新orm框架的文章,好奇的点了进去。开发后端多年,看到这个还是有点兴奋的。常用mysql的orm框架mybatis、jpa,到后来的优化框架mybatis-plus都是用过,他们或多或少都有优缺点吧。程序员本就是日常革新技术的职业,所以了解更多的框架绝对不会有错误。所以我尝试着把自己学习该框架的过程,记录下来,尽可能去掉一些项目工程中用不到的功能,展示一些实用有帮助的代码。
特性
首先分享一下码云上的项目链接:码云地址
看一下官方给出的特性图
给出对几个特性乍一看还是很全面的,其中比较吸引我的是两点。
1、从图中给出的语法,和sql十分相近,不仔细看还以为是直接sql语句扔了上来。看上去就比较实用。
2、No xml&mapper,虽然mybatis-plus已经做到实用 IService接口实现大部分的sql操作
项目搭建
springboot搭建一项目的过程就不过多赘述了,这里说下我实用的springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
代码结构如下:
maven依赖引入-fluent-mybatis
<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>
完整maven依赖如下
<?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>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hy</groupId>
<artifactId>fluent-mybatis-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fluent-mybatis-project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<fluent-mybatis.version>1.8.7</fluent-mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
<!-- 引入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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
表构建
在数据库创建一张测试表,表比较简单,先试试看。sql如下:
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;
代码生成工具类
注意:放到测试代码包中。结构如下图:
代码生成工具类代码,先按照官方给的简单样例来,如下:
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 { // 类名随便取, 只是配置定义的一个载体
}
}
执行代码生成工具,看看都生成了些什么。
可以看到生成的包如下。
解决类找不到问题
这里有个坑,看下面的截图
其实官方给了解决方法,只是没有对此说明。
简而言之就是你需要使用maven编译一下,所以我们compile一下。
编译结束后我们可以在target中,找到报错包位置中的编译文件。
之前报错的类已经不再报错了。完美。
来源:https://huyi-aliang.blog.csdn.net/article/details/120848199
标签:Java,Fluent,Mybatis,实战,项目构建
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 实现repalceAll只替换第二个匹配到的字符串
2021-06-12 11:56:20
Java实现在Word指定位置插入分页符
2021-06-29 03:24:44
![](https://img.aspxhome.com/file/2023/1/75451_0s.png)
SpringIOC容器Bean的作用域及生命周期实例
2023-10-01 11:21:34
![](https://img.aspxhome.com/file/2023/1/76611_0s.png)
JAVA中的基本数据类型
2023-07-29 07:38:34
DataGridView冻结列或行、列顺序调整、操作行头列头标题的方法
2021-10-25 06:53:41
![](https://img.aspxhome.com/file/2023/5/86945_0s.jpg)
spring springMVC中常用注解解析
2023-09-14 20:45:46
SpringBoot项目的测试类实例解析
2021-05-29 20:35:04
springboot @Valid注解对嵌套类型的校验功能
2023-07-01 13:02:18
![](https://img.aspxhome.com/file/2023/9/77159_0s.png)
Android实现美团外卖底部导航栏动画
2022-09-21 20:09:13
![](https://img.aspxhome.com/file/2023/9/98679_0s.gif)
Java如何将ResultSet结果集遍历到List中
2022-07-01 06:30:46
![](https://img.aspxhome.com/file/2023/7/83097_0s.jpg)
MyBatis-Plus结合Layui实现分页方法
2023-07-10 13:23:09
![](https://img.aspxhome.com/file/2023/9/85899_0s.png)
Mybatis-plus foreach拼接字符串查询无数据返回问题
2022-09-26 17:29:02
Android recyclerview实现纵向虚线时间轴的示例代码
2023-08-23 07:03:39
![](https://img.aspxhome.com/file/2023/3/102613_0s.png)
JavaWeb文件上传入门教程
2022-04-22 10:11:46
详解spring cloud eureka注册中心
2023-11-10 17:54:10
![](https://img.aspxhome.com/file/2023/3/59163_0s.png)
轻松学习C#的装箱与拆箱
2021-07-01 12:11:51
java日期操作工具类(获取指定日期、日期转换、相隔天数)
2023-11-28 06:42:53
winform 实现选择文件和选择文件夹对话框的简单实例
2022-02-21 07:05:12
详解SpringBoot程序启动时执行初始化代码
2022-05-07 13:36:05
高可用架构etcd选主故障主备秒级切换实现
2022-08-08 23:40:48