Original link: http://www.yiidian.com/mybatis/one-to-one.html
1 What are one-to-one, one-to-many mappings?
Take users and orders for example,
One-to-one: An order belongs to only one user==>The order is one-to-one with the user
One-to-many: A user can have multiple orders==>A user is one-to-many with an order
Note: In MyBatis, if you want to complete a many-to-many relationship, it is actually two one-to-many mappings!
Next, let's explain how MyBatis's one-to-one mapping works.
2 Establish table structure
2.1 Create 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 Create Order Form
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 Insert Test Data
3 Design Pojo entities and build relationships
3.1 User Entity Class
package com.yiidian.domain; import java.util.List; /** * User Entities * One Point Tutorial Web - 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 Order Entity Class
package com.yiidian.domain; /** * Order Entities * One Point Tutorial Web - 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 that establishes a relationship with a User entity through the user attribute.Note that here is a User.
4 Write Dao interface
4.1 UserDao interface
package com.yiidian.dao; import com.yiidian.domain.User; import java.util.List; /** * User Dao Interface * One Point Tutorial Web - 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; /** * Order Dao Interface * One Point Tutorial Web - www.yiidian.com */ public interface OrderDao { /** * Query all orders */ public List<Order> findAllOrders(); }
5 Write Dao Mapping Configuration
5.1 UserDao.xml Configuration
<?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: Used to specify that the mapping file needs to be mapped Dao Interface --> <mapper namespace="com.yiidian.dao.UserDao"> </mapper>
5.2 OrderDao.xml Configuration
<?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: Used to specify that the mapping file needs to be mapped Dao Interface --> <mapper namespace="com.yiidian.dao.OrderDao"> <!--One-to-one mapping--> <resultMap id="OrderResultMap" type="com.yiidian.domain.Order"> <id property="id" column="oid"/> <result property="orderno" column="orderno"/> <result property="amount" column="amount"/> <!--Associate the user to which the query order belongs--> <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: used in one-to-one association mapping
- Property: the user property of the corresponding Order class
- column: corresponding foreign key field name
- javaType:Fully qualified name of User class
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 Test Class-One-to-One Mapping * One Point Tutorial Web - www.yiidian.com */ public class TestOne2One { /** * Test one-to-one mapping */ @Test public void testOrderDao(){ //1. Get the SqlSession object SqlSession sqlSession = MyBatisUtils.getSession(); //2. Create Mapper Proxy Object OrderDao orderDao = sqlSession.getMapper(OrderDao.class); //3. Call Method List<Order> list = orderDao.findAllOrders(); System.out.println(list); //4. Close the connection sqlSession.close(); } }
7 Run Test Class
Start the test class in debug mode and look at the list variable to see that the Order object successfully encapsulates the User object's data!
Source download: https://pan.baidu.com/s/1jZrfapjqB_VHI_GLgKPo4g
Welcome to my public number: A little tutorial.Get exclusively organized learning resources and daily dry delivery.
If you are interested in my series of tutorials, you can also follow my website: yiidian.com