SpringBoot整合Mybatis
五、多对多实现
目标:根据id查询运营商拥有的用户和电话信息
1. 构建项目
使用上一章内容
2.编写代码
1). 实体类Combo
位于entity包,添加UserCombo集合,添加对应的setter、getter
/**
* @author Ray
* @date 2018/7/7 0007
* 运营商实体类
*/
public class Combo {
private int id;
private String name;
private List<UserCombo> userCombos;
public List<UserCombo> getUserCombos() {
return userCombos;
}
public void setUserCombos(List<UserCombo> userCombos) {
this.userCombos = userCombos;
}
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;
}
@Override
public String toString() {
return "Combo{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2). 实体类UserCombo
位于entity包,添加User集合,添加对应的setter、getter
/**
* @author Ray
* @date 2018/7/7 0007
* 用户拥有运营商实体
*/
public class UserCombo {
private int uid;
private int cid;
private int price;
private List<User> users;
private List<Combo> combos;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public List<Combo> getCombos() {
return combos;
}
public void setCombos(List<Combo> combos) {
this.combos = combos;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "UserCombo{" +
"uid=" + uid +
", cid=" + cid +
", price=" + price +
", combos=" + combos +
'}';
}
}
3). 数据操作类ComboMapper
位于mapper包,添加查询方法selectUserById
/**
* @author Ray
* @date 2018/7/7 0007
* 数据操作层
*/
public interface ComboMapper {
/**
* 根据id查询运营商拥有的用户和电话信息
*/
public Combo selectUserById(Integer id);
}
4). ComboMapper类映射ComboMapper.xml
多对多的理解:就是多个一对多的组合。
<?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.ray.mybatis.mapper.ComboMapper">
<!-- 注意:抽离comboMap -->
<resultMap id="comboMap" type="combo">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<!-- 注意:继承comboMap -->
<resultMap id="userMap" type="combo" extends="comboMap">
<!-- 一对多 【ofType是映射到list集合属性中pojo的类型】 -->
<collection property="userCombos" ofType="userCombo">
<result property="price" column="price"/>
<!-- 一对多 -->
<collection property="users" ofType="user">
<id property="id" column="uid"/>
<result property="name" column="uname"/>
<!-- 一对多 【ofType是映射到list集合属性中pojo的类型】 -->
<collection property="phones" ofType="phone">
<id property="id" column="pid"/>
<result property="name" column="pname"/>
<result property="number" column="number"/>
</collection>
</collection>
</collection>
</resultMap>
<!-- 根据id查询运营商拥有的用户和电话信息 -->
<select id="selectUserById" parameterType="int" resultMap="userMap">
SELECT
c.*,
s.price,
u.id uid,u.name uname,
p.id pid,p.name pname,p.number
FROM
combo c, user_combo s, user u, phone p
WHERE
c.id=s.cid
AND
s.uid=u.id
AND
u.id=p.uid
and
c.id=#{id}
</select>
</mapper>
5). 数据控制类ComboController
位于controller包
/**
* @author Ray
* @date 2018/7/7 0007
* 数据控制层
*/
@RestController
public class ComboController {
@Autowired
private ComboMapper comboMapper;
/**
* 根据id查询运营商拥有的用户和电话信息
* localhost:8080/combo/user/2
*/
@GetMapping("/combo/user/{id}")
public Combo selectUserById(@PathVariable("id") Integer id){
Combo combo = comboMapper.selectUserById(id);
return combo;
}
}
3. 测试
根据id查询运营商拥有的客户和电话信息-GET