Mybaits--动态拼接SQL语句之常用标签(包含一对多双向配置所用标签)
来源:互联网 发布:网络不文明行为 编辑:程序博客网 时间:2024/05/29 15:39
先看一下有哪些常用标签
where标签 及使用前后效果对比
使用where标签前 <select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1 <!-- &&=&&=and " "="" #{} = ? --> <if test="command !=null &&!"".equals(command.trim())"> and COMMAND=#{command} </if> <if test="description != null and !"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%' </if> </select>使用where标签后(仅仅少了where1=1这个小技巧)<select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE <where> <!-- &&=&&=and " "="" #{} = ? --> <if test="command !=null &&!"".equals(command.trim())"> and COMMAND=#{command} </if> <if test="description != null and !"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%' </if> </where> </select>
sql 标签 及引用
作用:
1.相当于定义一个常量属性,用的时候就进行引用,提升代码的复用性。
2.数据库字段的扩展不会对原有的sql语句产生影响。
<sql id="columns">ID,COMMAND,DESCRIPTION,CONTENT</sql><select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select <include refid="columns"></include> from MESSAGE <where> <!-- &&=&&=and " "="" #{} = ? --> <if test="command !=null &&!"".equals(command.trim())"> and COMMAND=#{command} </if> <if test="description != null and !"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%' </if> </where> </select>
set 标签
作用:在update语句中解决动态修改多个值SQL语句的拼接问题
<update id=""> update MESSAGE <set> <if test="command !=null &&!"".equals(command.trim())"> COMMAND=#{command}, </if> <if test="description != null and !"".equals(description.trim())"> DESCRIPTION = #{description}, </if> </set> </update>
trim 标签
作用:根据项目需求,灵活解决动态SQL语句的拼接问题
相当于where标签自动去除SQL语句where前边的and/or suffix="后缀,下标" 代表如果标签中有输入值则输出suffix="后缀,下标"定义的内容 <trim prefix="where" suffix="后缀,下标" prefixOverrides="and/or"> </trim> 相当于set标签自动去除SQL语句后边的',' <trim prefix="set" suffixOverrides=","> </trim>
choose 标签
作用:相当于
if(){}
elseif(){}
else{},根据逻辑条件动态拼接SQL语句
<select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select <include refid="columns"></include> from MESSAGE <where> <choose> <when test="description != null and !"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%' </when> <when test="command !=null &&!"".equals(command.trim())"> and COMMAND=#{command} </when> <otherwise>and COMMAND=#{command}</otherwise> </choose> </where> </select> <sql id="columns">ID,COMMAND,DESCRIPTION,CONTENT</sql>
association 标签 (多对一关联映射)
作用:做关联查询的时候把父表的数据,映射到子表实体类中引用父类的对应【父类对象属性】中,通过此属性可以取出父表中的取值
<mapper namespace="CommandContent"> <resultMap type="com.imooc.bean.CommandContent" id="Content"> <id column="ID" jdbcType="INTEGER" property="id"/> <result column="CONTENT" jdbcType="VARCHAR" property="content"/> <result column="COMMAND_ID" jdbcType="VARCHAR" property="commandId"/> <association property="字表引用父表的属性名" resultMap="(父表配置中)namespace.resultMap-id"></association> </resultMap></mapper>
collection 标签 (一对多关联映射)
作用:做关联查询的时候把子表的数据,映射到父表实体类中引用子类的对应【子类集合属性】中,通过此属性可以取出子表中的取值
<resultMap type="com.imooc.bean.Command" id="Command"> <!-- column对应的为sql语句中字段名,如果给字段取别名,column必须配置为别名才能被识别 --> <id column="C_ID" jdbcType="INTEGER" property="id" /> <result column="NAME" jdbcType="VARCHAR" property="name" /> <result column="DESCRIPTION" jdbcType="VARCHAR" property="description" /> <!-- 【一方】配置文件中【引用】 【一方】实体类定义的【多方】的集合 --> <collection property="contentList" resultMap="CommandContent.Content" /> </resultMap></mapper>
forech 标签 (遍历集合)
作用:多用于查询语句的in语句中,用来遍历集合,达到select * from tablename where column in (1,2,3,4…,n)语句的效果;
<!--包含递归算法的in语句和遍历集合--> <select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map"> SELECT <include refid="Base_Column_List"></include> from mmall_product where status = 1 <if test="productName!=null"> AND name LIKE #{productName} </if> <if test="categoryIdList !=null"> and category_id in <foreach collection="categoryIdList" item="item" open="(" close=")" index="indext" separator=","> #{item} </foreach> </if> </select></mapper>
阅读全文
0 0
- Mybaits--动态拼接SQL语句之常用标签(包含一对多双向配置所用标签)
- Mybatis配置及动态SQL语句标签
- Mybaits--配置文件之动态SQL配置备忘录
- mybaits之动态sql
- 动态SQL语句--mysql--mybaits-
- Mybaits 的动态sql语句
- mybaits中常用表标签详解
- 智能 标签 一对多
- mybatis生成动态sql的常用标签
- mybatis中动态sql常用的标签
- mybatis中动态sql常用的标签
- mybatis中动态sql常用的标签
- mybatis中动态sql常用的标签
- Mybaits--使用log4j调试动态SQL语句
- MySQL中根据if标签实现多条件模糊查询(动态SQL语句)
- mybatis 之 动态sql(一)choose标签的使用
- MyBatis学习之动态SQL标签
- 动态sql之使用choose标签
- Python基础知识思维导图
- 阿里云服务器Ubuntu14.0.4部署git管理网站
- logistic算法解析
- MariaDB的安装
- 计算机英语·UVWXYZ
- Mybaits--动态拼接SQL语句之常用标签(包含一对多双向配置所用标签)
- 同步复位和异步复位的比较
- java复用类
- [HDU](1576) A/B ----扩展欧几里德(数论)
- apt-cache 的简单
- 分治法举例之快速排序,归并排序
- 一步一步实现 iOS 微信自动抢红包(非越狱)
- Android TextView设置阴影效果
- gulp详细入门教程