Mtbatis
为大家介绍一下Mybatis的强大功能
标签元素
if:利用if实现简单的条件选择
choose(when,otherwise):相当于java中的switch语句,通常与when和otherwise搭配
where:简化SQL语句中where的条件判断
set:解决动态更新语句
trim:可以灵活地去除多余的关键字
foreach:迭代一个集合,通常用于in条件
首先从小白开始的基本sql语句CRUD
新增
insert into user(username,password) values (#{username},#{username})
条件查询
select * from user where username=#{username}
修改
update user set password=#{password} where username=#{username}
删除
dekete from user where username=#{username}
mybatis的动态sql
使用if-where
<select id="getUserList" resultType="User">
select * from user
<where>
<if test="username!=null and username!=''">
and username like CONCAT('%',#{username},'%')
</if>
<if test="password!=null and password!=''">
and password like CONCAT('%',#{password},'%')
</if>
</where>
</select>
上面代码是最简单的if+where的sql语句,where元素会自动识别其标签内是否有返回值,若有,就插入一个where关键字,若该标签返回的内容是以and或者or开头的,where元素会将其自动剔除,if元素标签里主要的属性就是test属性,test后面跟的是一个表达式,返回true或者false,以此来进行判断
使用if+trim实现对条件查询
<select id="getUserList" resultType="User">
select * from user
<trim prefix="where" prefixOverride="and|or">
<if test="username!=null and username!=''">
and username like CONCAT('%',#{username},'%')
</if>
</trim>
</select>
上面代码多了几个元素
prefix:前缀,作用是在trim包含的内容上加上前缀。
suffix:后缀,作用是在trim包含的内容上加上后缀。
prefixOverride:对于trim包含内容的首部进行指定内容(如此出的“and|or“)的忽略。
suffixOverride:对于trim包含内容的首尾部进行指定内容的忽略。
使用if+set实现更新操作
<update id="modify" parameterType="AppInfo">
update user
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
</set>
where id=#{id}
</update>
使用if-trim更新
<update id="modify" parameterType="AppInfo">
update user
<trim prefix="set" suffixOverrides="," suffix="where id = #{id}>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
</trim>
</update>
使用if-where新增
insert into
user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null and id !=''">
id,
</if>
<if test="username != null and username !=''">
username,
</if>
<if test="password != null and password !=''">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null and id !=''">
#{id},
</if>
<if test="username != null and username !=''">
#{username},
</if>
<if test="password != null and password !=''">
#{password},
</if>
</trim>
接下来就算批量操作了
基本元素
Item:表示集合中每一个元素进行迭代的别名(如此处的roleIds)。
index:指定一个名称,用于表示在迭代过程中,每次迭代到的位置。
open:表示该语句以什么开始(既然是in条件语句,所以必然是以“(”开始)
separator:表示在每次进行迭代之间以什么符号作为分隔符(既然是in条件语句,所以必然以“,作为分隔符)。
close:表示该语句以什么结束(既然是in条件语句,所以必然是以“)”结束)。
collection:最关键并最容易出错的属性,需格外注意,该属性必须指定,不同情况下,该属性的值是不一样的。主要有三种情况
若入参为单参数且参数类型是一个list的时候,collection属性值为list。
若入参为单参数且参数类型是一个数组的时候,collection属性值为array(此处传入参数Integer[] roleIds为数组类型,故此处collection属性值设为“array”)。
若传入参数为多参数,就需要把它们封装为一个Map进行处理
入参为数组的foreach迭代查询
<select id="getUser_foreach_array" >
select * from user where username in
<foreach collection="array" item="usernames" open="(" separator=","close=")">
#{usernames}
</foreach>
</select>
批量新增
insert into user
(username,passowrd,create_date)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.username},
#{item.passowrd},
now()
)
</foreach>
批量修改
<foreach collection="list" item="item" index="index" separator=";">
update
user
<trim prefix="SET" suffixOverrides=",">
<if test="item.password != null and item.password != ''">
password=#{item.password},
</if>
</trim>
where username=#{item.username}
</foreach>
批量删除
<delete id="delete_foreach_array" >
delete from user where username in
<foreach collection="array" item="usernames" open="("separator","close=")">
#{usernames}
</foreach>
</delete>
批量新增返回主键
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>