当前位置:   article > 正文

Mybatis中对象关系映射

mybatis 数据库对象 java对象的映射

在实际开发中,实体类之间有一对一、一对多、多对多的关系,所以需要正确配置它们对应关系,Mybatis通过配置文件能够从数据库中获取列数据后自动封装成对象。

如:一个订单Orders类对应一个用户User类,一个User类对应多个Orders类。

Orders.java

package cn.itheima.pojo;

import java.util.Date;

public class Orders {
    private Integer id;

    private Integer userId;

    private String number;

    private Date createtime;

    private String note;
    
    private User user;  // 一个订单对应一个用户

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number == null ? null : number.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }  
    
}

User.java

package cn.itheima.pojo;

import java.util.Date;
import java.util.List;

public class User {
    private int id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    
    private List<Orders> ordersList;  // 一个用户对应多个订单
    
    public List<Orders> getOrdersList() {
        return ordersList;
    }
    public void setOrdersList(List<Orders> ordersList) {
        this.ordersList = ordersList;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String i) {
        this.sex = i;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", birthday=" + birthday + ", address=" + address + "]";
    }   
}

 

一、一对一映射

1、 在UserMapper.xml中配置Orders和User的一对一映射关系

    <!-- 
        id:resultMap的唯一标识
        type:将查询出的数据放入到指定对象中
        注意:手动映射需要指定数据库中表的字段名与java中pojo类的属性名称对应关系
     -->
    <resultMap type="cn.itheima.pojo.Orders" id="orderAndUserResultMap">
        <!-- 
            id:指定主键字段对应关系
            column:列,数据库中的字段名称
            property:属性,pojo的属性名称
         -->
        <id column="id" property="id"/>
        <!-- result:指定非主键字段的对应关系 -->
        <result column="user_id" property="userId"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createtime"/>
        <result column="note" property="note"/>
        <!-- 指定单个对象的对应关系
            property:指定将数据放入Orders中的user属性中
            javaType:user属性的类型
         -->
        <association property="user" javaType="cn.itheima.pojo.User">
            <id column="uid" property="id"/>
            <result column="username" property="username"/>
            <result column="sex" property="sex"/>
            <result column="birthday" property="birthday"/>
            <result column="address" property="address"/>
        </association>
    </resultMap>
    <select id="findOrdersAndUser2" resultMap="orderAndUserResultMap">
        SELECT a.*,b.id uid,username,birthday,sex,address
        FROM orders a,user b
        WHERE a.user_id=b.id
    </select>

2、在UserMapper.java中编写接口

List<Orders> findOrdersAndUser2();

3、测试

    @Test
    public void testFindOrdersAndUser2() throws Exception {
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<Orders> list = userMapper.findOrdersAndUser2();
        System.out.println(list);
    }

 

二、一对多映射

1、在UserMapper.xml中配置User和Orders的一对多关系

    <resultMap type="cn.itheima.pojo.User" id="userAndOrdersResultMap">
        <id column="id" property="id"/>
        <result column="birthday" property="birthday"/>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        <!-- 指定对应的集合对象关系映射
            property:将数据放入user对象中的ordersList中
            ofType:指定ordersList属性的泛型类型
         -->
        <collection property="ordersList" ofType="cn.itheima.pojo.Orders">
            <id column="oid" property="id"/>
            <result column="user_id" property="userId"/>
            <result column="number" property="number"/>
            <result column="createtime" property="createtime"/>
        </collection>
    </resultMap>
    <select id="findUserAndOrders" resultMap="userAndOrdersResultMap">
        SELECT a.*,b.id oid,user_id,number,createtime 
        FROM user a,orders b WHERE a.id=b.user_id
    </select>

2、在UserMapper.java中编写接口

List<User> findUserAndOrders();

3、测试

    @Test
    public void testFindUserAndOrders() throws Exception {
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<User> list = userMapper.findUserAndOrders();
        System.out.println(list);
    }

 

转载于:https://www.cnblogs.com/binaway/p/9171587.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/625626
推荐阅读
相关标签
  

闽ICP备14008679号