在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个。当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名称了,mybatis中有以下四种
第一种:使用map传递
1⃣️定义接口
1 //使用map传递多个参数进行查询
2 public List getByMap(Map paramMap);
2⃣️sql语句
1
2
3 SELECT * FROM product4 WHERE product_name LIKE concat('%',#{name},'%') AND5 CAST(product_price AS INT) > #{price}6
需要注意的有:
1、parameterType参数类型为map(此处使用别名)
2、参数名称是map中的key
3⃣️查询
1 /**
2 * 通过map传递多个参数3 *4 *@return
5 */
6 public voidgetProductsByMap() {7 System.out.println("使用map方式传递多个参数");8 List products = new ArrayList<>();9 Map paramMap = new HashMap<>();10 paramMap.put("name", "恤");11 paramMap.put("price", 200);12 sqlSession =MybatisTool.getSqlSession();13 productMapper = sqlSession.getMapper(ProductMapper.class);14 products =productMapper.getByMap(paramMap);15 printResult(products);16 }
4⃣️查看结果
1 使用map方式传递多个参数2 T恤2的价格是230元3 T恤3的价格是270元4 T恤4的价格是270元
这种方式的缺点是:
1、map是一个键值对应的集合,使用者只有阅读了它的键才能知道其作用;
2、使用map不能限定其传递的数据类型,可读性差
所以一般不推荐使用这种方式。
第二种:使用注解传递
1⃣️创建接口
1 //使用注解传递多个参数进行查询
2 public List getByAnnotation(@Param("name") String name, @Param("price") int price);
2⃣️定义sql
1
2
3 SELECT * FROM product4 WHERE product_name LIKE concat('%',#{name},'%') AND CAST(product_price5 AS INT) >6 #{price}7
这种方式不需要设置参数类型 ,参数名称为注解定义的名称
3⃣️查询
1 /**
2 * 通过注解传递多个参数3 */
4 public voidgetProductByAnnotation() {5 System.out.println("使用注解方式传递多个参数");6 List products = new ArrayList<>();7 sqlSession =MybatisTool.getSqlSession();8 productMapper = sqlSession.getMapper(ProductMapper.class);9 products = productMapper.getByAnnotation("恤", 200);10 printResult(products);11 }
4⃣️查看结果
1 使用注解方式传递多个参数2 T恤2的价格是230元3 T恤3的价格是270元4 T恤4的价格是270元
这种方式能够大大提高可读性,但是只适合参数较少的情况,一般是少于5个用此方法,5个以上九要用其他方式了。
第三种:使用javabean传递
此中方式需要将传递的参数封装成一个javabean,然后将此javabean当作参数传递即可,为了方便,我这里只有两个参数封装javabean。
1⃣️参数封装成javabean
1 /**
2 * 定义一个Javabean用来传递参数3 */
4 public classParamBean {5 publicString name;6 public intprice;7
8 public ParamBean(String name, intprice) {9 this.name =name;10 this.price =price;11 }12
13 publicString getName() {14 returnname;15 }16
17 public voidsetName(String name) {18 this.name =name;19 }20
21 public intgetPrice() {22 returnprice;23 }24
25 public void setPrice(intprice) {26 this.price =price;27 }28 }
2⃣️创建接口
1 //使用JavaBean传递多个参数进行查询
2 public List getByJavabean(ParamBean paramBean);
3⃣️定义sql
1
2
3 SELECT * FROM product4 WHERE product_name LIKE concat('%',#{name},'%')5 AND CAST(product_price AS INT) > #{price}6
需要注意的是:
1、参数类型parameterType为前面定义的javabean的全限定名或别名;
2、sql中的参数名称是javabean中定义的属性;
4⃣️查询
1 /**
2 * 通过javabean传递多个参数3 */
4 public voidgetProductByJavabean() {5 System.out.println("使用javabean方式传递多个参数");6 List products = new ArrayList<>();7 sqlSession =MybatisTool.getSqlSession();8 productMapper = sqlSession.getMapper(ProductMapper.class);9 ParamBean paramBean = new ParamBean("恤", 200);10 products =productMapper.getByJavabean(paramBean);11 printResult(products);12 }
5⃣️查看结果
1 使用javabean方式传递多个参数2 T恤2的价格是230元3 T恤3的价格是270元4 T恤4的价格是270元
这种方式在参数多于5个的情况下比较实用。
第四种:使用混合方式传递
假设我要进行分页查询,那么我可以将分页参数单独封装成一个javabean进行传递,其他参数封装成上面的javabean,然后用注解传递这两个javabean,并在sql中获取。
1⃣️封装分页参数javabean
1 /*
2 * 定义一个分页的javabean3 */
4 public classPageParamBean {5 public intstart;6 public intlimit;7
8 public PageParamBean(int start, intlimit) {9 super();10 this.start =start;11 this.limit =limit;12 }13
14 public intgetStart() {15 returnstart;16 }17
18 public void setStart(intstart) {19 this.start =start;20 }21
22 public intgetLimit() {23 returnlimit;24 }25
26 public void setLimit(intlimit) {27 this.limit =limit;28 }29
30 }
2⃣️创建接口
1 //使用混合方式传递多个参数进行查询
2 public List getByMix(@Param("param") ParamBean paramBean, @Param("page") PageParamBean pageBean);
可以看出此处使用javabean+注解的方式传递参数
3⃣️定义sql
1
2
3 SELECT * FROM product WHERE4 product_name LIKE concat('%',#{param.name},'%') AND CAST(product_price5 AS INT) >6 #{param.price} LIMIT #{page.limit} OFFSET #{page.start}7
只要是注解方式,就不需要定义参数类型。
4⃣️查询
1 /**
2 * 通过混合方式传递多个参数3 */
4 public voidgetProductByMix() {5 System.out.println("使用混合方式传递多个参数");6 List products = new ArrayList<>();7 sqlSession =MybatisTool.getSqlSession();8 productMapper = sqlSession.getMapper(ProductMapper.class);9 ParamBean paramBean = new ParamBean("恤", 200);10 PageParamBean pageBean = new PageParamBean(0, 5);11 products =productMapper.getByMix(paramBean, pageBean);12 printResult(products);
5⃣️查看结果
1 使用混合方式传递多个参数2 T恤2的价格是230元3 T恤3的价格是270元4 T恤4的价格是270元
以上就是四种方式传递多个参数的实例。