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",
basePack = "com.mybatis.fluent.fluent_mybatis",
class="lazy" data-srcDir = "/class="lazy" data-src/main/java",
daoDir = "/class="lazy" data-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",
basePack = "com.mybatis.fluent.fluent_mybatis",
class="lazy" data-srcDir = "/class="lazy" data-src/main/java",
daoDir = "/class="lazy" data-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
到此这篇关于Fluent Mybatis让你摆脱Xml文件的技巧的文章就介绍到这了,更多相关Fluent Mybatis Xml文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341