目录
1.1 使用useGeneratedKeys、keyProperty
可查看该链接(转载)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>