MyBatis mapper (a) - a multi-parameter passing

Reference from: https://www.cnblogs.com/hellowhy/p/9678245.html

 

In mybatis mapper interface, typically you need to pass some parameters query as a query, sometimes one, sometimes more. When only one parameter, as long as we can use the parameter name in the sql interface, but if it is more, it can not directly use the parameter name, mybatis in the following four

The first: use the map delivery

1⃣️ defined interfaces

1 // Use the map to pass multiple parameters to query
2    public List<Product> getByMap(Map<String, Object> paramMap);

2⃣️sql statement

1 <- first:! Map transmitted by ->
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>

It should be noted are:

1, parameterType type parameter map (alias used here)

2, parameter names are in the key map

3⃣️ inquiry

Copy the code
 1 /**
 2 * pass through the plurality of map parameters
 3      * 
 4      * @return
 5      */
 6     public void getProductsByMap() {
 7 System.out.println ( "pass manner using a plurality of parameter 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     }
Copy the code

4⃣️ View Results

A map using a plurality of parameters passed by
2 T-Shirt 2 is 230 yuan
3 3 T-Shirt was 270 yuan
4 4 T-Shirt was 270 yuan

The disadvantage of this approach are:

1, map is a key value of the collection, users only read it to know its key role;

2, can not use the map data define the type of transfer, poor readability

It is generally not recommended to use this way.

The second: use annotations transfer

1⃣️ create an interface

1 // use annotations to pass multiple parameters to query
2     public List<Product> getByAnnotation(@Param("name") String name, @Param("price") int price);

2⃣️ defined sql

Copy the code
1 <- The second:! By passing notes ->
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>
Copy the code

In this manner not need to set the parameter type, the parameter name is defined name annotation

3⃣️ inquiry

Copy the code
 1 /**
 2 * pass through the plurality of parameters annotations
 3      */
 4     public void getProductByAnnotation() {
 5 System.out.println ( "Annotation mode using a plurality of transmission parameters");
 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     }
Copy the code

4⃣️ View Results

1 using a plurality of parameters passed by annotations
2 T-Shirt 2 is 230 yuan
3 3 T-Shirt was 270 yuan
4 4 T-Shirt was 270 yuan

This approach can greatly improve the readability, but only for the case of fewer parameters, typically less than 5 using this method, more than five nine other ways to use the.

Third: Use transfer javabean

Parameters of which way you need to pass a package to javabean, javabean then this can be passed as an argument, for convenience, I am here only two parameters package javabean.

1⃣️ parameters encapsulated javabean

Copy the code
 1 /**
 * 2 defines the parameters used to pass a 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 }
Copy the code

2⃣️ create an interface 

1 // use JavaBean to pass multiple parameters to query
2     public List<Product> getByJavabean(ParamBean paramBean); 

3⃣️ defined sql

1 <- Third:! Javabean transmitted by ->
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>

have to be aware of is:

1, the parameter type as javabean parameterType fully qualified name or alias defined above;

2, the parameter name is sql javabean defined attributes;

4⃣️ inquiry

Copy the code
 1 /**
 2 * pass through the plurality of parameters javabean
 3      */
 4     public void getProductByJavabean() {
 5 System.out.println ( "transmitting a plurality of parameters javabean mode");
 6         List<Product> products = new ArrayList<>();
 7         sqlSession = MybatisTool.getSqlSession();
 8         productMapper = sqlSession.getMapper(ProductMapper.class);
 9 ParamBean paramBean = new ParamBean ( "shirt", 200);
10         products = productMapper.getByJavabean(paramBean);
11 printResult (products);
12     }
Copy the code

5⃣️ View Results

1 using a plurality of parameters passed by javabean
2 T-Shirt 2 is 230 yuan
3 3 T-Shirt was 270 yuan
4 4 T-Shirt was 270 yuan 

This embodiment is more practical in the case where more than five parameters.

Fourth: a mixed mode delivery

Suppose I paging query, then I can be packaged into a single paging parameters javabean passed, packaged into other parameters javabean above, and passing these two javabean with annotations, and acquires the sql.

1⃣️ package paging parameters javabean

Copy the code
 1 /*
 2 * Define a paging 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 }
Copy the code

2⃣️ create an interface

// pass manner using a plurality of mixing parameters query
2 public List<Product> getByMix(@Param("param") ParamBean paramBean, @Param("page") PageParamBean pageBean);

It can be seen here javabean + annotations using transmission parameters manner

3⃣️ defined sql 

Copy the code
1 <- Fourth:! Passed by mixing ->
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>
Copy the code

As long as the way is a comment, you need to define the parameter type.

4⃣️ inquiry

Copy the code
 1 /**
 * 2 by passing a mixed manner a plurality of parameters
 3      */
 4     public void getProductByMix() {
 5 System.out.println ( "mixed mode multiple transmission parameter");
 6         List<Product> products = new ArrayList<>();
 7         sqlSession = MybatisTool.getSqlSession();
 8         productMapper = sqlSession.getMapper(ProductMapper.class);
 9 ParamBean paramBean = new ParamBean ( "shirt", 200);
10         PageParamBean pageBean = new PageParamBean(0, 5);
11         products = productMapper.getByMix(paramBean, pageBean);
12 printResult (products);
Copy the code

5⃣️ View Results

A mixed manner a plurality of transmission parameters
2 T-Shirt 2 is 230 yuan
3 3 T-Shirt was 270 yuan
4 4 T-Shirt was 270 yuan

 These are examples of a plurality of four ways to pass parameters.

Guess you like

Origin www.cnblogs.com/zhf123/p/12150664.html