先看文章目录,大致了解知识点结构,直接点击文章目录可以跳转到文章指定位置。
①使用 标签,我们提前把一些SQL片段(语句或关键字)封装起来 ,方便调用
<sql id="baseColumn">
goods_id,goods_name,goods_price,goods_img
sql>
<sql id="codeWhere">
goodsName=#{goodsName}
sql>
<insert id="addGoods" parameterType="com.Goods">
insert into goods (
<include refid="baseColumn"/>
) values(null,#{goodsName},#{goodsPrice},#{goodsImg})
insert>
<select id="seeGoods" parameterType="com.Goods" >
select <include refid="baseColumn"/>
from goods
where <include refid="codeWhere"/>
select>
在 mapper 的动态 SQL 中如果出现大于号、小于号、大于等于号,小于等于号等XML 可能会出现解析出错问题。
这里提供两种解决办法:
①使用
②将其转换为实体符号
第一种使用 | 第二种使用实体符号 | 对应符号 | 符号说明 |
---|---|---|---|
< | < | 小于 | |
]]> | > | > | 大于 |
<= | <= | 小于等于 | |
= ]]> | ≥或者>= | >= | 大于等于 |
]]> | <> | <> | 不等于 |
\ | & | & | 与 |
\ | ' | ‘ | 单引号 |
\ | " | " | 双引号 |
\ | 空格 |
用标签把SQL片段包起来
if 标签:对值进行逻辑判断 根据结果 决定是否拼接if中的sql
<if test="goodsImg !=null">
goods_img=#{goodsImg},
if>
if和set结合使用
<update id="updateATMById" parameterType="com.bean.ATM">
update atm set
<trim suffixOverrides=",">
<if test="atmName!=null and atmName!=''">
atm_name=#{atmName},
if>
<if test="atmCode!=null and atmCode!=''">
atm_code=#{atmCode},
if>
<if test="atmPwd!=null and atmPwd!=''">
atm_pwd=#{atmPwd},
if>
<if test="atmPhoto!=null and atmPhoto!=''">
atm_photo=#{atmPhoto},
if>
<if test="atmMoney!=0.0">
atm_money=#{atmMoney},
if>
trim>
where atm_id=#{atmId}
update>
if和where结合使用
<select id="getByActivity" parameterType="com.bean.ATM" resultMap="goodsResultMap">
select * from atm
<where>
<if test="atmId!=0">
and atm_id = #{atmId}
if>
<if test="atmName!=null and atmName!=''">
and atm_name like '%' #{atmName} '%'
if>
<if test="atmCode!=null and atmCode!=''">
and atm_code =#{atmCode}
if>
<if test="atmPwd!=null and atmPwd!=''">
and atm_pwd =#{atmPwd}
if>
where>
select>
trim标签所有场合均能使用,既能去除逗号,又能去除and
属性 | 描述 |
---|---|
prefix | 给sql语句拼接的前缀 |
suffix | 给sql语句拼接的后缀 |
prefixOverrides | 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND" |
suffixOverrides | 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定 |
举例一:
<select id="getByTrim" parameterType="com.bean.ATM" resultMap="atmResultMap">
select * from atm
<trim prefix="where (" suffix=")" prefixOverrides="and">
<if test="atmId!=0">
and atm_id = #{atmId}
if>
<if test="atmName!=null and atmName!=''">
and atm_name like '%' #{atmName} '%'
if>
<if test="atmCode!=null and atmCode!=''">
and atm_code =#{atmCode}
if>
<if test="atmPwd!=null and atmPwd!=''">
and atm_pwd =#{atmPwd}
if>
trim>
select>
<insert id="saveATMByTrim" parameterType="com.bean.ATM" >
insert into atm
(
<trim suffixOverrides=",">
<if test="atmName!=null and atmName!=''">
atm_name,
if>
<if test="atmCode!=null and atmCode!=''">
atm_code,
if>
<if test="atmPwd!=null and atmPwd!=''">
atm_pwd,
if>
<if test="atmMoney!=0.0">
atm_money,
if>
<if test="atmPhoto!=null and atmPhoto!=''">
atm_photo,
if>
trim>
)
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="atmName!=null and atmName!=''">
#{atmName},
if>
<if test="atmCode!=null and atmCode!=''">
#{atmCode},
if>
<if test="atmPwd!=null and atmPwd!=''">
#{atmPwd},
if>
<if test="atmMoney!=0.0">
#{atmMoney},
if>
<if test="atmPhoto!=null and atmPhoto!=''">
#{atmPhoto},
if>
trim>
insert>
再次举例
<insert id="insert" parameterType="account">
insert into account
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="money != null">
money,
if>
<if test="name != null and name != ''">
name
if>
trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="money != null">
#{money},
if>
<if test="name != null and name != ''">
#{name}
if>
trim>
insert>
批量操作trim和foreach结合
<select id="getByList" parameterType="java.util.List" resultMap="goodsResultMap">
select * from goods where goods_id in
<trim prefix="(" suffix=")">
<foreach collection="list" item="goodsId" separator=",">
#{goodsId}
foreach>
trim>
select>
批量删除
<delete id="deleteBatch">
delete from account where aid in
<foreach collection="array" item="aid" open="(" close=")" separator=",">
#{aid}
foreach>
delete>
批量查询
<select id="findATMByIds" resultMap="atmResultMap">
select * from atm
where atm_id in
<foreach collection="array" open="(" item="aid" separator="," close=")">
#{aid}
foreach>
select>
<select id="findATMByIdList" resultMap="atmResultMap">
select * from atm
where atm_id in
<foreach collection="list" open="(" item="aid" separator="," close=")">
#{aid}
foreach>
select>
注意:这里%需要使用双引号,不能使用单引号。因为#{···}解析成sql语句会在变量外侧自动加单引号’’
<select id="findATMByName" parameterType="string" resultMap="baseMap">
select id,name,code,money
from atm
where name like "%"#{Name}"%"
select>
注意:MySQL的CONCAT()函数用于将多个字符串拼接成一个字符串。
<select id="findATMByName" parameterType="string" resultMap="baseMap">
select id,name,code,money
from atm
where name like concat("%",#{Name},"%")
select>
注意:在bind标签中自定义拼接后,SQL语句将使用bind的name
<select id="findATMByName" parameterType="string" resultMap="baseMap">
select id,name,code,money
from atm
<bind name="pattern" value="'%'+Name+'%'">bind>
where name like #{pattern}
select>