Mybatis实现连接查询和分段查询以及association和collection的运用
作者:互联网
首先我们创建我们的数据库实现表与表之间的关系
id dept_name
------ -------------
1 开发部
2 市场部
3 客服部
4 xxxx部门
5 yyyyy部门
6 ttt部门
这是我们数据库表中部门和员工信息,其中deptid作为外键连接表dept中的ID主键。
id name sex email deptid ye version
------ ------------------------- ------ -------------- ------ ------ ---------
1 张三 女 qqq@qq.com 2 290 7
2 tom 女 tom@126.com 1 100 1
3 jerry 男 jerry@126.com 2 100 1
4 张三 女 张三@126.com 2 100 1
6 李四 男 lisi@126.com 1 100 1
7 王五1 男 lisi@126.com 3 100 1
8 刘德华 男 ldh@126.com 3 100 1
10 李晓红 女 lisi@126.com 3 100 1
11 李晓红2 女 lisi@126.com 1 100 1
12 李晓红3 女 lisi@126.com 2 100 1
15 王璐璐 男 wlulu@126.com 1 100 1
16 王璐璐1 男 wlulu@126.com 1 100 1
17 刘亮亮1 男 liull@126.com 1 100 1
18 刘亮亮1 男 liull@126.com 1 100 1
19 刘亮亮8 男 liull@126.com 1 100 1
20 刘亮亮1 男 liull@126.com 1 100 1
21 刘亮亮1 男 liull@126.com 1 100 1
22 刘亮亮1 男 liull@126.com 1 100 1
23 刘亮亮1 男 liull@126.com 1 100 1
24 jim 男 hehe@126.com 1 100 1
25 jim222 男 hehe@126.com 1 100 1
26 jim333 男 hehe@126.com 1 100 1
27 jim444 男 hehe@126.com 1 100 1
28 李晓辉 男 lxh@126.com 1 100 1
29 jim333 男 jim@126.com 1 100 1
30 周润发 男 zrf@126.com 1 100 1
31 jim 男 jim@126.com 1 100 1
32 周润发 男 zrf@126.com 1 100 1
33 周润发 男 zrf@126.com 1 100 1
34 周润发 男 zrf@126.com 1 100 1
35 周润发 男 zrf@126.com 1 100 1
36 jim 男 jim@126.com 1 100 1
37 jim 男 jim@126.com 1 100 1
38 jim 男 jim@126.com 1 100 1
39 jim 男 jim@126.com 1 100 1
40 jim 男 jim@126.com 1 100 1
41 jim 男 jim@126.com 1 100 1
42 jim 男 jim@126.com 1 100 1
43 jim 男 jim@126.com 1 100 1
44 jim 男 jim@126.com 1 100 1
45 jim 男 jim@126.com 1 100 1
46 jim 男 jim@126.com 1 (NULL) 1
47 !¥#%……&%……¥ 男 jim@126.com 1 (NULL) 1
48 jim 男 jim@126.com 1 (NULL) 1
49 jim 男 jim@126.com 1 (NULL) 1
50 周润发 男 jim@126.com 1 (NULL) 1
51 jim 男 jim@126.com 1 (NULL) 1
52 jim 男 jim@126.com 1 (NULL) 1
53 jim 男 jim@126.com 1 (NULL) 1
54 周润发 男 zrf@126.com 1 (NULL) 1
55 周润发 男 zrf@126.com 1 (NULL) 1
56 周润发 男 zrf@126.com 1 (NULL) 1
57 jim 男 jim@126.com 1 (NULL) 1
58 jim 男 jim@126.com 1 (NULL) 1
59 jim 男 jim@126.com 1 (NULL) 1
60 周润发 男 zrf@126.com 1 (NULL) 1
61 周润发 男 zrf@126.com 1 (NULL) 1
62 周润发 男 zrf@126.com 1 (NULL) 1
63 周润发 男 zrf@126.com 1 (NULL) 1
64 周润发 男 zrf@126.com 1 (NULL) 1
65 周润发 男 zrf@126.com 1 (NULL) 1
66 周润发 男 zrf@126.com 1 (NULL) 1
其二是我们mybatis_config.xml的配置文件,其中用到Setting配置信息来设置延迟加载。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting value="true" name="cacheEnabled"/>
<setting value="true" name="lazyLoadingEnabled"/>
<setting value="false" name="aggressiveLazyLoading"/>
<!--开启二级缓存-->
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.mapper"/>
</mappers>
</configuration>
第三是我们持久化对象的类,以及set,get方法。
package com.po;
public class UserInfo {
private int id;
private String uname;//实体类的属性名 和 表的字段名称 不一样
private String sex;
private String email;
private int deptid;
private Dept dept;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", uname='" + uname + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
", deptid=" + deptid +
", dept=" + dept +
'}';
}
}
package com.po;
import java.util.List;
public class Dept {
private int id;
private String dept_name;
private List<UserInfo> userinfo;
public List<UserInfo> getUserinfo() {
return userinfo;
}
public void setUserinfo(List<UserInfo> userinfo) {
this.userinfo = userinfo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDept_name() {
return dept_name;
}
public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}
@Override
public String toString() {
return "Dept [id=" + id + ", dept_name=" + dept_name + ", userinfo=" + userinfo + "]";
}
}
最后就是实现我们mapper映射的关系配置。
<?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.mapper.UserMapper">
<select resultType="com.po.UserInfo" id="selectAllStudent"> select id,name as uname,sex,email,deptid from userinfo </select>
<resultMap id="userinfomap1" type="com.po.UserInfo">
<id property="id" column="id"/>
<result property="uname" column="name"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="deptid" column="deptid"/>
</resultMap>
<select resultType="com.po.UserInfo" id="selectAllStudent2" resultMap="userinfomap1"> select id,name ,sex,email,deptid from userinfo </select>
<resultMap id="userinfomap2" type="com.po.UserInfo">
<id property="id" column="id"/>
<result property="uname" column="name"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="dept.id" column="did"/>
<result property="dept.dept_name" column="dname"/>
</resultMap>
<select id="selectUserAndDept" resultMap="userinfomap2"> SELECT u.*, d.id AS did, d.dept_name AS dname FROM userinfo u INNER JOIN dept d ON u.deptid=d.id </select>
<!--分段查询 -->
<resultMap id="userinfomap3" type="com.po.UserInfo">
<id property="id" column="id"/>
<result property="uname" column="name"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" column="deptid" select="com.mapper.DeptMapper.selectDeptById"> </association>
</resultMap>
<!-- 可以使用延迟加载(懒加载)(按需加载) 我们每次查询User对象的时候,都将一起查询出来 部门信息在我们使用的时候再去查询 分段查询的基础之上加上两个配置 <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> -->
<select id="selectUserAndDeptSetp" resultMap="userinfomap3"> SELECT id ,name as uname, sex, email , deptid FROM userinfo </select>
<select resultType="com.po.UserInfo" id="getUserByDeptId"> select id,name as uname,sex,email,deptid from userinfo where deptid = #{id} </select>
</mapper>
<?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.mapper.DeptMapper">
<select resultType="com.po.Dept" id="selectDeptById">
select id,dept_name from dept where id=#{id}
</select>
<resultMap id="deptandusermap1" type="com.po.Dept">
<id column="did" property="id"/>
<result column="dname" property="dept_name"/>
<collection property="userInfo" ofType="com.po.UserInfo">
<id column="id" property="uname"/>
<result column="name" property="uname"/>
<result column="sex" property="sex"/>
<result column="email" property="email"/>
</collection>
</resultMap>
<select id="selectDeptAndUsers" resultMap="deptandusermap1">
SELECT u.id ,u.name, u.sex, u.email , d.id AS did, d.dept_name AS dname FROM userinfo u INNER JOIN dept d ON u.deptid=d.id
</select>
<!--分段查询-->
<resultMap id="deptandusermap2" type="com.po.Dept">
<id column="id" property="id"/>
<result column="dept_name" property="dept_name"/>
<collection column="id" property="userInfo" select="com.mapper.UserMapper.getUserByDeptId"/>
</resultMap>
<select id="selectDeptAndUserStep" resultMap="deptandusermap2"> SELECT id,dept_name from dept </select>
</mapper>
最后实现我们的测试
package com.test;
import Utils.*;
import com.po.Dept;
import com.po.UserInfo;
import com.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class test {
public void test1() {
SqlSession session = MybatisUtils.getSession();
List<UserInfo> lists = session.getMapper(UserMapper.class).selectAllStudent();
for (int i = 0 ; i < lists.size() ; i++){
UserInfo userInfo = lists.get(i);
System.out.println(userInfo);
}
session.close();
}
public void test2() {
SqlSession session = MybatisUtils.getSession();
List<UserInfo> lists = session.getMapper(UserMapper.class).selectAllStudent2();
for (int i = 0 ; i < lists.size() ; i++){
UserInfo userInfo = lists.get(i);
System.out.println(userInfo);
}
session.close();
}
public void test3() {
SqlSession session = MybatisUtils.getSession();
List<UserInfo> lists = session.getMapper(UserMapper.class).selectUserAndDept();
for (int i = 0 ; i < lists.size() ; i++){
UserInfo u = lists.get(i);
//System.out.println(u.getId() + "\t" + u.getUname() + "\t" + u.getSex() + "\t" + u.getEmail() + "\t" + u.getDept().getId() + "\t" + u.getDept().getDept_name());
System.out.println(u.getId() + "\t" + u.getUname() + "\t" + u.getSex() + "\t" + u.getEmail() );
}
session.close();
}
//测试延迟加载
public void test4() {
SqlSession session = MybatisUtils.getSession();
List<UserInfo> lists = session.getMapper(UserMapper.class).selectUserAndDeptSetp();
for (int i = 0 ; i < lists.size() ; i++){
UserInfo u = lists.get(i);
Dept dept = u.getDept();
System.out.println(u.getId() + "\t" + u.getUname() + "\t" + u.getSex() + "\t" + u.getEmail() + dept.getDept_name());
}
session.close();
}
public static void main(String[] args) {
test t = new test();
// t.test1();
//t.test2();
//t.test3();
t.test4();
}
}
import com.po.Dept;
import com.po.UserInfo;
import com.mapper.DeptMapper;
import com.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import Utils.*;
import java.util.List;
public class test02 {
public void test2() {
SqlSession session = MybatisUtils.getSession();
List<Dept> lists = session.getMapper(DeptMapper.class).selectDeptAndUsers();
for (int i = 0 ; i < lists.size() ; i++){
Dept dept = lists.get(i);
List<UserInfo> users = dept.getUserinfo();
//System.out.println(dept.getId() + "\t" + dept.getDept_name());
for (UserInfo u : users) {
System.out.println(dept.getId() + "\t" + dept.getDept_name() + "\t" + u.getId()+"\t"+u.getUname() + "\t" + u.getEmail()+"\t"+u.getSex());
}
}
session.close();
}
public void test3() {
SqlSession session = MybatisUtils.getSession();
List<Dept> lists = session.getMapper(DeptMapper.class).selectDeptAndUserStep();
for (int i = 0 ; i < lists.size() ; i++){
Dept dept = lists.get(i);
List<UserInfo> users = dept.getUserinfo();
//System.out.println(dept.getId() + "\t" + dept.getDept_name());
for (UserInfo u : users) {
System.out.println(dept.getId() + "\t" + dept.getDept_name() + "\t" + u.getId()+"\t"+u.getUname() + "\t" + u.getEmail()+"\t"+u.getSex());
}
}
session.close();
}
public static void main(String[] args) {
test t = new test();
//t.test2();
t.test3();
}
}
标签:jim,collection,查询,dept,126,Mybatis,100,com,public 来源: https://blog.csdn.net/qq_45169180/article/details/117559441