您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
13-参数传递arg,param,List,Array,Map(mapper接口的param注解与map的原理)
发布时间:2024-11-22 15:54:55编辑:雪饮阅读()
-
arg
mybatis的mapper接口中抽象方法的参数如果是单个参数的时候其类型有
POJO类型,Map集合,Collection,List,Array、其它类型(直接用)
而当多个参数时候就会封装为Map集合。
第一个参数可以用key为arg0或key为param1来访问
第二个参数可以用key为arg1或key为param2来访问
以此类推。
这是你在不用注解的情况下。
所以mapper配置文件中新增如
<insert id="add2">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values (#{arg0},#{arg1},#{arg2},#{arg3},#{arg4})
</insert>
然后mapper接口方法如
void add2(String brandName,String companyName,int ordered,String description,int status);
于是实现的测试方法
@Test
public void testAdd2() throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession(true);
BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
brandMapper.add2("松鼠","松鼠",1,"松鼠",1);
sqlSession.close();
}
那么你还可以注解与arg混用
void add2(@Param("brandName") String brandName,String companyName,int ordered,String description,int status);
当你注解了一个参数,则对应的arg参数名就会被替换,例如这里用brandName替换了arg0,所以此时如果在mapper配置文件中该方法对应id的标签中使用arg0就会报错的。不存在该属性了。
所以此时该标签也调用修改为
<insert id="add2">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values (#{brandName},#{arg1},#{arg2},#{arg3},#{arg4})
</insert>
param
但param1则仍旧保留,还是可用的。
param1保持不变
<insert id="add2">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values (#{brandName},#{param1},#{arg2},#{arg3},#{arg4})
</insert>
而且param不是从0开始,而是从1开始。
Collection
如果是Collection类型的,那么仍然是map集合,只不过只有两个key,一个是arg0,存储的就是collection集合,还有一个是collection这个key,也是存储的collection集合。
所以假如接口定义是这样
void add3(Collection collection);
那么mapper配置文件中则可以是这样
<insert id="add3">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values
<foreach collection="arg0" item="val" separator="," open="(" close=")">
#{val}
</foreach>
</insert>
那么测试方法可以是这样
@Test
public void testAdd3() throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession(true);
BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
HashSet hs=new HashSet();
hs.add("brand_field_1");
hs.add("brand_field_2");
hs.add(1);
hs.add("brand_field_4");
hs.add(1);
brandMapper.add3(hs);
sqlSession.close();
}
但请记住这个HashSet是无序的,且我们这个字段的类型必须对应上,你循环的时候并不能保证正好循环的是int对应int的字段呢。而且HashSet是无序且不支持像是通过index这种索引去取的。所以这里只是伪代码。
当然foreach在mapper配置文件中是支持index的,只不过对于无序集合来说,也是很鸡肋
<insert id="add3">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values (
<foreach collection="arg0" item="val" index="i">
<if test="i == 4">
${val},
</if>
</foreach>
<foreach collection="arg0" item="val" index="i">
<if test="i == 3">
${val},
</if>
</foreach>
<foreach collection="arg0" item="val" index="i">
<if test="i == 2">
${val},
</if>
</foreach>
<foreach collection="arg0" item="val" index="i">
<if test="i == 1">
${val},
</if>
</foreach>
<foreach collection="arg0" item="val" index="i">
<if test="i == 0">
${val}
</if>
</foreach>
)
</insert>
List
那么当接口方法参数类型为List时候则是key为arg0,值为list集合
key为collection,值为list集合,key为list,值为list集合
那么接口方法实现如
void add3(List list);
测试方法实现如
@Test
public void testAdd3() throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession(true);
BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
List<String> list=new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
brandMapper.add3(list);
sqlSession.close();
}
映射配置文件中实现如
<insert id="add3">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values
<foreach collection="arg0" item="val" index="i" separator="," open="(" close=")">
${val}
</foreach>
</insert>
可以看到这里我是利用所有字段值都用字符类型的1去伪装,这样就很方便插入,如果类型是int或者char都可以。同样也算是上面的HashSet的解决方案吧。
Array
对于Array类型则是封装为Map集合,key为arg0,值为数组,key为array,值为数组。
这里可要小心,Array就是类似这样int[],而不需要import其它包的,我差点就忘了,以为类似import java.util.List;这样,还有一个import java.util.Array。
Map
而Map类型的则是直接可以用
接口方法定义如
void add4(Map map);
调用接口方法如
Map map=new HashMap();
map.put("brandName","1");
map.put("companyName","1");
map.put("ordered","1");
map.put("description","1");
map.put("status","1");
brandMapper.add4(map);
mapper的映射配置文件如
<insert id="add4">
insert into tb_brand (brand_name,company_name,ordered,description,status)
values (#{brandName},#{companyName},#{ordered},#{description},#{status})
</insert>
但有种意外情况就是map也可能是这样定义呢?
Map map=new HashMap();
Brand brand=new Brand();
brand.setBrandName("1");
brand.setCompanyName("1");
brand.setStatus(1);
brand.setDescription("1");
brand.setOrdered(1);
map.put("map",map);
可以思考下。
关键字词:arg,param,List,Array,Map,mapper
相关文章
- 09-查询-条件查询(散装参数,POJO对象参数,Map参数)
- 08-查询-查看详情(mybatis的mapper接收参数的处理)
- 07-查询-查询所有&结果映射(POJO驼峰属性与数据库字段
- 05-Mybatis核心配置文件(开发与测试环境environment的
- 04-Mapper代理开发(IntelliJ IDEA拷贝路径的小技巧,ma
- magento2 模板设计 二(margin)
- 38、存储 configmap(2)
- 37、存储 configmap(1)
- pushgateway搭建并为prometheus增加targets
- 14_Jedis_操作list(lpush,rpush,lpop,rpop,del)