写在前面:此文章为本人学习过程,路过大神不喜勿喷
一、啥是Fluent-Mybatis
与Mybatis-Plus类似,是对Mybaits进一步的封装,使之语法简洁明了,更重要的是不需要在自主创建Xml文件,可以只用一个实体类对象,通过代码生成器,在编译的过程中生成所需要的各类文件,简化了项目的基础构建,提高开发效率。
二、SpringBoot + Fluent-Mybatis
1、创建数据库测试表
DROP TABLE IF EXISTS `t_user`;
create table `t_user`
(
id bigint auto_increment comment '主键ID' primary key,
name varchar(30) charset utf8 null comment '姓名',
age int null comment '年龄',
email varchar(50) charset utf8 null comment '邮箱',
gmt_create datetime null comment '记录创建时间',
gmt_modified datetime null comment '记录最后修改时间',
is_deleted tinyint(2) default 0 null comment '逻辑删除标识'
);
2、创建一个Springboot项目,pom文件如下
<?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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mybatis.fluent</groupId>
<artifactId>fluent_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fluent_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<fluent.mybatis.version>1.5.6</fluent.mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--JDBC驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- fluent mybatis依赖-->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis</artifactId>
<version>${fluent.mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis-processor</artifactId>
<version>${fluent.mybatis.version}</version>
<scope>provided</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
3、代码生成器
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
public class AppEntityGenerator {
public static void main(String[] args) {
FileGenerator.build(Abc.class);
}
@Tables(
/** 数据库连接信息 **/
url = "jdbc:mysql://IP:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
username = "XXXXXX",
password = "XXXXXX",
/** Entity类parent package路径 **/
basePack = "com.mybatis.fluent.fluent_mybatis",
/** Entity代码源目录 **/
srcDir = "/src/main/java",
/** Dao代码源目录 **/
daoDir = "/src/main/java",
/** 如果表定义记录创建,记录修改,逻辑删除字段 **/
gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
/** 需要生成文件的表 **/
tables = @Table(value = {"t_user"})
)
static class Abc {
}
}
需要注意,默认的是MySQL数据库,如果需要其他数据库,需要手动配置dbType和driver,其他选项按照需要修改。例如oralce
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import cn.org.atool.generator.database.DbType;
public class AppEntityGenerator {
public static void main(String[] args) {
FileGenerator.build(Abc.class);
}
@Tables(
/** 数据库连接信息 **/
dbType= DbType.ORACLE,
driver= "oracle.jdbc.OracleDriver",
url = "jdbc:oracle:thin:@IP:1521:orcl",
username = "XXXXXX",
password = "XXXXXX",
/** Entity类parent package路径 **/
basePack = "com.mybatis.fluent.fluent_mybatis",
/** Entity代码源目录 **/
srcDir = "/src/main/java",
/** Dao代码源目录 **/
daoDir = "/src/main/java",
/** 如果表定义记录创建,记录修改,逻辑删除字段 **/
gmtCreated = "INSERT_DATE_TIME",
/** 需要生成文件的表 **/
tables = @Table(value = {"table_name"})
)
static class Abc {
}
}
main方法启动执行,生成的项目结构,如果在执行是出现异常
需要暂时注释
当生成好对应文件,项目目录如下
此时TUserDaoImpl会有错误
此时将需要将之前注释的provided打开,在执行
执行过后,异常消除。此时你就拥有的对数据库此表的强大操作能力。
4、测试CURD
import cn.org.atool.fluent.mybatis.model.IPagedList;
import cn.org.atool.fluent.mybatis.model.StdPagedList;
import com.mybatis.fluent.fluent_mybatis.dao.impl.HospitalinfoDaoImpl;
import com.mybatis.fluent.fluent_mybatis.dao.impl.TUserDaoImpl;
import com.mybatis.fluent.fluent_mybatis.entity.HospitalinfoEntity;
import com.mybatis.fluent.fluent_mybatis.entity.TUserEntity;
import com.mybatis.fluent.fluent_mybatis.helper.TUserWrapperHelper;
import com.mybatis.fluent.fluent_mybatis.wrapper.HospitalinfoQuery;
import com.mybatis.fluent.fluent_mybatis.wrapper.TUserQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
@SpringBootTest
class FluentMybatisApplicationTests {
@Resource
private TUserDaoImpl dao;
@Test
void add() {
TUserEntity entity = new TUserEntity().setAge(20).setEmail("122@163.com").setName("lisi").setIsDeleted(false);
Serializable id = dao.save(entity);
System.out.println("插入后返回的主键ID为:" + id);
}
@Test
void select(){
/**
* 分页查询构造条件
*/
TUserQuery query = TUserQuery.query().limit(0,1);
List<TUserEntity> list = dao.listEntity(query);
System.out.println(list);
}
@Test
void upData(){
TUserEntity entity = dao.selectById(1L);
System.out.println(entity);
entity.setAge(2000).setEmail("120098922@qq.com").setName("lisi111").setIsDeleted(true);
System.out.println(entity);
boolean b = dao.updateById(entity);
System.out.println(b);
}
@Test
void delete(){
boolean b = dao.deleteById(1L);
System.out.println(b);
TUserQuery query = TUserQuery.query();
List<TUserEntity> list = dao.listEntity(query);
System.out.println(list);
}
}
三、官方链接
官方网站提供了完整切详细的构建和使用的文档,本文的内容仅为学习练习的Demo
最后的最后:希望文章可以帮到大家,路过大神不喜勿喷,我们共同学习,加油吧!!!!