java Mybatis框架动态SQL
1.if+where
<select id="getgoods" resultType="Goods" >
select * from goods
<where>
<if test="name!=null and name!=' ' ">
and name concat('%',#{name},'%')
</if>
<if test="条件">
and .....
</if>
</where>
</select>
2.if+trim
(查询的)
<trim prefix="where" prefixOverrides="and | or">
<if test="条件">
and .....
</if>
</trim>
(修改的)
<trim prefix="set" sufixOverrides="," suffix="where id=#{id}">
<if test="条件">
name=#{name}
</if>
</trim>
3.if+set(用于修改)
update goods(表名)
<set>
<if test="条件">
name=#{name},
</if>
</set>
where id=#{id}
4.foreach
<select id="getgoods" resultType="goods">
select * from goods where price in
<foreach collection="array" items="prices" open="("separator=","close=")">
#{prices}
</foreach>
</select>
collection:数组类型
若参数类型是一个List(动态数组) collection属性值为list
若参数类型是一个数组的时候 collection属性值为array
备注:如果参多个参数的话就封装为一个Map
用法如下:
sql映射文件
test代码:
动态sql大致这么多,谢谢参考。
最新文章
- osx下配置Cocos2d-x 3.x Android开发环境
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.8.Datepicker控件
- iOS --- 通过openURL实现APP之间跳转并传递数据
- iOS字体设置
- js 如何获取文本框中光标索引位置
- XML DOM操作,适用目前流行的浏览器
- 【KMP】Oulipo
- ionic2 +Angular 使用自定义图标
- 深入解析Linux中的fork函数
- cocos2d-x 开发常见问题:
- ELK(elasticsearch+kibana+logstash)搜索引擎(一): 环境搭建
- Linux 小知识翻译 - 「Shell 脚本」
- Gravitee.io docker-compose运行
- windows7(64位) PHP APACHE MYSQL
- 【Cf #502 H】The Films(莫队)
- SQL的子查询操作
- 用 go 写 WebAssembly入门
- Java基础教程(7)--运算符
- PhoneGap获取设备信息
- Flask框架 之 第三方组件