One to one mapping relationship MyBatis

Original link: http: //www.yiidian.com/mybatis/one-to-one.html

1 What is one to one, one to many mapping?

User and order, for example,

One: an order belongs to only one user ==> Order of the user is one relationship

Many: a user can have multiple orders ==> user orders are to-many relationship

Note: In MyBatis, if you want to complete many relationships, in fact, two-to-many mapping!

Then how to explain MyBatis one mapping implementation.

2 Establish table structure

2.1 Create a user table

CREATE TABLE `t_user` (
   `id` int(11) DEFAULT NULL,
   `username` varchar(50) DEFAULT NULL,
   `password` varchar(50) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.2 Creating Orders table

CREATE TABLE `t_order` (
   `id` int(11) DEFAULT NULL,
   `orderno` varchar(100) DEFAULT NULL,
   `amount` double DEFAULT NULL,
   `user_id` int(11) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.3 Inserting the test data

file

file

3 Pojo entity design, build relationships

3.1 User entity class

package com.yiidian.domain;

import java.util.List;

/**
 * 用户实体
 * 一点教程网 - www.yiidian.com
 */
public class User {
    private Integer id;
    private String username;
  
    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3.2 Orders entity classes

package com.yiidian.domain;

/**
 * 订单实体
 * 一点教程网 - www.yiidian.com
 */
public class Order {
    private Integer id;
    private String orderno;
    private Double amount;
    private User user;

    public Integer getId() {
        return id;
    }

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

    public String getOrderno() {
        return orderno;
    }

    public void setOrderno(String orderno) {
        this.orderno = orderno;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public User getUser() {
        return user;
    }

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

Order entity class, establish a relationship with the User entity by user attributes. Note that this is a User.

4 Write Dao Interface

4.1 UserDao Interface

package com.yiidian.dao;

import com.yiidian.domain.User;

import java.util.List;

/**
 * 用户Dao接口
 * 一点教程网 - www.yiidian.com
 */
public interface UserDao {

}

4.2 OrderDao Interface

package com.yiidian.dao;
import com.yiidian.domain.Order;
import com.yiidian.domain.User;
import java.util.List;

/**
 * 订单Dao接口
 * 一点教程网 - www.yiidian.com
 */
public interface OrderDao {
    /**
     * 查询所有订单
     */
    public List<Order> findAllOrders();
}

5 write Dao mapping configuration

5.1 UserDao.xml arrangement

<?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">
<!--
   namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.UserDao">

</mapper>

5.2 OrderDao.xml arrangement

<?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">
<!--
   namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.OrderDao">

    <!--一对一映射-->
    <resultMap id="OrderResultMap" type="com.yiidian.domain.Order">
        <id property="id" column="oid"/>
        <result property="orderno" column="orderno"/>
        <result property="amount" column="amount"/>
        <!--关联查询订单所属的用户-->
        <association property="user" column="id" javaType="com.yiidian.domain.User">
            <id property="id" column="id"/>
            <result property="username" column="username"/>
            <result property="password" column="password"/>
        </association>
    </resultMap>

    <select id="findAllOrders" resultMap="OrderResultMap">
        SELECT
          o.id oid,
          o.orderno orderno,
          o.amount amount,
          u.*
        FROM t_order o
          LEFT JOIN t_user u
            ON o.user_id = u.id
    </select>
</mapper>
  • association: one used in association mapping
  • property: user attribute corresponding to the Order class
  • column: the name of the corresponding foreign key field
  • javaType: User class fully qualified name

6 write test classes

package com.yiidian.mybatis;

import com.yiidian.dao.OrderDao;
import com.yiidian.domain.Order;
import com.yiidian.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * MyBatis测试类 - 一对一映射
 * 一点教程网 - www.yiidian.com
 */
public class TestOne2One {

    /**
     * 测试一对一映射
     */
    @Test
    public void testOrderDao(){
        //1.获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSession();

        //2.创建Mapper代理对象
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);

        //3.调用方法
        List<Order> list = orderDao.findAllOrders();
        System.out.println(list);
        //4.关闭连接
        sqlSession.close();
    }

}

7 run the test class

Start the test case class to debug mode, view the list of variables, can be seen in the success of the Order object encapsulates data User objects!

file

file

Source Download: https: //pan.baidu.com/s/1jZrfapjqB_VHI_GLgKPo4g

file

Welcome to my attention that the public number :: tutorial. Get exclusive organize learning resources and push dry goods daily.
If you are interested in my tutorial series, you can focus on my website: yiidian.com

Guess you like

Origin www.cnblogs.com/yiidian/p/12536428.html