1、多参数传递
@Param(传递的形参参数名1) 实参参数1, ... @Param(传递的形参参数名n-1) 实参参数n-1, @Param(传递的形参参数名n) 实参参数n
参考链接
1. 五、深入理解Mybatis中的参数parameterType (传递一个简单参数,传递多个参数:@Param、使用自定义对象、按位置、使用Map)
2、 parameterType=“java.util.List” 自动查找实体类参数
mapper.xml 文件内的参数格式:parameterType="java.util.List"
可以传递任意的实体类数组 List<Object>
具体的例子如下:
TestMapper.xml
批量更新操作演示:更新表是 test_table
<mapper namespace="com.test.mapper.TestMapper">
<update id="testUpdate" parameterType="java.util.List">
<choose>
<when test="list != null and list.size() > 0">
<foreach collection="list" item="item" separator=";">
UPDATE test_table
<set>
name=#{item.testname}
</set>
WHERE id=#{item.id}
</foreach>
</when>
<otherwise>
select ""
</otherwise>
</choose>
</update>
</mapper>
Java 调用演示:
1. mytestEntity 实体类有 id 和 testname 字段
2. TestMapper.java 设计
import org.apache.ibatis.annotations.Mapper;
import com.test.entity.mytestEntity;
import java.util.List;
@Mapper
public interface TestMapper{
// 查询数据
List<mytestEntity> getMyTestList();
// 批量更新数据
Integer testUpdate(List<mytestEntity> testList);
}
4. testUpdate 接口设计:
testUpdate(List<mytestEntity> testList);
5. getMyTestList是获取一个实体类为mytestEntity的 List 数组数据
############################################# 具体实际演示如下
import com.test.entity.mytestEntity;
import com.test.mapper.TestMapper;
import java.util.List;
public class Mytest{
@Autowired
private TestMapper testMapper;
public void test() {
// 查询数据
List<mytestEntity> testEntity = testMapper.getMyTestList();
// 批量更新数据
Integer result = testMapper.testUpdate(testEntity);
}
}
参考链接
1. @Param注解的用法解析与parameterType
2. MyBatis学习之路——获取参数值和各种查询功能(查询实体类、List集合、Map集合)
4. Mybatis insert报错 Cause: java.sql.SQLException: SQL String cannot be empty