前面几篇博客我们用mybatis能对单表进行增删改查操作了,也能用动态SQL书写比较复杂的sql语句。但是在实际开发中,我们做项目不可能只是单表操作,往往会涉及到多张表之间的关联操作。那么我们如何用 mybatis 处理多表之间的关联操作呢?请看本篇博客详解。
我们以用户表 user 和订单表 orders 为例。设定一个订单只能由一个 用户创建,那么由订单到用户就是一对一的关系。
①、创建用户表 user 和订单表 orders
用户表 user
订单表 orders
②、创建项目工程,导入相应的 jar 包
③、创建实体类
User.java
package com.ys.po;
public class User {
//用户ID
private int id;
//用户姓名
private String username;
//用户性别
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ "]";
}
}
Orders.java
package com.ys.po;
public class Orders {
//订单ID
private int id;
//用户ID
private int userId;
//订单数量
private String number;
//和用户表构成一对一的关系,即一个订单只能由一个用户创建
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Orders [id=" + id + ", userId=" + userId + ", number=" + number
+ ", user=" + user + "]";
}
}
④、创建 OrderMapper 接口和 OrderMapper.xml 文件
由于我们采用 Mapper 代理加载 xxxMapper.xml 文件,这里我们重复一下 Mapper 代理所需的条件,接口和xml文件必须满足以下几个条件:
1、接口必须要和 xml 文件同名且在同一个包下,也就是说 xml 文件中的namespace是接口的全类名
2、接口中的方法名和xml 文件中定义的 id 一致
3、接口输入参数类型要和xml 中定义的 parameterType 一致
4、接口返回数据类型要和xml 中定义的 resultType 一致
详细介绍参考上一篇博客:http://www.cnblogs.com/ysocean/p/7301548.html
OrderMapper 接口
package one.to.one.mapper;
import com.ys.po.Orders;
import com.ys.po.User;
public interface OrdersMapper {
/**
* 方式一:嵌套结果
* select * from orders o,user u where o.user_id=u.id and o.id=#{id}
* @param orderId
* @return
*/
//根据订单ID查询订单和用户信息
public Orders selectOrderAndUserByOrderID(int orderId);
/**
* 方式二:嵌套查询
* select * from order WHERE id=1;//得到user_id
* select * from user WHERE id=1 //1 是上一个查询得到的user_id的值
* @param userID
* @return
*/
//根据订单ID得到订单信息(包含user_id)
public Orders getOrderByOrderId(int orderId);
//根据用户ID查询用户信息
public User getUserByUserId(int userID);
}
OrderMapper .xml文件
"1.0" encoding="UTF-8" ?>
mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"one.to.one.mapper.OrdersMapper">"selectOrderAndUserByOrderID" resultMap="getOrderAndUser">
select * from orders o,user u where o.user_id=u.id and o.id=#{id}type="com.ys.po.Orders" id="getOrderAndUser">"id" property="id"/>"user_id" property="userId"/>"number" property="number"/>"user" javaType="com.ys.po.User">"id" property="id"/>"username" property="username"/>"sex" property="sex"/>"getOrderByOrderId" resultMap="getOrderMap">
select * from order where id=#{id}type="com.ys.po.Orders" id="getOrderMap">"id" property="id"/>"number" property="number"/>"userId" column="id" select="getUserByUserId">"getUserByUserId" resultType="com.ys.po.User">
select * from user where id=#{id}
⑤、向 mybatis-configuration.xml 配置文件中注册 OrderMapper.xml 文件
"1.0" encoding="UTF-8"?>
configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
"db.properties">"com.ys.po"/>"development">"development">type="JDBC"/>type="POOLED">"driver" value="${jdbc.driver}"/>"url" value="${jdbc.url}"/>"username" value="${jdbc.username}"/>"password" value="${jdbc.password}"/>"one.to.one.mapper.OrdersMapper"/>
⑥、测试
package one.to.one.mapper;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.ys.po.Orders;
public class OneToOneTest {
//定义 SqlSession
SqlSession session =null;
@Before
public void init(){
//定义mybatis全局配置文件
String resource = "mybatis-configuration.xml";
//加载 mybatis 全局配置文件
InputStream inputStream = OneToOneTest.class.getClassLoader()
.getResourceAsStream(resource);
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//根据 sqlSessionFactory 产生 session
session = sessionFactory.openSession();
}
/**
* 方式一:嵌套结果
* select * from orders o,user u where o.user_id=u.id and o.id=#{id}
*/
@Test
public void testSelectOrderAndUserByOrderId(){
String statement = "one.to.one.mapper.OrdersMapper.selectOrderAndUserByOrderID";
//创建OrdersMapper对象,mybatis自动生成mapepr代理对象
OrdersMapper orderMapper = session.getMapper(OrdersMapper.class);
Orders order = orderMapper.selectOrderAndUserByOrderID(1);
System.out.println(order);
session.close();
}
/**
* 方式二:嵌套查询
* select * from order WHERE id=1;//得到user_id
* select * from user WHERE id=1 //1 是上一个查询得到的user_id的值
*/
@Test
public void testgetOrderByOrderId(){
String statement = "one.to.one.mapper.OrdersMapper.getOrderByOrderId";
//创建OrdersMapper对象,mybatis自动生成mapepr代理对象
OrdersMapper orderMapper = session.getMapper(OrdersMapper.class);
Orders order = orderMapper.selectOrderAndUserByOrderID(1);
System.out.println(order);
session.close();
}
}
还是以用户表 user 和 订单表 orders 为例,一个用户能创建多个订单。故用户和订单构成一对多的关联。
我们在 user.java 中添加一个属性 public Listorders;
①、创建实体类
user.java如下,orders.java保持不变
package com.ys.po;
import java.util.List;
public class User {
//用户ID
private int id;
//用户姓名
private String username;
//用户性别
private String sex;
//一个用户能创建多个订单,用户和订单构成一对多的关系
public List orders;
public List getOrders() {return orders;
}
public void setOrders(List orders) {
this.orders = orders;
}
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 getSex() {return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ "]";
}
}
②、创建 UserMapper 接口和 UserMapper.xml 文件
UserMapper接口
package one.to.many.mapper;
import com.ys.po.User;
public interface UserMapper {
//根据用户id查询用户信息,以及用户下面的所有订单信息
public User selectUserAndOrdersByUserId(int UserId);
}
UserMapper.xml
"1.0" encoding="UTF-8" ?>
mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"one.to.many.mapper.UserMapper">"selectUserAndOrdersByUserId" resultMap="getUserAndOrders">
select u.*,o.id oid,o.number number from user u,orders o where u.id=o.user_id and u.id=#{id}type="com.ys.po.User" id="getUserAndOrders">"id" property="id"/>"username" property="username"/>"sex" property="sex"/>"orders" ofType="com.ys.po.Orders">"oid" property="id"/>"number" property="number"/>
③、向 mybatis-configuration.xml 配置文件中注册 UserMapper.xml 文件
④、测试
@Test
public void testSelectOrderAndUserByOrderId(){
String statement = "one.to.many.mapper.UserMapper.selectUserAndOrdersByUserId";
//创建OrdersMapper对象,mybatis自动生成mapepr代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectUserAndOrdersByUserId(1);
System.out.println(user.getOrders().size());
session.close();
}
这里我们以用户 user 表和 角色role 表为例,假定一个用户能被分配成多重角色,而一种角色也能分给多个用户,故用户和角色构成多对多的关系。
需求:给定角色id,查询这个角色所属的所有用户信息
①、在数据库中建立相应的表
user 表和上面的保持不变
role 表
两者之间的关联表user_role
②、建立对应的实体类
User.java
Role.java
package com.ys.po;
import java.util.List;
public class Role {
private int id;
private String name;
private List users;
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 List getUsers() {return users;
}
public void setUsers(List users) {
this.users = users;
}
}
User_Role.java
package com.ys.po;
public class User_Role {
private User user;
private Role role;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
③、创建 UserMapper 接口和 UserMapper.xml 文件
UserMapper 接口
package many.to.many.mapper;
import java.util.List;
import com.ys.po.User;
public interface UserMapper {
//给定一个角色id,要得到具有这个角色的所有用户信息
public List getUserByRoleId(int roleId);
}
UserMapper.xml
"1.0" encoding="UTF-8" ?>
mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"many.to.many.mapper.UserMapper">"getUserByRoleId" resultMap="getUserMap">
select * from user_role ur,user u where ur.user_id=u.id and ur.role_id=#{id} type="com.ys.po.User" id="getUserMap">"id" property="id"/>"username" property="username"/>"sex" property="sex"/>
④、向 mybatis-configuration.xml 配置文件中注册 UserMapper.xml 文件
⑤、测试
@Test
public void testGetUserByRoleId(){
String statement = "many.to.many.mapper.UserMapper.getUserByRoleId";
//创建OrdersMapper对象,mybatis自动生成mapepr代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
List users = userMapper.getUserByRoleId(1);
session.close();
}
多对多主要是关联关系要找好,然后根据关联去查询。
本系列教程持续更新,可以微信搜索「 IT可乐 」第一时间阅读。回复《电子书》有我为大家特别筛选的书籍资料