六、深入学习映射配置文件(返回主键、动态SQL)

目录

一、返回主键

1.1 使用useGeneratedKeys、keyProperty

1.2 使用selectKey

 二、动态SQL

2.1 if 标签

2.2 set 标签

2.3 foreach 标签

2.3.1 集合

 2.3.2 数组

 2.4 sql片段 -  sql 标签


可查看该链接(转载)https://blog.csdn.net/qq_39623058/article/details/88779242 

一、返回主键

在数据库插入一条记录后返回这条记录在数据库中主键的值;

1.1 使用useGeneratedKeys、keyProperty

useGeneratedKeys="true" 声明返回主键

keyProperty="id" 把返回主键的值,封装到实体的id属性中

注意 :只适用于 主键自增 的数据库,mysql和sqlserver支持, oracle不支持
public interface UserMapper { 
    // 返回主键 
    public void save(User user); 
}
<?xml version="1.0" encoding="utf-8"?>

<!-- 
    useGeneratedKeys="true" 声明返回主键 
    keyProperty="id" 把返回主键的值,封装到实体的id属性中 
    注意:只适用于主键自增的数据库,mysql和sqlserver支持,oracle不支持 
-->
<insert id="save" parameterType="user" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO `user`(username,birthday,sex,address) 
    values(#{username},#{birthday},#{sex},#{address})
</insert>

1.2 使用selectKey

selectKey 适用范围广,支持所有类型数据库
keyColumn="id" 指定主键列名
keyProperty="id" 指定主键封装到实体的id属性中
resultType="int" 指定主键类型
order="AFTER" 设置在sql语句执行前(后),执行此语句
<?xml version="1.0" encoding="utf-8"?>

<insert id="save" parameterType="user"> 
    <selectKey keyColumn="id" keyProperty="id" resultType="int" order="AFTER">
        SELECT LAST_INSERT_ID();
    </selectKey> 
    INSERT INTO `user`(username,birthday,sex,address) 
    values(#{username},#{birthday},#{sex},#{address})
</insert>

 二、动态SQL

2.1 if 标签

public List<User> findByIdAndUsernameIf(User user);
<?xml version="1.0" encoding="utf-8"?>

<select id="findByIdAndUsernameIf" parameterType="user" resultType="user">
  SELECT * FROM `user` 
  <where> 
    <if test="id != null and id != ''">
        AND id = #{id}
    </if>  
    <if test="username != null and username != ''">
        AND username = #{username}
    </if> 
  </where> 
</select>

这里的<where> 相当于 where 1=1,但是如果没有条件,就不会拼接

select * from user 【where 1=1】 【and id=1】 【and username = ‘AA’】;

2.2 set 标签

更新数据表

public void updateIf(User user);
<?xml version="1.0" encoding="utf-8"?>

<update id="updateIf" parameterType="user">
  UPDATE `user` 
  <set>
    <if test="username != null">
        username = #{username},
    </if>  
    <if test="birthday != null">
        birthday = #{birthday},
    </if>  
    <if test="sex !=null">
        sex = #{sex},
    </if>  
    <if test="address !=null">
        address = #{address},
    </if> 
  </set> 
  WHERE id = #{id}
</update>
set标签在更新的时候,自动加上set关键字,然后去掉最后一个条件的逗号

2.3 foreach 标签

数据的遍历循环        

如:select * from user where id in (1,2,3) ,传入的参数就要使用<foreach>实现

使用方式:

<foreach> 标签用于遍历集合,它的属性:
• collection :代表要遍历的集合元素
• open :代表语句的开始部分
• close :代表结束部分
• item :代表遍历集合的每个元素,生成的变量名
• sperator :代表分隔符

2.3.1 集合

public List<User> findByList(List<Integer> ids);
<?xml version="1.0" encoding="utf-8"?>

<select id="findByList" parameterType="list" resultType="user">
    SELECT * FROM `user` 
    <where> 
        <foreach collection="collection" open="id in(" close=")" 
                    item="id" separator=",">
            #{id}
        </foreach> 
  </where> 
</select>
如果查询条件为普通类型 List集合,collection属性值为:collection 或者 list

 2.3.2 数组

public List<User> findByList(Integer[] ids);
<?xml version="1.0" encoding="utf-8"?>

<select id="findByArray" parameterType="int" resultType="user">
    SELECT * FROM `user` 
    <where> 
        <foreach collection="array" open="id in(" close=")" 
                    item="id" separator=",">
            #{id}            
        </foreach> 
  </where> 
</select>
如果查询条件为普通类型 Array数组,collection属性值为:array

 2.4 sql片段 -  sql 标签

提取重复sql,使用<include> 引用

<!--抽取的sql片段--> 
<sql id="selectUser"> 
    SELECT * FROM `user` 
</sql> 

<select id="findByList" parameterType="list" resultType="user" > 
    <!--引入sql片段--> 
    <include refid="selectUser"></include> 
    <where> 
        <foreach collection="collection" open="id in(" close=")" item="id" separator=",">
            #{id} 
        </foreach> 
    </where> 
</select> 

<select id="findByArray" parameterType="integer[]" resultType="user"> 
    <!--引入sql片段--> 
    <include refid="selectUser"></include> 
    <where> 
        <foreach collection="array" open="id in(" close=")" item="id" separator=",">
            #{id} 
        </foreach> 
    </where> 
</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值