批量查询
Mapper层
List<Plant> selectListByPlantId(@Param("city")String city, @Param("plantName") String plantName,@Param("userName")String userName,@Param("list")List<Integer> plantiIds);
xml
<select id="selectListByPlantId" resultType="com.aa.Plant">
select * from plant
where 1=1
<if test="city != null and city != ''">
and city like concat('%',#{city},'%')
</if>
<if test="plantName != null and plantName != ''">
AND plantName like concat('%',#{plantName},'%')
</if>
<if test="userName != null and userName != ''">
AND accountName like concat('%',#{userName},'%')
</if>
<if test="list != null and list.size() > 0">
and id in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
</select>
这里要对list判断是不是为null,之前没有加判断一直报错,再就是where 1=1 这里是因为不能直接连and语句,所以加了一个一定正确的条件。
批量插入
int insertList((@Param("list")List<UsersModel> list);
<!--批量插入信息-->
<insert id="insertList" parameterType="java.util.List">
insert into users(
id, name
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id,jdbcType=INTEGER}, #{item.name,jdbcType=VARCHAR}
)
</foreach>
</insert>
批量插入跟批量查询类似,这里的UserModel是个对象,所以插入的时候取的时候item.id
批量删除和批量修改和上面的类似可参考文章: