MyBatis映射器(一)--多参数传递方式

参考自:https://www.cnblogs.com/hellowhy/p/9678245.html

在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个。当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名称了,mybatis中有以下四种

第一种:使用map传递

1⃣️定义接口

1 // 使用map传递多个参数进行查询
2    public List<Product> getByMap(Map<String, Object> paramMap);

2⃣️sql语句

1 <!--第一种:通过map传递 -->
2     <select id="getByMap" resultType="product" parameterType="map">
3         SELECT * FROM product
4         WHERE product_name LIKE concat('%',#{name},'%') AND
5         CAST(product_price AS INT) > #{price}
6     </select>

需要注意的有:

1、parameterType参数类型为map(此处使用别名)

2、参数名称是map中的key

3⃣️查询

复制代码
 1 /**
 2      * 通过map传递多个参数
 3      * 
 4      * @return
 5      */
 6     public void getProductsByMap() {
 7         System.out.println("使用map方式传递多个参数");
 8         List<Product> products = new ArrayList<>();
 9         Map<String, Object> 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<Product> getByAnnotation(@Param("name") String name, @Param("price") int price);

2⃣️定义sql

复制代码
1 <!--第二种:通过注解传递 -->
2     <select id="getByAnnotation" resultType="product">
3         SELECT * FROM product
4         WHERE product_name LIKE concat('%',#{name},'%') AND CAST(product_price
5         AS INT) >
6         #{price}
7     </select>
复制代码

这种方式不需要设置参数类型 ,参数名称为注解定义的名称

3⃣️查询

复制代码
 1 /**
 2      * 通过注解传递多个参数
 3      */
 4     public void getProductByAnnotation() {
 5         System.out.println("使用注解方式传递多个参数");
 6         List<Product> 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 class ParamBean {
 5     public String name;
 6     public int price;
 7 
 8     public ParamBean(String name, int price) {
 9         this.name = name;
10         this.price = price;
11     }
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public int getPrice() {
22         return price;
23     }
24 
25     public void setPrice(int price) {
26         this.price = price;
27     }
28 }
复制代码

2⃣️创建接口 

1 // 使用JavaBean传递多个参数进行查询
2     public List<Product> getByJavabean(ParamBean paramBean); 

3⃣️定义sql

1 <!--第三种:通过javabean传递 -->
2     <select id="getByJavabean" resultType="product" parameterType="paramBean">
3         SELECT * FROM product
4         WHERE product_name LIKE concat('%',#{name},'%')
5         AND CAST(product_price AS INT) > #{price}
6     </select>

需要注意的是:

1、参数类型parameterType为前面定义的javabean的全限定名或别名;

2、sql中的参数名称是javabean中定义的属性;

4⃣️查询

复制代码
 1 /**
 2      * 通过javabean传递多个参数
 3      */
 4     public void getProductByJavabean() {
 5         System.out.println("使用javabean方式传递多个参数");
 6         List<Product> 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  * 定义一个分页的javabean
 3  */
 4 public class PageParamBean {
 5     public int start;
 6     public int limit;
 7 
 8     public PageParamBean(int start, int limit) {
 9         super();
10         this.start = start;
11         this.limit = limit;
12     }
13 
14     public int getStart() {
15         return start;
16     }
17 
18     public void setStart(int start) {
19         this.start = start;
20     }
21 
22     public int getLimit() {
23         return limit;
24     }
25 
26     public void setLimit(int limit) {
27         this.limit = limit;
28     }
29 
30 }
复制代码

2⃣️创建接口

1 // 使用混合方式传递多个参数进行查询
2 public List<Product> getByMix(@Param("param") ParamBean paramBean, @Param("page") PageParamBean pageBean);

可以看出此处使用javabean+注解的方式传递参数

3⃣️定义sql 

复制代码
1 <!--第四种:混合方式传递 -->
2     <select id="getByMix" resultType="product">
3         SELECT * FROM product WHERE
4         product_name LIKE concat('%',#{param.name},'%') AND CAST(product_price
5         AS INT) >
6         #{param.price} LIMIT #{page.limit} OFFSET #{page.start}
7     </select>
复制代码

只要是注解方式,就不需要定义参数类型。

4⃣️查询

复制代码
 1 /**
 2      * 通过混合方式传递多个参数
 3      */
 4     public void getProductByMix() {
 5         System.out.println("使用混合方式传递多个参数");
 6         List<Product> 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元

 以上就是四种方式传递多个参数的实例。

猜你喜欢

转载自www.cnblogs.com/zhf123/p/12150664.html