Fluent Mybatis让你摆脱Xml文件的技巧
作者:樱花、散落的季节╮ 发布时间:2023-10-02 18:31:00
标签:Fluent,Mybatis,Xml
一、啥是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
来源:https://blog.csdn.net/qq_45624560/article/details/117692600


猜你喜欢
- 目录前言常量池反编译代码验证字符串初始化操作总结前言在深入学习字符串类之前,我们先搞懂JVM是怎样处理新生字符串的。当你知道字符串的初始化细
- jdk安装有两种方式:第一种是使用yum命令一键安装,默认安装目录在/usr/lib/jvm第二种是手动安装,须去oracle官网下载jdk
- android开机自动运行APP实现方式其实很简单。在android系统运行时,会发出“android.intent.action.BOOT
- 近日学习Easyui,发现非常好用,界面很美观。将学习的心得在此写下,这篇博客
- Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。自从5.0开始,jdk在java.util.concurrent
- 第一种就是 最常见的 用Try..Catch..再try中强转你要确认的string 类型成功就是int catch 就不是&n
- 1. 确保你项目能编译通过,安装java jdk 环境填写环境变量2. 添加SpringBootServletInitializer的子类重
- 简介现在市面上的apk只要涉及用户中心都会有头像,而且这个头像也是可自定义的,有的会采取读取相册选择其中一张作为需求照片,另一种就是调用系统
- 引言java中的Math.random()是一个在[0,1)范围等概率返回double数值类型的算法,基于此函数,我们来延申一些随机概率算法
- 项目背景在做项目的时候,把SpringBoot的项目打包成安装包了,在客户上面安装运行,一切都是那么的完美,可是发生了意外,对方突然说导出导
- Android 登录处理简单实例今天整理一下之前在项目中写的关于某些界面需要登录判断处理。这里整理了一个简易的 Demo 模拟一下 登录情况
- utf-8转unicode public static String utf8ToUnicode(String inStr) {  
- 1、数据访问计数器 在Spring Boot项目中,有时需要数据访问计数器。大致有下列三种情形:1)纯计数:如登录的密码错误计数,超过门限
- 这篇文章主要介绍了springboot自定义异常视图过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 这篇文章主要介绍了spring boot如何实现切割分片上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 淘宝返回的数据为:{"code":0,"data":{"country":&qu
- 本文实例为大家分享了C#实现拼手气红包算法的具体代码,供大家参考,具体内容如下一、方案1:即开即中,考虑机会均等,减少金额差较大的几率可以每
- 简介OCSP在线证书状态协议是为了替换CRL而提出来的。对于现代web服务器来说一般都是支持OCSP的,OCSP也是现代web服务器的标配。
- 表示 Windows 注册表中的项级节点。 此类是注册表封装。继承层次结构System.Object &nb
- 一、Filter(过滤器)Filter接口定义在javax.servlet包中,是Servlet规范定义的,作用于Request/Respo