mybatis部分语法
- 获取sqlSession
- foreach
- where
- choose
- like
6.传参
7、
多对1(1)
多对1(2)


1对多 (1)
1对多(2)
自关联查询(类似递归查询)
批量更新
<update id="batchUpdateEvaScores" parameterType="java.util.List">
<!--方法一-->
<foreach collection="list" item="item" index="index" separator=";" open="" close="">
update eva_score
<set>baifenbi=#{item.baifenbi},
taskid=#{item.taskid},
unit_type=#{item.unit_type}
</set>
where name=#{item.name} and mg_type=#{item.mgType}
</foreach>
<!--
<!--方法二-->
<foreach collection="list" separator=";" item="cus">
update eva_score set
baifenbi = #{cus.baifenbi},
taskid = #{cus.taskid},
unit_type = #{cus.unit_type}
where name=#{cus.name} and mg_type=#{cus.mgType}
</foreach>
<!-- 方法三 -->
update eva_score
<trim prefix="set" suffixOverrides=",">
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.baifenbi !=null and item.baifenbi != -1">
when name=#{item.name} then #{item.baifenbi}
</if>
<if test="item.baifenbi == null or item.baifenbi == -1">
when name=#{item.name} then eva_score.baifenbi//原数据
</if>
</foreach>
</trim>
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.taskid !=null and item.taskid != -1">
when name=#{item.name} then #{item.taskid}
</if>
<if test="item.taskid == null or item.taskid == -1">
when name=#{item.name} then eva_score.taskid//原数据
</if>
</foreach>
</trim>
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.unit_type !=null and item.unit_type != -1">
when name=#{item.name} then #{item.unit_type}
</if>
<if test="item.unit_type == null or item.unit_type == -1">
when name=#{item.name} then eva_score.unit_type//原数据
</if>
</foreach>
</trim>
</trim>
where name in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.name}
</foreach>
-->
</update>
注意:当使用第一第二种写法时,需要给mysql配置批量执行,在spring.datasource.url后加上allowMultiQueries=true
如 spring.datasource.url=jdbc:mysql://47.93.23.66:3306/db_smarte?allowMultiQueries=true
否则,将报错不能执行。
8.插入后获取id
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="purchaseOrderNew.id">
SELECT LAST_INSERT_ID()
</selectKey>