标签:ack array main domain select pass 举例 set att
以用户和订单举例,
一对一 : 一个订单只属于一个用户 ==> 订单对用户是一对一关系
一对多 : 一个用户可以拥有多个订单 ==> 用户对订单是一对多关系
注意:在MyBatis中,如果要完成多对多关系,其实就是两个一对多映射!
接下来先讲解MyBatis的一对一映射如何实现。
CREATE TABLE `t_user` (
`id` int(11) DEFAULT NULL,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
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
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;
}
}
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实体类,通过user属性建立跟User实体的关系。注意,这里是一个User。
package com.yiidian.dao;
import com.yiidian.domain.User;
import java.util.List;
/**
* 用户Dao接口
* 一点教程网 - www.yiidian.com
*/
public interface UserDao {
}
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();
}
<?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>
<?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>
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();
}
}
以debug模式启动测试类,查看list变量的情况,可以看到Order对象中成功封装了User对象的数据!
标签:ack array main domain select pass 举例 set att
原文地址:https://www.cnblogs.com/ysd139856/p/12538439.html