JAVA framework Mybaits one-to-one, one-to-many

One: Explanation

In our daily operations, we often encounter multi-table joint queries. Due to the unreasonable reference objects, there will be one-to-one and one-to-many situations. For example: account information and order table, from the perspective of the order table and account information are one-to-one (one order can only be one user), from the user's point of view, there will be a one-to-many situation (one user There will be multiple orders).

Two, one-on-one:

Need to be clear:

Now we create 2 tables:

 1         CREATE TABLE username(
 2             id INT PRIMARY KEY AUTO_INCREMENT,
 3             NAME VARCHAR(20),
 4             sex VARCHAR(20)
 5         );
 6 
 7         CREATE TABLE orders (
 8             id INT PRIMARY KEY AUTO_INCREMENT,
 9             num VARCHAR(20),
10             user_id INT
11         );

 

Create an order table and a user table. Insert some values.

Reference is: order

Union query sql:

1 SELECT o.`id`AS oid ,o.`num` AS num ,o.`user_id` AS uid,u.* FROM orders AS o, username AS u WHERE o.user_id=u.id;

result:

The first method:

First map the class:

The orders class:

 1 package jd.com.ou;
 2 
 3 public class orders {
 4     private String num;
 5     private Integer user_id;
 6 
 7     public void setNum(String num) {
 8         this.num = num;
 9     }
10 
11     public void setUser_id(Integer user_id) {
12         this.user_id = user_id;
13     }
14 
15     public Integer getUser_id() {
16         return user_id;
17     }
18 
19     public String getNum() {
20         return num;
21     }
22 }

 

user class:

 1 package jd.com.ou;
 2 
 3 public class user {
 4     private String name;
 5     private String sex;
 6 
 7     public void setName(String name) {
 8         this.name = name;
 9     }
10 
11     public void setSex(String sex) {
12         this.sex = sex;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public String getSex() {
20         return sex;
21     }
22 }

 

Note: It is not necessary to create these two tables for joint query, but these two classes are the projo classes of the user table and the ordes table.

Then we define the projo class that returns the data:

 1 package jd.com.ou;
 2 
 3 public class customuo extends orders {
 4     private String name;
 5     private String sex;
 6 
 7     public void setSex(String sex) {
 8         this.sex = sex;
 9     }
10 
11     public void setName(String name) {
12         this.name = name;
13     }
14 
15     public String getSex() {
16         return sex;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     @Override
24     public String toString() {
25         return this.name+this.sex+this.getNum();
26     }
27 }

 

mapper configuration file:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="jd.com.ou.oumapper">
6     <select id="findOrderAndUser" resultType="jd.com.ou.customuo" parameterType="jd.com.ou.customuo">
7         SELECT o.`id`AS oid ,o.`num` AS num ,o.`user_id` AS uid,u.* FROM orders AS o, username AS u WHERE o.`user_id`=u.id;
8     </select>
9 </mapper>

 

Note that in the sql statement, in the where keyword, the alias of the column is defined earlier, and the alias of the column cannot be used in the expression of where.

mapper interface:

1 package jd.com.ou;
2 
3 import java.util.List;
4 
5 public interface oumapper {
6     List<customuo> findOrderAndUser();
7 }

 

Test class:

 1 package jd.com.ou;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 import org.junit.jupiter.api.Test;
 8 
 9 import java.io.IOException;
10 import java.io.InputStream;
11 
12 import java.util.List;
13 
14 public class testDemo {
15 
16     @Test
17     public  void  testDemo() throws IOException {
18         String reource="SqlMapConfig.xml";
19         InputStream inp= Resources.getResourceAsStream(reource);
20         SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inp);
21         SqlSession sqlSession=sqlSessionFactory.openSession();
22         oumapper oum=sqlSession.getMapper(oumapper.class);
23         List<customuo> list=oum.findOrderAndUser();
24         System.out.println(list);
25     }
26 }

 

Notice:

    Here we define the customuo class that receives the return value, and we do not set the user field to the customuo attribute. why? ?

Because we do not pass in parameters, that is, we cannot set the user class to the properties of customuo, so that if we call the user property of customuo, it will be the default value of the object: null.

So what if we want to use the user field?

The second method:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966243&siteId=291194637