您好,登录后才能下订单哦!
这篇文章主要介绍“mybatis中resultMap怎么定义及使用”,在日常操作中,相信很多人在mybatis中resultMap怎么定义及使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mybatis中resultMap怎么定义及使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
我们知道 ,mybatis框架存在pojo对象映射 , 直接将查询到的结果封装到对象中给我们返回, 但如果数据库的中的列和java中类属性名就是不一致,或者如果我们实际返回的对象需要去关联其他的对象(也就是说,其他类的对象作为我们这个类的成员变量),那么这时候使用resultType肯定是不行的
这里我们则需要去定义 resultMap来完成我们的需求
定义resultMap的过程就是描述如何从数据库结果集中去加载对象
resultMap多用于多表查询间的映射关系, 例如 :
我们以部门和员工为例 , 一个部门有多个员工 , 一个员工属于一个部门
建立部门表和员工表
CREATE TABLE dept( -- 部门表 id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10) ) CREATE TABLE employee( -- 员工表 id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10), age INT, deptId INT )
在java中创建 Dept(部门)类 和 Employee(员工)类
public class Employee {       //员工类
    private Integer id;
    private String name;
    private Integer age;
    // 一个员工关联一个部门
    private Dept dept;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", dept=" + dept +
                '}';
    }
}public class Dept {
    private Integer id;
    private String name;
    // 一个部门有多个员工 ,使用List集合存储
    private List<Employee> list;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Employee> getList() {
        return list;
    }
    public void setList(List<Employee> list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", list=" + list +
                '}';
    }
}首先我们查询员工 , 定义dao层接口 :
public interface EmployeeDao {
    // 查询所有员工
    List<Employee> selectAllEmployee();
}由于我们在对象中关联了其他对象, 所以已经不是普通映射 ,这里我们定义 resultMap
<resultMap id="employeeMap" type="Employee"> <!--主键列--> <id column="id" property="id"/> <!--其他属性映射--> <result property="name" column="ename"/> <result property="age" column="age"/> <!--一对一关联--> <association property="dept" javaType="Dept"> <!--需要映射的对象属性--> <result property="name" column="dname"/> </association> </resultMap> <select id="selectAllEmployee" resultMap="employeeMap"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id </select>
resultMap 中的id 是唯一标识 , 相当于名字 , type类型是我们要返回的类型
<select>中使用resultMap , 传入刚定义的id即可
这样在java代码中我们就可以得到我们想要的映射格式
查询部门 :
public interface DeptDao {
    //查询部门
    List<Dept> selectDept();
}定义与使用resultMap
<resultMap id="deptMap" type="Dept"> <id column="id" property="id"/> <result column="dname" property="name"/> <!--collection为一对多 , 这里一个部门包含多个员工--> <collection property="list" javaType="List" ofType="Employee"> <result property="name" column="ename"/> </collection> </resultMap> <select id="selectDept" resultMap="deptMap"> SELECT d.id,d.name dname,e.name ename FROM dept d LEFT JOIN employee e ON d.id = e.deptId </select>
这里 JavaType我们选择list , 因为用list集合来存储多个员工信息, ofType是list集合中实际包含的对象名,这里是员工 Employee
通过resultMap 我们就可以得到自己想要的映射关系
到此,关于“mybatis中resultMap怎么定义及使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。