mybatis学习之动态sql

mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录:

1、select查询

简单的select类似如下:

<select id="findById" resultMap="StudentResult" parameterType="Integer">
    select * from t_student where id = #{id}
</select>

1)if(常用于各种查询的条件判断部分)

<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    where gradeId = #{gradeId}
    <if test="name != null">
        and name like #{name}
    </if>
    <if test="age != null">
        and age = #{age}
    </if>
</select>

结合where标签使用如下:

<select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    <where>
        <if test="gradeId != null">
            gradeId = #{gradeId}
        </if>
        <if test="name != null">
            and name like #{name}
        </if>
        <if test="age != null">
            and age = #{age}
    </if>
    </where>
</select>

2)choose(同if..else..类似)

<select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    <choose>
        <when test="searchBy==‘gradeId‘">
            where gradeId = #{gradeId}
        </when>
        <when test="searchBy==‘name‘">
            where name like #{name}
        </when>
        <otherwise>
            where age = #{age}
        </otherwise>
    </choose>
</select>

3)trim

<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    <trim prefix="where" prefixOverrides="and|or">
        <if test="gradeId != null">
            gradeId = #{gradeId}
        </if>
        <if test="name != null">
            and name like #{name}
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
    </trim>
</select>

prefix前置,prefixOverrides前置覆盖,简单理解为:trim子句中最前面的and或者or用where替换。

4)foreach

<select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    <if test="gradeIds != null">
        <where>
            gradeId in
            <foreach collection="gradeIds" item="gradeId" open="("
                close=")" separator=",">
                #{gradeId}
            </foreach>
        </where>
    </if>
</select>

collections即数组集合,可以是list类型,如arrayList等,open指定左侧拼接方式,close指定右侧,separator指定分隔符,这里拼接后为括号括起来逗号隔开的字符串,从而用于in查询。

2、update

<update id="updateStudent" parameterType="Student">
    update t_student
    <set>
        <if test="name != null">
            name = #{name},
        </if>
        <if test="age != null">
            age = #{age}        <!-- 自动剔除最后的逗号 -->
        </if>
    </set>
    where id = #{id}
</update>
<update id="update" parameterType="Student">
    update t_student set name =
    #{name},age = #{age} where id = #{id}
</update>

3、delete

<delete id="delete" parameterType="Integer">
    delete from t_student where id = #{id}
</delete>

4、insert

<!-- 插入 -->
<insert id="add" parameterType="Student">
    insert into t_student(id,name,age) values(null,#{name},#{age})
</insert>
时间: 2024-08-06 03:44:12

mybatis学习之动态sql的相关文章

Mybatis学习笔记-动态SQL与模糊查询

需求:实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间) User.java实体类 public class User { private int id; private String name; private int age; //... } ConditionUser.java public class ConditionUser { private String name; private int minAge; private int maxAge; //... }

Mybatis学习之动态sql语句(7)

Mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似. 3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀) 4. where (主要是用来简化sql语句中where条件判断的,能智能的

Mybatis使用之动态SQL语句

Mybatis使用之动态SQL语句 一:简介 Mybatis动态SQL语句可帮助我们根据需要动态拼接SQL语句.主要在配置文件中使用<where> <if><choose><when><otherwise> <set> <trim><foreach>标签来实现. 二:具体使用方式 2.1 where 2.1.1 功能 语句的作用主要是简化SQL语句中where中的条件判断,where元素的作用是会在写入wher

MyBatis进阶使用——动态SQL

MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBatis精简了元素种类,在MyBatis3中,我们只需要学习以下4种元素: if choose(when,otherwise) trim(where,set) foreach if 动态SQL通常要做的事情就是根据条件包含where子句的一部分,比如: <select id="findActiveB

MyBatis中的动态SQL

动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号.利用动态 SQL 这一特性可以彻底摆脱这种痛苦. 虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形. 动态 SQL 元素和 JSTL 或基于类似 XML 的文本处

Mybatis框架之动态SQL书写方式小结

动态SQL简介 动态SQL是Mybatis框架中强大特性之一.在一些组合查询页面,需要根据用户输入的查询条件生成不同的查询SQL,这在JDBC或其他相似框架中需要在代码中拼写SQL,经常容易出错,在Mybatis框架中可以解决这种问题. 使用动态SQL元素与JSTL相似,它允许我们在XML中构建不同的SQL语句.常用元素为: 判断元素:if,choose 关键字元素:where,set,trim 循环元素:foreach if元素 if元素是简单的条件判断逻辑,满足指定条件时追加if元素内的SQ

mybatis入门基础----动态SQL

原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接. 1.3.UserMapper.xml 1 <!-- 用户信息综合查询

MyBatis学习17-动态sql

1.mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 2.问题来源 <select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom"> select * from user where sex= #{userCustom.sex} and username like '%${userCustom.us

6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些东西. 动态Sql主要有以下类型: if choose,when,otherwise trim,where,set foreach 这里主要介绍几个常见的where  if  foreach,直接贴代码了 1.where 这里的where有一个好处就是在拼接成功的时候,会自动去掉第一个and 2.i