经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Fluent Mybatis让你摆脱Xml文件的技巧
来源:jb51  时间:2021/8/4 17:56:00  对本文有异议

一、啥是Fluent-Mybatis

与Mybatis-Plus类似,是对Mybaits进一步的封装,使之语法简洁明了,更重要的是不需要在自主创建Xml文件,可以只用一个实体类对象,通过代码生成器,在编译的过程中生成所需要的各类文件,简化了项目的基础构建,提高开发效率。

在这里插入图片描述

二、SpringBoot + Fluent-Mybatis

1、创建数据库测试表

  1. DROP TABLE IF EXISTS `t_user`;
  2. create table `t_user`
  3. (
  4. id bigint auto_increment comment '主键ID' primary key,
  5. name varchar(30) charset utf8 null comment '姓名',
  6. age int null comment '年龄',
  7. email varchar(50) charset utf8 null comment '邮箱',
  8. gmt_create datetime null comment '记录创建时间',
  9. gmt_modified datetime null comment '记录最后修改时间',
  10. is_deleted tinyint(2) default 0 null comment '逻辑删除标识'
  11. );

2、创建一个Springboot项目,pom文件如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.mybatis.fluent</groupId>
  12. <artifactId>fluent_mybatis</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>fluent_mybatis</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <fluent.mybatis.version>1.5.6</fluent.mybatis.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter</artifactId>
  24. </dependency>
  25. <!--JDBC驱动-->
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. <scope>runtime</scope>
  30. </dependency>
  31. <!--Mybatis-->
  32. <dependency>
  33. <groupId>org.mybatis.spring.boot</groupId>
  34. <artifactId>mybatis-spring-boot-starter</artifactId>
  35. <version>2.0.1</version>
  36. </dependency>
  37. <!-- fluent mybatis依赖-->
  38. <dependency>
  39. <groupId>com.github.atool</groupId>
  40. <artifactId>fluent-mybatis</artifactId>
  41. <version>${fluent.mybatis.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.github.atool</groupId>
  45. <artifactId>fluent-mybatis-processor</artifactId>
  46. <version>${fluent.mybatis.version}</version>
  47. <scope>provided</scope>
  48. </dependency>
  49.  
  50. <dependency>
  51. <groupId>org.projectlombok</groupId>
  52. <artifactId>lombok</artifactId>
  53. <optional>true</optional>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-test</artifactId>
  58. <scope>test</scope>
  59. <exclusions>
  60. <exclusion>
  61. <groupId>org.junit.vintage</groupId>
  62. <artifactId>junit-vintage-engine</artifactId>
  63. </exclusion>
  64. </exclusions>
  65. </dependency>
  66. </dependencies>
  67.  
  68. <build>
  69. <plugins>
  70. <plugin>
  71. <groupId>org.springframework.boot</groupId>
  72. <artifactId>spring-boot-maven-plugin</artifactId>
  73. <configuration>
  74. <excludes>
  75. <exclude>
  76. <groupId>org.projectlombok</groupId>
  77. <artifactId>lombok</artifactId>
  78. </exclude>
  79. </excludes>
  80. </configuration>
  81. </plugin>
  82. </plugins>
  83. </build>
  84.  
  85. </project>

3、代码生成器

  1. import cn.org.atool.generator.FileGenerator;
  2. import cn.org.atool.generator.annotation.Table;
  3. import cn.org.atool.generator.annotation.Tables;
  4.  
  5. public class AppEntityGenerator {
  6.  
  7. public static void main(String[] args) {
  8. FileGenerator.build(Abc.class);
  9. }
  10.  
  11. @Tables(
  12. /** 数据库连接信息 **/
  13. url = "jdbc:mysql://IP:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
  14. username = "XXXXXX",
  15. password = "XXXXXX",
  16. /** Entity类parent package路径 **/
  17. basePack = "com.mybatis.fluent.fluent_mybatis",
  18. /** Entity代码源目录 **/
  19. srcDir = "/src/main/java",
  20. /** Dao代码源目录 **/
  21. daoDir = "/src/main/java",
  22. /** 如果表定义记录创建,记录修改,逻辑删除字段 **/
  23. gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
  24. /** 需要生成文件的表 **/
  25. tables = @Table(value = {"t_user"})
  26. )
  27. static class Abc {
  28.  
  29. }
  30. }

需要注意,默认的是MySQL数据库,如果需要其他数据库,需要手动配置dbType和driver,其他选项按照需要修改。例如oralce

  1. import cn.org.atool.generator.FileGenerator;
  2. import cn.org.atool.generator.annotation.Table;
  3. import cn.org.atool.generator.annotation.Tables;
  4. import cn.org.atool.generator.database.DbType;
  5.  
  6. public class AppEntityGenerator {
  7.  
  8. public static void main(String[] args) {
  9. FileGenerator.build(Abc.class);
  10. }
  11.  
  12. @Tables(
  13. /** 数据库连接信息 **/
  14. dbType= DbType.ORACLE,
  15. driver= "oracle.jdbc.OracleDriver",
  16. url = "jdbc:oracle:thin:@IP:1521:orcl",
  17. username = "XXXXXX",
  18. password = "XXXXXX",
  19. /** Entity类parent package路径 **/
  20. basePack = "com.mybatis.fluent.fluent_mybatis",
  21. /** Entity代码源目录 **/
  22. srcDir = "/src/main/java",
  23. /** Dao代码源目录 **/
  24. daoDir = "/src/main/java",
  25. /** 如果表定义记录创建,记录修改,逻辑删除字段 **/
  26. gmtCreated = "INSERT_DATE_TIME",
  27. /** 需要生成文件的表 **/
  28. tables = @Table(value = {"table_name"})
  29. )
  30. static class Abc {
  31.  
  32. }
  33. }

main方法启动执行,生成的项目结构,如果在执行是出现异常

在这里插入图片描述

需要暂时注释

在这里插入图片描述

当生成好对应文件,项目目录如下

在这里插入图片描述

此时TUserDaoImpl会有错误

在这里插入图片描述

此时将需要将之前注释的provided打开,在执行

在这里插入图片描述

执行过后,异常消除。此时你就拥有的对数据库此表的强大操作能力。

4、测试CURD

  1. import cn.org.atool.fluent.mybatis.model.IPagedList;
  2. import cn.org.atool.fluent.mybatis.model.StdPagedList;
  3. import com.mybatis.fluent.fluent_mybatis.dao.impl.HospitalinfoDaoImpl;
  4. import com.mybatis.fluent.fluent_mybatis.dao.impl.TUserDaoImpl;
  5. import com.mybatis.fluent.fluent_mybatis.entity.HospitalinfoEntity;
  6. import com.mybatis.fluent.fluent_mybatis.entity.TUserEntity;
  7. import com.mybatis.fluent.fluent_mybatis.helper.TUserWrapperHelper;
  8. import com.mybatis.fluent.fluent_mybatis.wrapper.HospitalinfoQuery;
  9. import com.mybatis.fluent.fluent_mybatis.wrapper.TUserQuery;
  10. import org.junit.jupiter.api.Test;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12.  
  13. import javax.annotation.Resource;
  14. import java.io.Serializable;
  15. import java.util.List;
  16.  
  17. @SpringBootTest
  18. class FluentMybatisApplicationTests {
  19.  
  20. @Resource
  21. private TUserDaoImpl dao;
  22.  
  23. @Test
  24. void add() {
  25. TUserEntity entity = new TUserEntity().setAge(20).setEmail("122@163.com").setName("lisi").setIsDeleted(false);
  26. Serializable id = dao.save(entity);
  27. System.out.println("插入后返回的主键ID为:" + id);
  28. }
  29.  
  30. @Test
  31. void select(){
  32. /**
  33. * 分页查询构造条件
  34. */
  35. TUserQuery query = TUserQuery.query().limit(0,1);
  36. List<TUserEntity> list = dao.listEntity(query);
  37. System.out.println(list);
  38. }
  39.  
  40. @Test
  41. void upData(){
  42. TUserEntity entity = dao.selectById(1L);
  43. System.out.println(entity);
  44. entity.setAge(2000).setEmail("120098922@qq.com").setName("lisi111").setIsDeleted(true);
  45. System.out.println(entity);
  46. boolean b = dao.updateById(entity);
  47. System.out.println(b);
  48. }
  49.  
  50. @Test
  51. void delete(){
  52. boolean b = dao.deleteById(1L);
  53. System.out.println(b);
  54. TUserQuery query = TUserQuery.query();
  55. List<TUserEntity> list = dao.listEntity(query);
  56. System.out.println(list);
  57. }
  58.  
  59. }

三、官方链接

官方文档

官方网站提供了完整切详细的构建和使用的文档,本文的内容仅为学习练习的Demo

到此这篇关于Fluent Mybatis让你摆脱Xml文件的技巧的文章就介绍到这了,更多相关Fluent Mybatis Xml文件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号