MyBatis3一对一,一对多
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis3一对一,一对多相关的知识,希望对你有一定的参考价值。
在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的“多”中也只有一条数据。id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性。这帮助来改进整体表现,特别是缓存和嵌入结果映射。所以不同数据的id应该唯一区别,不然导致数据结果集只有一条数据。
一、表
二、实体
1.person
- package com.kerwin.mybatis.pojo;
- import java.util.List;
- public class Person {
- private int id;
- private String name;
- private List<Orders> orderList;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public List<Orders> getOrderList() {
- return orderList;
- }
- public void setOrderList(List<Orders> orderList) {
- this.orderList = orderList;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + "]";
- }
- public Person() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Person(int id, String name, List<Orders> orderList) {
- super();
- this.id = id;
- this.name = name;
- this.orderList = orderList;
- }
- }
2.order
- package com.kerwin.mybatis.pojo;
- public class Orders {
- private int id;
- private double price;
- private Person person;
- public Person getPerson() {
- return person;
- }
- public void setPerson(Person person) {
- this.person = person;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- @Override
- public String toString() {
- return "Orders [id=" + id + ", price=" + price + "]";
- }
- public Orders() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
三、映射mapper文件
1. PersonMapper.xml
- <?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">
- <mapper namespace="com.kerwin.mybatis.pojo.Person">
- <resultMap type="com.kerwin.mybatis.pojo.Person" id="personreSultMap">
- <id column="p_id" property="id"/>
- <result column="name" property="name"/>
- <collection property="orderList" ofType="com.kerwin.mybatis.pojo.Orders" column="pid">
- <id column="o_id" property="id"/>
- <result column="price" property="price"/>
- </collection>
- </resultMap>
- <select id="selectPersonFetchOrder" parameterType="int" resultMap="personreSultMap" >
- select p.*,o.* from person p,orders o where o.pid=p.p_id and p.p_id=#{id}
- </select>
- </mapper>
2. OrdersMapper.xml
- <?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">
- <mapper namespace="com.kerwin.mybatis.pojo.Orders">
- <resultMap type="com.kerwin.mybatis.pojo.Orders" id="OrdersResultMap">
- <id column="o_id" property="id"/>
- <result column="price" property="price"/>
- <association property="person" javaType="com.kerwin.mybatis.pojo.Person">
- <id column="p_id" property="id"/>
- <result column="name" property="name"/>
- </association>
- </resultMap>
- <select id="selectOrdersFetchPerson" resultMap="OrdersResultMap">
- select p.*,o.* from person p,orders o where o.pid=p.p_id and o.o_id=#{id}
- </select>
- </mapper>
3.sqlMapConfig.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <typeAliases>
- <typeAlias type="com.kerwin.mybatis.pojo.Author" alias="Author"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/kerwin/mybatis/pojo/AuthorMapper.xml"/>
- <mapper resource="com/kerwin/mybatis/pojo/PostMapper.xml"/>
- <mapper resource="com/kerwin/mybatis/pojo/PersonMapper.xml"/>
- <mapper resource="com/kerwin/mybatis/pojo/OrdersMapper.xml"/>
- </mappers>
- </configuration>
四。测试类
- /**
- *
- */
- package com.kerwin.mybatis.test;
- import java.io.InputStream;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import com.kerwin.mybatis.pojo.Orders;
- import com.kerwin.mybatis.pojo.Person;
- /**
- * @author Administrator
- *
- */
- public class PersonAndOrderTest {
- private static SqlSessionFactory sessionFactory;
- /**
- * @throws java.lang.Exception
- */
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
- InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
- sessionFactory = factoryBuilder.build(inputStream);
- }
- //一对多,查询person(一)级联查询订单order(多)
- @Test
- public void testSelectPersonFetchOrder() throws Exception {
- SqlSession session = sessionFactory.openSession();
- Person person = session.selectOne("com.kerwin.mybatis.pojo.Person.selectPersonFetchOrder", 1);
- System.out.println(person);
- System.out.println(person.getOrderList().size());
- for(Orders orders : person.getOrderList()){
- System.out.println(orders);
- }
- session.close();
- }
- //多对一,查询订单order(多)级联查询person(一)
- @Test
- public void testSelectOrdersFetchPerson() throws Exception{
- SqlSession session = sessionFactory.openSession();
- Orders orders = session.selectOne("com.kerwin.mybatis.pojo.Orders.selectOrdersFetchPerson", 1);
- System.out.println(orders);
- System.out.println(orders.getPerson());
- session.close();
- }
- }
五、测试结果
1.一对多,查询person(一)级联查询订单order(多)
2.多对一,查询订单order(多)级联查询person(一)
注意:两张表中的主键id字段名要唯一,例如不能都写id,不然的话,在一对多查询的时候就会出现:级联出来的订单项只有一条记录。我之前就是将两张表的主键id字段名都写为id,导致测试结果级联出来的多一直只有一条数据,具体如下:
以上是关于MyBatis3一对一,一对多的主要内容,如果未能解决你的问题,请参考以下文章