mybatis高级映射(一对一,一对多)

一对一

场景:生活中每一个人都有一个身份证,这是最简单的一对一的关系。

用户实体对象

@Data
public class User implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Card card;
}

身份证实体对象

@Data
public class Card implements Serializable {
    private Integer id;
    private String code;
}

用户表XML映射文件

<mapper namespace="com.relation.demo.dao.UserMapper">

    <resultMap id="BaseResultMap" type="com.relation.demo.model.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="CHAR"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
        <!--一对一关联映射-->
        <association property="card"  javaType="com.relation.demo.model.Card">
            <id column="card_id" property="id"/>
            <result column="code" property="code"/>
        </association>
    </resultMap>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="integer">
        select
        user.id as id,name,sex,age,card.id as card_id,code
        from user , card
        where user.id=#{id,jdbcType=INTEGER}
    </select>
</mapper>

一对一关系映射

<association property="card"  javaType="com.relation.demo.model.Card">
    <id column="card_id" property="id"/>
    <result column="code" property="code"/>
</association>

测试Controller

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService iUserService;

    @GetMapping("/get/{id}")
    public ResponseModel<User> getUser(@PathVariable Integer id){
        return iUserService.selectByPrimaryKey(id);
    }
}

测试结果

{
    "code": 200,
    "msg": "success",
    "data": {
        "id": 1,
        "name": "小明",
        "sex": "男",
        "age": 18,
        "card": {
            "id": 1,
            "code": "36232199512184513"
        }
    }
}

一对多

场景:生活中每一个班级有多个学生,这是最简单的一对多的关系。

班级实体对象

@Data
public class Clazz implements Serializable {
    private Integer id;
    private String code;
    private String name;
    private List<Student> students;
}

学生实体对象

@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Integer clazzId;
}

班级表XML映射文件

<mapper namespace="com.relation.demo.dao.ClazzMapper">

    <resultMap id="BaseResultMap" type="com.relation.demo.model.Clazz">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="code" property="code" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <!--一对多关联映射-->
        <collection property="students" ofType="com.relation.demo.model.Student">
            <id column="student_id" property="id"/>
            <result column="name" property="name" />
            <result column="sex" property="sex" />
            <result column="age" property="age" />
            <result column="clazz_id" property="clazzId" />
        </collection>
    </resultMap>

    <select id="selectByPrimeryKey" resultMap="BaseResultMap" parameterType="integer">
        select
        clazz.*,student.id student_id,student.name,student.sex,student.age,student.clazz_id
        from clazz , student
        where clazz.id=#{id,jdbcType=INTEGER}
    </select>
</mapper>

一对多关系映射

<collection property="students" ofType="com.relation.demo.model.Student">
  <id column="student_id" property="id"/>
  <result column="name" property="name" />
  <result column="sex" property="sex" />
  <result column="age" property="age" />
  <result column="clazz_id" property="clazzId" />
</collection>                                                       

测试Controller

@RestController
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    IClazzService iClazzService;

    @GetMapping("/get/{id}")
    public ResponseModel<Clazz> selectByPrimeryKey(@PathVariable Integer id){
        return iClazzService.selectByPrimeryKey(id);
    }
}

测试结果

{
    "code": 200,
    "msg": "success",
    "data": {
        "id": 1,
        "code": "1",
        "name": "1班",
        "students": [
            {
                "id": 1,
                "name": "1班",
                "sex": "男",
                "age": 18,
                "clazzId": 1
            },
            {
                "id": 2,
                "name": "1班",
                "sex": "女",
                "age": 17,
                "clazzId": 1
            }
        ]
    }
}

标签: none