Java
来源:互联网 发布:雾霾防护口罩知乎 编辑:程序博客网 时间:2024/06/07 13:38
对于一些复杂的查询,我们可能会指定多个查询条件,但是这些条件可能存在也可能不存在,例如在58同城上面找房子,我们可能会指定面积、楼层和所在位置来查找房源,也可能会指定面积、价格、户型和所在位置来查找房源,此时就需要根据用户指定的条件动态生成SQL语句。如果不使用持久层框架我们可能需要自己拼装SQL语句,还好MyBatis提供了动态SQL的功能来解决这个问题。MyBatis中用于实现动态SQL的元素主要有:
- if
- choose / when / otherwise
- trim
- where
- set
- foreach
下面是映射文件的片段。
<select id="foo" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </select>
当然也可以像下面这些书写。
<select id="foo" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>再看看下面这个例子。
<select id="bar" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
阅读全文
1 0
- java
- JAVA
- JAVA
- JAVA
- java
- Java
- Java
- JAVA:
- java
- java
- java
- java
- Java
- java
- java
- java
- JAVA?
- java
- HSF和dubbo的区别
- linux安装zookeeper
- 初始化参数之memory_target
- IDEA--【Intellij Idea】Project Structure 讲解
- 移动端车牌识别哪家好
- Java
- pymysql 提交sql语句报错问题
- php-fpm的作用
- C语言进阶-第6讲:递归法问题求解(递归求数组的最大值)
- float等比例布局
- 何为设计中的美?
- Android自定义控件0----View绘制的原理
- Spring事务异常回滚
- 近期任务