SpringBoot整合Mybatis (五) 多对多

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 userCombos;

    public List getUserCombos() {
        return userCombos;
    }

    public void setUserCombos(List 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 users;
    private List combos;

    public List getUsers() {
        return users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    public List getCombos() {
        return combos;
    }

    public void setCombos(List 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

      多对多的理解:就是多个一对多的组合。




    
    
        
        
    

    
    
        
        
            
            
            
                
                
                
                
                    
                    
                    
                
            
        
    

    
    



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

SpringBoot整合Mybatis (五) 多对多_第1张图片

你可能感兴趣的:(SpringBoot)