Mybatis常用的方法大全
一、查询
1、数组
后台mapper
public List<User> dynamicForeach2Test(int[] ids);
xml文件
<select id="dynamicForeach2Test" resultType="com.mybatis.entity.User">
select * from t_user where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
2、Map
后台mapper
public List<User> dynamicForeach3Test(Map<String, Object> params);
xml文件
<select id="dynamicForeach3Test" resultType="com.mybatis.entity.User">
select * from t_user where username like '%${username}%' and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
3、单一List
后台mapper
public List<User> dynamicForeachTest(List<Integer> ids);
xml文件
<select id="dynamicForeachTest" resultType="com.mybatis.entity.User">
select * from t_user where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
4、List里放Map
后台mapper
public List<User> dynamicForeachTest(List<Map<String,Object>> list);
xml文件
<select id="dynamicForeachTest" resultType="com.mybatis.entity.User">
select * from t_user where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</select>
二、新增
后台mapper
public int dynamicForeachTest(User user);
xml文件
<insert id="addUser" parameterType="com.mybatis.entity.User"
useGeneratedKeys="true" keyProperty="id">
insert into user(userName,userAge,userAddress)
values(#{userName},#{userAge},#{userAddress})
</insert>
三、修改
后台mapper
public Integer dynamicForeachTest(User user);
xml文件
<update id="updateUser" parameterType="User" >
update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}
</update>
四、删除
后台mapper
public Integer dynamicForeachTest(@Param("id") String userId);
xml文件
<delete id="deleteUser">
delete from user where id=#{id}
</delete>