同步操作将从 fluent-mybatis/fluent-mybatis-docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。 国内又以Mybatis用的多,基于mybatis上的增强框架,又有mybatis plus和TK mybatis等。 今天我们介绍一个新的mybatis增强框架 fluent mybatis, 那既然JDBC --> Mybatis或Mybatis Plus无疑简化了开发者的工作,而今天我们所讲的 Fluent MyBatis又起到什么作用呢?
Fluent MyBatis是一个 MyBatis 的增强工具,他只做了mybatis的语法糖封装,没有对mybatis做任何修改。 通过编译手段,提供了一系列辅助类来帮助开发简化开发、提高效率。
创建一个示例的数据库表
DROP TABLE IF EXISTS `your_table`;
create table `your_table`
(
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 '逻辑删除标识'
);
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.url=jdbc:mysql://localhost:3306/fluent_mybatis_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
这里直接使用fluent mybatis提供的工具类生成代码
public class AppEntityGenerator {
static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8";
public static void main(String[] args) {
FileGenerator.build(Abc.class);
}
@Tables(
/** 数据库连接信息 **/
url = url, username = "root", password = "password",
/** Entity类parent package路径 **/
basePack = "cn.org.fluent.mybatis.springboot.demo",
/** Entity代码源目录 **/
srcDir = "spring-boot-demo/src/main/java",
/** Dao代码源目录 **/
daoDir = "spring-boot-demo/src/main/java",
/** 如果表定义记录创建,记录修改,逻辑删除字段 **/
gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
/** 需要生成文件的表 **/
tables = @Table(value = {"your_table"})
)
static class Abc {
}
}
这里有3个特殊字段
执行生成代码main函数, 在工程main/src/java目录下产出 Entity, DaoIntf, DaoImpl文件; 观察YourEntity的主键 id, gmt_create, gmt_modified, is_deleted这几个字段的注解
@Data
@Accessors(chain = true)
@FluentMybatis(table = "your_table")
public class YourEntity implements IEntity{
private static final long serialVersionUID = 1L;
@TableId(value = "id")
private Long id;
@TableField(value = "gmt_create", insert = "now()")
private Date gmtCreate;
@TableField(value = "gmt_modified", insert = "now()", update = "now()")
private Date gmtModified;
@TableField(value = "is_deleted", insert = "0")
private Boolean isDeleted;
@TableField(value = "age")
private Integer age;
@TableField(value = "email")
private String email;
@TableField(value = "name")
private String name;
@Override
public Serializable findPk() {
return id;
}
}
生成的Dao文件,引用到了YourTableBaseDao类,这个类需要根据Entity类编译生成,在重新编译前会有编译错误,所以生成代码后需要重新Rebuild下
@Repository
public class YourDaoImpl extends YourBaseDao implements YourDao {
// 在这里添加你自己的业务逻辑代码
}
碰到上面这种编译错误,不要慌张,编译工程即可!
在Rebuild后,会在target目录下就会多出几个文件, 重新刷新一下工程把target/generated-sources加到源目录上即可。
这时工程已经具备fluent mybatis强大的增删改查功能了。我们创建一个测试类来验证一下,在测试类中注入 YourMapper,这里演示一个查询所有的方法,所以使用了 listEntity ,其参数是一个Query对象。
@SpringBootTest(classes = QuickStartApplication.class)
public class FluentMybatisApplicationTest {
@Autowired
private YourMapper yourMapper;
@Test
void contextLoads() {
List<YourEntity> list = yourMapper.listEntity(yourMapper.query());
for (YourEntity entity : list) {
System.out.println(entity);
}
}
}
你可以手工往数据库中插入几条记录,验证一下效果。
下面我们分别介绍FluentMybatis提供的insert, select, update和delete方法,内容的介绍基本按4部分解析
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。