文章图片
文章图片
左表为user,右表为orders,一对多的关系
实体类
package 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;
//此处存的为多的一方的集合}
package 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接口
package cn.itcast.mybatis.mapper;
import cn.itcast.mybatis.pojo.User;
import java.util.List;
public interface UserMapper { public List testOneToMany();
}
user对应的mapper.xml
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
测试类
package 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 users = mapper.testOneToMany();
System.out.println(users);
sqlSession.close();
}
}
【Mybatis|Mybatis一对多实战】得到的结果-->第二行和第三行为user对应的的二个orders
[User{id=1, username='王五', sex='null', birthday=null, address='null',
orders=[Orders{id=3, user_id=1, number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015, note='null'},
Orders{id=4, user_id=1, number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015, note='null'}]},
User{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。
推荐阅读
- mysql|InnoDB数据页结构
- javaweb|基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
- mysql|一文深入理解mysql
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- SQL|SQL基本功(五)--函数、谓词、CASE表达式
- vue|电商后台管理系统(vue+python|node.js)
- 框架|Mybatis的一级缓存和二级缓存
- Java及基础算法及数据结构|旧笔记整理(MySQL)
- mysql|双非本211硕,无实习无项目,自学大数据开发,秋招上岸
- 数据库|Mysql--InnoDB存储引擎详解