mybatis association使用總結


import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class Employee implements Serializable {
    private Integer id;
    private String name;
    private int sex;
    private String job;
    private double salary;
    //private Integer companyID;
    private Company company;
    private List<Menu> menus;
}
@Repository
public interface EmployeeMapper {

    Employee findEmployee(Integer empID);

    Employee findEmployeeTwo(Integer empID);

    Employee findEmployeeThree(Integer empID);
}
<?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.example.mybatis01.mapper.EmployeeMapper">
    <resultMap id="employeeMap" type="com.example.mybatis01.entity.Employee">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
            <result column="sex" property="sex"></result>
            <result column="job" property="job"></result>
            <result column="salary" property="salary"></result>
            <!--<result column="company_id" property="companyID"></result>-->
            <association property="company" javaType="com.example.mybatis01.entity.Company"
                         column="company_id" >
                <id column="cid" property="id"></id>
                <result column="cname" property="name"></result>
                <result column="ccode" property="code"></result>
                <result column="caddress" property="address"></result>
                <result column="ctelphone" property="telephone"></result>
            </association>
    </resultMap>

    <select id="findEmployee" resultMap="employeeMap">
        select p.id,p.name,p.sex,p.job,p.salary,
                c.id cid,c.code ccode,c.name cname,c.address caddress,c.telephone ctelphone
        from t_employee p inner join t_company c on  p.company_id = c.id
        where p.id = #{id}
    </select>

    <resultMap id="employeeMapTwo" type="com.example.mybatis01.entity.Employee">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="sex" property="sex"></result>
        <result column="job" property="job"></result>
        <result column="salary" property="salary"></result>
        <!--<result column="company_id" property="companyID"></result>-->
        <association property="company" select="com.example.mybatis01.mapper.CompanyMapper.selectByID"
                     column="company_id" javaType="com.example.mybatis01.entity.Company"
                fetchType="lazy">
        </association>
    </resultMap>

    <select id="findEmployeeTwo" resultMap="employeeMapTwo">
        select p.id,p.name,p.sex,p.job,p.salary,p.company_id from t_employee p where p.id = #{empID}
    </select>

    <resultMap id="employeeMapThree" type="com.example.mybatis01.entity.Employee">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="sex" property="sex"></result>
        <result column="job" property="job"></result>
        <result column="salary" property="salary"></result>
        <!--<result column="company_id" property="companyID"></result>-->
        <result column="cid" property="company.id"></result>
        <result column="ccode" property="company.code"></result>
        <result column="cname" property="company.name"></result>
        <result column="caddress" property="company.address"></result>
        <result column="ctelephone" property="company.telephone"></result>
    </resultMap>

    <select id="findEmployeeThree" resultMap="employeeMapThree">
        select p.id,p.name,p.sex,p.job,p.salary,
                c.id cid,c.code ccode,c.name cname,c.address caddress,c.telephone ctelephone
        from t_employee p inner join t_company c on  p.company_id = c.id
        where p.id = #{id}
    </select>

</mapper>

@SpringBootTest
class Mybatis01ApplicationTests {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Test
    void contextLoads() {

        Employee employeeOne = employeeMapper.findEmployee(1);
        System.out.println(employeeOne);

        Employee employeeTwo = employeeMapper.findEmployeeTwo(1);
        System.out.println(employeeTwo);

        Employee employeeThree = employeeMapper.findEmployeeThree(1);
        System.out.println(employeeThree);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章