FluentMybatis实现mybatis动态sql拼装和fluent api语法
开始第一个例子: hello world
新建java工程,设置maven依赖
新建maven工程,设置项目编译级别为java8及以上,引入fluent mybatis依赖包。
<dependencies> <!-- 引入fluent-mybatis 运行依赖包, scope为compile --> <dependency> <groupid>com.github.atool</groupid> <artifactid>fluent-mybatis</artifactid> <version>1.3.1</version> </dependency> <!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 --> <dependency> <groupid>com.github.atool</groupid> <artifactid>fluent-mybatis-processor</artifactid> <version>1.3.1</version> </dependency> </dependencies>
新建演示用的数据库结构
create schema fluent_mybatis_tutorial; create table hello_world ( id bigint unsigned auto_increment primary key, say_hello varchar(100) null, your_name varchar(100) null, gmt_create datetime default null comment '创建时间', gmt_modified datetime default null comment '更新时间', is_deleted tinyint(2) default 0 comment '是否逻辑删除' ) engine = innodb character set = utf8 comment '简单演示表';
创建数据库表对应的entity类
创建数据库表对应的entity类: helloworldentity, 你只需要简单的做3个动作:
- 根据驼峰命名规则命名entity类和字段
- helloworldentity继承ientity接口类
- 在helloworldentity类上加注解 @fluentmybatis
@fluentmybatis public class helloworldentity implements ientity { private long id; private string sayhello; private string yourname; private date gmtcreate; private date gmtmodified; private boolean isdeleted; // get, set, tostring 方法 }
很简单吧,在这里,你即不需要配置任何mybatis xml文件, 也不需要写任何mapper接口, 但你已经拥有了强大的增删改查的功能,并且是fluent api,让我们写一个测试来见证一下fluent mybatis的魔法力量!
运行测试来见证fluent mybatis的神奇
为了运行测试, 我们还需要进行junit和spring test相关配置。
配置spring bean定义
数据源datasource配置
mybatis的mapper扫描路径
mybatis的sqlsessionfactorybean
@componentscan(basepackages = "cn.org.atool.fluent.mybatis.demo1") @mapperscan("cn.org.atool.fluent.mybatis.demo1.entity.mapper") @configuration public class helloworldconfig { /** * 设置datasource属性 * * @return */ @bean public datasource datasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://localhost:3306/fluent_mybatis_tutorial?useunicode=true&characterencoding=utf8"); datasource.setusername("root"); datasource.setpassword("password"); return datasource; } /** * 定义mybatis的sqlsessionfactorybean * * @param datasource * @return */ @bean public sqlsessionfactorybean sqlsessionfactorybean(datasource datasource) { sqlsessionfactorybean bean = new sqlsessionfactorybean(); bean.setdatasource(datasource); return bean; } }
使用junit4和spring-test来执行测试
- 使用spring-test初始化spring容器
- 注入helloworldentity对应的mapper类: helloworldmapper, 这个类是fluent mybatis编译时生成的。
- 使用helloworldmapper进行删除、插入、查询、修改操作。
@runwith(springjunit4classrunner.class) @contextconfiguration(classes = helloworldconfig.class) public class helloworldtest { /** * fluent mybatis编译时生成的mapper类 */ @autowired helloworldmapper mapper; @test public void testhelloworld() { /** * 为了演示方便,先删除数据 */ mapper.delete(mapper.query() .where.id().eq(1l).end()); /** * 插入数据 */ helloworldentity entity = new helloworldentity(); entity.setid(1l); entity.setsayhello("hello world"); entity.setyourname("fluent mybatis"); entity.setisdeleted(false); mapper.insert(entity); /** * 查询 id = 1 的数据 */ helloworldentity result1 = mapper.findone(mapper.query() .where.id().eq(1l).end()); /** * 控制台直接打印出查询结果 */ system.out.println("1. helloworldentity:" + result1.tostring()); /** * 更新id = 1的记录 */ mapper.updateby(mapper.updater() .update.sayhello().is("say hello, say hello!") .set.yourname().is("fluent mybatis is powerful!").end() .where.id().eq(1l).end() ); /** * 查询 id = 1 的数据 */ helloworldentity result2 = mapper.findone(mapper.query() .where.sayhello().like("hello") .and.isdeleted().eq(false).end() .limit(1) ); /** * 控制台直接打印出查询结果 */ system.out.println("2. helloworldentity:" + result2.tostring()); } }
执行junit4测试方法,控制台输出
1. helloworldentity:helloworldentity{id=1, sayhello='hello world', yourname='fluent mybatis', gmtcreate=null, gmtmodified=null, isdeleted=false}
2. helloworldentity:helloworldentity{id=1, sayhello='say hello, say hello!', yourname='fluent mybatis is powerful!', gmtcreate=null, gmtmodified=null, isdeleted=false}
神奇吧! 我们再到数据库中查看一下结果
现在,我们已经通过一个简单例子演示了fluent mybatis的强大功能,
在进一步介绍fluent mybatis更强大功能前,我们揭示一下为啥我们只写了一个数据表对应的entity类,
却拥有了一系列增删改查的数据库操作方法。
fluent mybatis根据entity类上@fluentmybatis注解在编译时,
会在target目录class目录下自动编译生成一系列文件:
核心接口类, 使用时需要了解
- mapper/*mapper: mybatis的mapper定义接口, 定义了一系列通用的数据操作接口方法。
- dao/*basedao: dao实现基类, 所有的daoimpl都继承各自基类
- 根据分层编码的原则,我们不会在service类中直接使用mapper类,而是引用dao类。我们在dao实现类中根据条件实现具体的数据操作方法。
- wrapper/*query: fluent mybatis核心类, 用来进行动态sql的构造, 进行条件查询。
- wrapper/*updater: fluent mybatis核心类, 用来动态构造update语句。
- entity/*entityhelper: entity帮助类, 实现了entity和map的转换方法
- 辅助实现时, 实现fluent mybatis动态sql拼装和fluent api时内部用到的类,使用时无需了解
- 在使用上,我们主要会接触到上述5个生成的java类。fluent mybatis为了实现动态拼接和fluent api功能,还生成了一系列辅助类。
- helper/*mapping: 表字段和entity属性映射定义类
- helper/*sqlproviderp: mapper接口动态sql提供者
- helper/*wrapperhelper: query和updater具体功能实现, 包含几个实现:select, where, group by, having by, order by, limit
到此这篇关于fluentmybatis实现mybatis动态sql拼装和fluent api语法的文章就介绍到这了,更多相关fluentmybatis实现mybatis动态sql内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!