During project development, there are many multi-table nested queries. Today we will explain the use of multi-table queries in mybatis.
1. Preparation of database tables. This blog uses three tables (t_group (group information), t_person (person information), t_user (user information corresponding to personnel))
t_group table structure and test data
t_person table structure and test data
t_user table structure and test data
2. The entity class of the corresponding table
Case 1: There is a 1:1 relationship between personnel and their corresponding users
1)Group.java
public class Group { private int id; private String groupName; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
Person.java
public class Person { private int id; private String name; private String address; private String telphone; private String email; private int gid; private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelphone() { return telphone; } public void setTelphone(String telphone) { this.telphone = telphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
User.java
public class User { private int id; private String userName; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2) Create a new sql mapping file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zlt.mybatis.model.Group"> <!-- 1:1 query the user information corresponding to the person select p.id ,p.name,p.telphone,p.address,p.email,p.gid, u.id uid,u.user_name,u.password, u.pid from t_person p left join t_user u on u.pid = p.id where p.id = 1 --> <select id="findPersonByPID" parameterType="int" resultMap="PersonAndUser"> select p.id,p.name,p.telphone,p.address,p.email,p.gid, u.id uid,u.user_name,u.password, u.pid from t_person p left join t_user u on u.pid = p.id where p.id = #{id} </select> <!-- The first way to write --> <resultMap type="com.zlt.mybatis.model.Person" id="PersonAndUser"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="telphone" column="telphone" /> <result property="address" column="address"/> <result property="email" column="email"/> <result property="gid" column="gid"/> <association property="user" javaType="com.zlt.mybatis.model.User"> <id property="id" column="id"/> <result property="userName" column="user_name"/> <result property="password" column="password"/> </association> </resultMap> <!-- The second way of writing --> <!-- <resultMap type="com.zlt.mybatis.model.Person" id="person"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="telphone" column="telphone" /> <result property="address" column="address"/> <result property="email" column="email"/> <result property="gid" column="gid"/> </resultMap> <resultMap type="com.zlt.mybatis.model.Person" id="PersonAndUser" extends="person"> <association property="user" javaType="com.zlt.mybatis.model.User"> <id property="id" column="id"/> <result property="userName" column="user_name"/> <result property="password" column="password"/> </association> </resultMap> --> </mapper>
3) Register the sql mapping file in the mybatis configuration file
<mappers> <mapper resource="com/zlt/mybatis/mapping/GroupPersonUserMapper.xml" /> </mappers>
4) Unit testing
public void testPersonAndUserByPID(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); String statement = "com.zlt.mybatis.model.Group.findPersonByPID"; Person person = session.selectOne(statement, 3); if(null != person) { StringBuffer sb = new StringBuffer(); sb.append("id:" + person.getId() + "\t") .append("name:" + person.getName() +"\t") .append("telphone:" + person.getTelphone() +"\t") .append("email:" + person.getEmail() + "\t") .append("gid:" + person.getGid() + "\t"); User user = person.getUser(); sb.append("id:" + user.getId() + "\t") .append("userName:" + user.getUserName() + "\t") .append("password:" + user.getPassword() + "\t"); System.out.println(sb.toString()); } }finally{ if(null != session) session.close(); } }
5) Running result
id:3 name:王二 telphone:10001 email:[email protected] gid:6 id:3 userName:wanger password:123456
Note: This is to maintain user information from the perspective of personnel, and vice versa can also maintain personnel information from the perspective of users