继续在 SpringBoot整合Mybatis-Plus 基础上修改项目
一、创建两张表
用户表(User)、区域表(Area),其中用户表里通过 area_id 字段关联区域表的 id 主键
二、编写实体类
User
package org.spring.springboot.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String userName;
private String passWord;
private Integer areaId;
@TableField(exist = false)
private String areaName;
@TableField(exist = false)
private Area area;
}
Area
package org.spring.springboot.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.util.List;
@Data
public class Area {
@TableId(type = IdType.AUTO)
private Integer id;
private String areaName;
@TableField(exist = false)
private List<User> users;
}
三、使用 @One 注解实现一对一关联
UserMapper
package org.spring.springboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.spring.springboot.entity.User;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
@Select("select user.* ,area.area_name from user,area "+
" where user.area_id = area.id and user.id = #{id}"
)
User getUserById(int id);
@Select("SELECT * FROM user WHERE area_id = #{areaId}")
User selectByAreaId(int areaId);
}
UserMapperOne
package org.spring.springboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.spring.springboot.entity.User;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapperOne extends BaseMapper<User> {
@Results({
@Result(column = "area_id", property = "areaId"),
@Result(column = "area_id", property = "area",
one = @One(select = "org.spring.springboot.mapper.AreaMapper.selectById"))
})
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(int id);
}
四、使用 @Many 注解实现一对一关联
AreaMapper
package org.spring.springboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.spring.springboot.entity.Area;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface AreaMapper extends BaseMapper<Area> {
@Results({
@Result(column = "id", property = "id"),
@Result(column = "id", property = "users",
many = @Many(select = "org.spring.springboot.mapper.UserMapper.selectByAreaId"))
})
@Select("SELECT * FROM area WHERE id = #{id}")
Area getAreaById(int id);
}
五、Controller层
SelectController
package org.spring.springboot.controller;
import org.spring.springboot.entity.Area;
import org.spring.springboot.entity.User;
import org.spring.springboot.mapper.AreaMapper;
import org.spring.springboot.mapper.UserMapper;
import org.spring.springboot.mapper.UserMapperOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class SelectController {
@Autowired
UserMapper userMapper;
@Autowired
UserMapperOne userMapperOne;
@Autowired
AreaMapper areaMapper;
@RequestMapping("/selectUser")
public User test(){
User user = userMapper.getUserById(1);
return user;
}
@RequestMapping("/selectUserOne")
public User testOne(){
User user = userMapperOne.getUserById(1);
return user;
}
@RequestMapping("/selectUserMany")
public Area testMany(){
Area area = areaMapper.getAreaById(1);
return area;
}
}
六、测试
输入http://localhost:8086/api/selectUser 输入http://localhost:8086/api/selectUserOne 输入http://localhost:8086/api/selectUserMany 整体项目结构
参考文章 SpringBoot - MyBatis-Plus使用详解7(Mapper的CRUD接口4:多表关联查询)
|