主要演示DynamicSql风格代码如何使用,基本能应对大部分使用场景。DynamicSql基本介绍点我查看。
本文主要沿着增、删、改、查的思路进行介绍,尽量涵盖日常使用所需。
我这里还是要推荐一下大家看官方文档,尽量有问题先找官方文档教程,除非写的跟屎一样,但大概率不会。
本次使用的是mybatis-dynamic-sql1.2.1版本
<!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!-- MyBatis 生成器 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <!-- MyBatis 动态SQL支持 --> <dependency> <groupId>org.mybatis.dynamic-sql</groupId> <artifactId>mybatis-dynamic-sql</artifactId> <version>1.2.1</version> </dependency>
查询我尽量贴上SQL语句对照着java代码,方便读者阅读和理解。
而且基本都实际运行过,确保没有问题。
SELECT id,label,value FROM sys_dict
import static com.twj.spirngbasics.server.mapper.SysDictDynamicSqlSupport.*; //注意导入对应DynamicSqlSupport包的静态属性 SelectStatementProvider selectStatement = SqlBuilder.select(id, label, value) .from(sysDict) .build() .render(RenderingStrategies.MYBATIS3); List<SysDict> test = sysDictMapper.selectMany(selectStatement);
下面完全等价于上面代码,推荐上方写法,代码更整洁。
SelectStatementProvider selectStatement = SqlBuilder.select(SysDictDynamicSqlSupport.id, SysDictDynamicSqlSupport.label, SysDictDynamicSqlSupport.value) .from(SysDictDynamicSqlSupport.sysDict) .build() .render(RenderingStrategies.MYBATIS3); List<SysDict> list = sysDictMapper.selectMany(selectStatement);
可以看到DynamicSql的使用结构完全与sql语句一样,真香。
SELECT id,label,value,sort....... FROM sys_dict
SelectStatementProvider selectStatement = SqlBuilder.select(SysDictMapper.selectList) .from(SysDictDynamicSqlSupport.sysDict) .build() .render(RenderingStrategies.MYBATIS3); List<SysDict> list = sysDictMapper.selectMany(selectStatement);
SELECT * FROM sys_dict WHERE label = '男' OR label = '女' ORDER BY `value` ASC
SelectStatementProvider selectStatement = SqlBuilder.select(SysDictMapper.selectList) .from(SysDictDynamicSqlSupport.sysDict) .where(label, isEqualTo("男")) .or(label,isEqualTo("女")) .orderBy(value) .build() .render(RenderingStrategies.MYBATIS3); List<SysDict> list = sysDictMapper.selectMany(selectStatement);
java这里稍微注意一下,isEqualTo的包引用路径是在org.mybatis.dynamic.sql.SqlBuilder包下,可以像之前一样import static org.mybatis.dynamic.sql.SqlBuilder.*;引入所有静态方法。
排序:
SELECT * FROM sys_dict WHERE label IN ( '女', '男' ) ORDER BY `value`
SelectStatementProvider selectStatement = SqlBuilder.select(SysDictMapper.selectList) .from(SysDictDynamicSqlSupport.sysDict) .where(label, isIn("女", "男")) .orderBy(value) .build() .render(RenderingStrategies.MYBATIS3); List<SysDict> list = sysDictMapper.selectMany(selectStatement);
where条件查询还有很多我就不一一例举了,我这里有一张官方偷来的表格:
Condition | Example | Result |
---|---|---|
Between | where(foo, isBetween(x).and(y)) | where foo between ? and ? |
Equals | where(foo, isEqualTo(x)) | where foo = ? |
Greater Than | where(foo, isGreaterThan(x)) | where foo > ? |
Greater Than or Equals | where(foo, isGreaterThanOrEqualTo(x)) | where foo >= ? |
In | where(foo, isIn(x, y)) | where foo in (?,?) |
In (case insensitive) | where(foo, isInCaseInsensitive(x, y)) | where upper(foo) in (?,?) (the framework will transform the values for x and y to upper case) |
Less Than | where(foo, isLessThan(x)) | where foo < ? |
Less Than or Equals | where(foo, isLessThanOrEqualTo(x)) | where foo <= ? |
Like | where(foo, isLike(x)) | where foo like ? (the framework DOES NOT add the SQL wild cards to the value - you will need to do that yourself) |
Like (case insensitive) | where(foo, isLikeCaseInsensitive(x)) | where upper(foo) like ? (the framework DOES NOT add the SQL wild cards to the value - you will need to do that yourself, the framework will transform the value of x to upper case) |
Not Between | where(foo, isNotBetween(x).and(y)) | where foo not between ? and ? |
Not Equals | where(foo, isNotEqualTo(x)) | where foo <> ? |
Not In | where(foo, isNotIn(x, y)) | where foo not in (?,?) |
Not In (case insensitive) | where(foo, isNotInCaseInsensitive(x, y)) | where upper(foo) not in (?,?) (the framework will transform the values for x and y to upper case) |
Not Like | where(foo, isLike(x)) | where foo not like ? (the framework DOES NOT add the SQL wild cards to the value - you will need to do that yourself) |
Not Like (case insensitive) | where(foo, isNotLikeCaseInsensitive(x)) | where upper(foo) not like ? (the framework DOES NOT add the SQL wild cards to the value - you will need to do that yourself, the framework will transform the value of x to upper case) |
Not Null | where(foo, isNotNull()) | where foo is not null |
Null | where(foo, isNull()) | where foo is null |
SELECT * FROM user_resource WHERE id IN ( SELECT resource_id FROM user_role_resource WHERE role_id = '1' )
SelectStatementProvider selectStatement = SqlBuilder.select(userResourceMapper.selectList) .from(UserResourceDynamicSqlSupport.userResource) .where(UserResourceDynamicSqlSupport.id, isIn( select(UserRoleResourceDynamicSqlSupport.resourceId) .from(UserRoleResourceDynamicSqlSupport.userRoleResource) .where(UserRoleResourceDynamicSqlSupport.roleId, isEqualTo("1")))) .build() .render(RenderingStrategies.MYBATIS3); List<UserResource> list = userResourceMapper.selectMany(selectStatement);
子查询还有很多,我这里又有一张官方偷来的表格:
Condition | Example | Result |
---|---|---|
Equals | where(foo, isEqualTo(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo = (select bar from table2 where bar = ?) |
Greater Than | where(foo, isGreaterThan(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo > (select bar from table2 where bar = ?) |
Greater Than or Equals | where(foo, isGreaterThanOrEqualTo(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo >= (select bar from table2 where bar = ?) |
In | where(foo, isIn(select(bar).from(table2).where(bar, isLessThan(x))) | where foo in (select bar from table2 where bar < ?) |
Less Than | where(foo, isLessThan(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo < (select bar from table2 where bar = ?) |
Less Than or Equals | where(foo, isLessThanOrEqualTo(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo <= (select bar from table2 where bar = ?) |
Not Equals | where(foo, isNotEqualTo(select(bar).from(table2).where(bar, isEqualTo(x))) | where foo <> (select bar from table2 where bar = ?) |
Not In | where(foo, isNotIn(select(bar).from(table2).where(bar, isLessThan(x))) | where foo not in (select bar from table2 where bar < ?) |
详细看代码
QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder builder = SqlBuilder.select(SysDictMapper.selectList) .from(SysDictDynamicSqlSupport.sysDict) .where(); if (x) builder.where(label, isIn("女", "男")); if (y) builder.where(row,...); SelectStatementProvider selectStatement = builder.build().render(RenderingStrategies.MYBATIS3); List<SysDict> list = sysDictMapper.selectMany(selectStatement);
有前面的基础,连接查询其实异曲同工,我这里直接贴上官方示例代码:
SelectStatementProvider selectStatement = select(orderMaster.orderId, orderDate, orderDetail.lineNumber, orderDetail.description, orderDetail.quantity) .from(orderMaster, "om") .join(orderDetail, "od").on(orderMaster.orderId, equalTo(orderDetail.orderId)) .build() .render(RenderingStrategies.MYBATIS3);
目前支持四种连接类型:
新增这里就不附上SQL语句了
SysDict sysDict = new SysDict(); sysDict.setLabel("测试"); sysDict.setValue("0"); sysDict.setType("test"); sysDict.setSort(0); sysDict.setDescription("测试"); sysDict.insert("SYSTEM"); int row = sysDictMapper.insert(sysDict); System.out.println("成功插入条数:" + row);
List<SysDict> list = new ArrayList<>(); for (int i = 1; i < 10; i++) { SysDict sysDict = new SysDict(); sysDict.setLabel("测试"); sysDict.setValue(String.valueOf(i)); sysDict.setType("test"); sysDict.setSort(i); sysDict.setDescription("测试"); sysDict.insert("SYSTEM"); list.add(sysDict); } MultiRowInsertStatementProvider<SysDict> multiRowInsert = SqlBuilder.insertMultiple(list) .into(SysDictDynamicSqlSupport.sysDict) .map(id).toProperty("id") .map(createdBy).toProperty("createdBy") .map(createdTime).toProperty("createdTime") .map(updateBy).toProperty("updateBy") .map(updateTime).toProperty("updateTime") .map(dele).toProperty("dele") .map(remake).toProperty("remake") .map(spare1).toProperty("spare1") .map(value).toProperty("value") .map(label).toProperty("label") .map(type).toProperty("type") .map(description).toProperty("description") .map(sort).toProperty("sort") .build() .render(RenderingStrategies.MYBATIS3); int rows = sysDictMapper.insertMultiple(multiRowInsert); System.out.println("成功插入条数:" + rows);
批量新增这里需要注意的是map的添加,也可以不加,但我在使用过程中出现过不加map导致批量新增出现某些必填字段明明赋值了数据库却报没有不能为空,猜测应该是转换成sql语句时into与value没有一一对应,加上map就没问题了。
PS:.map可以直接从xxxDictMapper.insert()中copy过来。
//根据主键删除 sysDictMapper.deleteByPrimaryKey(""); //条件删除 DeleteStatementProvider deleteStatement = deleteFrom(SysDictDynamicSqlSupport.sysDict) .where(SysDictDynamicSqlSupport.type, isEqualTo("test")) .build() .render(RenderingStrategies.MYBATIS3); sysDictMapper.delete(deleteStatement);
常用的简单更新主要是下面两种:
//根据主键对所有属性进行更新 sysDictMapper.updateByPrimaryKey(sysDict); //根据主键对不为null的属性进行更新 sysDictMapper.updateByPrimaryKeySelective(sysDict);
复杂一点点的:
UpdateStatementProvider updateStatement = update(SysDictDynamicSqlSupport.sysDict) .set(remake).equalToNull() .where(type, isEqualTo("test")) .build() .render(RenderingStrategies.MYBATIS3); int rows = sysDictMapper.update(updateStatement); System.out.println("成功更新条数:" + rows);
注意set方法,常用的方法有以下:
到此这篇关于浅谈MyBatis3 DynamicSql风格语法使用指南的文章就介绍到这了,更多相关MyBatis3 DynamicSql风格内容请搜索小牛知识库以前的文章或继续浏览下面的相关文章希望大家以后多多支持小牛知识库!
你可能已经想知道如何将你放在*.vue文件中的代码检查风格了,尽管它们不是JavaScript。我们将假设你正在使用ESLint(如果你还没用,那你应该用了!)。 你还需要[eslint-plugin-html](https://github.com/BenoitZugmeyer/eslint-plugin-html),它支持在 *.vue 文件中提取和删除JavaScript。 确保在ESLin
介绍 在参与 Vant 开发时,请遵守约定的单文件组件风格指南,指南内容节选自 Vue 官方风格指南。 组件数据 组件的 data 必须是一个函数。 // bad export default { data: { foo: 'bar', }, }; // good export default { data() { return { foo: 'bar',
这是CatLib特有的代码风格指南,如果您在您的项目中使用CatLib,为了避免错误,降低沟通成本,小纠结和 反模式,阅读本指南是一份不错的选择。 我们不能保证风格指南中的所有内容,对于所有工程和团队都是理想的,所以根据项目环境,周围技术环境,风格出现偏差是可行的。 我们应该尽可能的遵守本风格指南提出的建议。 根据周围技术堆栈对于命名规范相关我们建议您阅读微软提供的:框架设计指南 优先级定义 (A
本项目的风格指南主要是参照 vue 官方的风格指南。在真正开始使用该项目之前建议先阅读一遍指南,这能帮助让你写出更规范和统一的代码。当然每个团队都会有所区别。其中大部分规则也都配置在了eslint-plugin-vue之中,当没有遵循规则的时候会报错,详细内容见eslint章节。 当然也有一些特殊的规范,是不能被 eslint 校验的。需要人为的自己注意,并且来遵循。最主要的就是文件的命名规则,这
注: 内容翻译来自官网资料 Style Guide. 这个文档为.proto文件提供风格指南. 通过遵循下列约定, 可以让protocol buffer消息定义和他们对应的类保持一致并容易阅读. 消息和字段名 消息名使用驼峰法 - 例如, SongServerRequest. 字段名使用下划线分隔 - 例如, song_name. message SongServerRequest { req
这份指南旨在提供一系列 Ruby on Rails 4 开发的最佳实践和风格惯例。本指南与社区驱动并制定的 Ruby 编码风格指南可以互为补充。