链接
https://mybatis.org/mybatis-3/zh/getting-started.html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
int deleteObjects(@Param(“ids”) Integer…ids);
namespace=“com.test.dao.GoodsDao” namespace后面是类的全限定类名
如果在映射文件中collection使用ids 需要在dao层的操作语句中加@param注解open 属性是开始,close是结束,separator是中间间隔符号 item是中间的 每一个元素,#{id}占位符获取每个id值
如果不使用可以用array
映射文件代码
<mapper namespace="com.test.dao.GoodsDao">
<delete id="deleteObjects">
delete from tb_goods
where id in <!-- (1,2,3,4,5) -->
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</delete>
</mapper>
数据操作语句优化
<delete id="deleteObjects">
delete from tb_goods
<choose>
<when test="ids!=null and ids.length>0">
where id in <!-- (1,2,3,4,5) -->
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</when>
<otherwise>
where 1=2;
</otherwise>
</choose>
</delete>
<delete id="deleteObjects">
delete from tb_goods
<where>
<choose>
<when test="ids!=null and ids.length>0">
id in <!-- (1,2,3,4,5) -->
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</when>
<otherwise>
1=2;
</otherwise>
</choose>
</where>
</delete>
<delete id="deleteObjects">
delete from tb_goods
<where>
<if test="ids!=null and ids.length>0">
id in <!-- (1,2,3,4,5) -->
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</if>
or 1=2
</where>
</delete>
本文地址:https://blog.csdn.net/lpw12121/article/details/107375692
如对本文有疑问, 点击进行留言回复!!
详解SpringBoot修改启动端口server.port的四种方式
网友评论