左表为user,右表为orders,一对多的关系
实体类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package cn.itcast.mybatis.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> orders;//此处存的为多的一方的集合 }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13package cn.itcast.mybatis.pojo; import java.util.Date; public class Orders { private int id; private int user_id; private String number; private Date createtime; private String note; }
user对应的dao接口
复制代码
1
2
3
4
5
6
7
8
9
10
11package cn.itcast.mybatis.mapper; import cn.itcast.mybatis.pojo.User; import java.util.List; public interface UserMapper { public List<User> testOneToMany(); }
user对应的mapper.xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<?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="cn.itcast.mybatis.mapper.UserMapper"> <resultMap id="onToMany" type="user"> <id column="id" property="id"></id> <result column="username" property="username"/> <result column="address" property="address"/> <collection property="orders" ofType="orders"> <id column="oid" property="id"></id> <result column="number" property="number"></result> <result column="createtime" property="createtime"></result> <result column="user_id" property="user_id"></result> </collection> </resultMap> <select id="testOneToMany" resultMap="onToMany"> select u.id,u.username,u.address,o.id oid,o.user_id,o.number,o.createtime from user u left join orders o on u.id = o.user_id </select> </mapper>
测试类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51package cn.itcast.mybatis.quickstart; import cn.itcast.mybatis.mapper.UserMapper; import cn.itcast.mybatis.pojo.User; 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.Test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * mybatis入门程序 * @author Ming Wang * */ public class QuickStartDemo { /** * 获取mybatis回话SqlSession * @return * @throws IOException */ private SqlSession getSqlSession() throws IOException { // 1、创建SqlSessionFactoryBuilder加载mybatis的核心配置文件 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 2、获取回话工厂SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); // 3、获取回话SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } @Test public void testOneToMany() throws IOException { SqlSession sqlSession = getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.testOneToMany(); System.out.println(users); sqlSession.close(); } }
得到的结果-->第二行和第三行为user对应的的二个orders
复制代码
1[User{id=1, username='王五', sex='null', birthday=null, address='null',
复制代码
1orders=[Orders{id=3, user_id=1, number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015, note='null'},
复制代码
1Orders{id=4, user_id=1, number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015, note='null'}]},
复制代码
1
2User{id=10, username='张三', sex='null', birthday=null, address='北京市', orders=[Orders{id=5, user_id=10, number='1000012', createtime=Thu Feb 12 16:13:23 CST 2015, note='null'}]}, User{id=16, username='张小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=22, username='陈小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=24, username='张三丰', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=25, username='陈小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=26, username='王五', sex='null', birthday=null, address='null', orders=[]}, User{id=27, username='1', sex='null', birthday=null, address='2', orders=[]}, User{id=28, username='1', sex='null', birthday=null, address='2', orders=[]}, User{id=29, username='lilili', sex='null', birthday=null, address='SHHHH', orders=[]}, User{id=31, username='q', sex='null', birthday=null, address='q', orders=[]}, User{id=32, username='1', sex='null', birthday=null, address='2', orders=[]}]
数据库查询出来的结果
总结:当表关系为一对多时,比如A表对应B表,可以在A的实体类中增加一个集合,存放多个B类。通过外连接查询后返回到A表。此时A表里的集合存储为多条B。
最后
以上就是舒服泥猴桃最近收集整理的关于Mybatis一对多实战的全部内容,更多相关Mybatis一对多实战内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复