一.在数据库中建立相应的表
use mybatis ;
-- 删除表
drop table if exists items ;
drop table if exists orders ;
drop table if exists users ;
-- 创建表
create table users(id int primary key auto_increment , name
varchar(20) , age int);
create table orders(id int primary key auto_increment ,
orderno varchar(20) , uid int);
create table items(id int primary key auto_increment ,
itemname varchar(20) , oid int);
-- 插入用户
insert into users(name,age) values('tom',12);
insert into users(name,age) values('tomas',13);
-- 插入订单
insert into orders(orderno,uid) values('No001',1);
insert into orders(orderno,uid) values('No002',1);
insert into orders(orderno,uid) values('No003',2);
insert into orders(orderno,uid) values('No004',2);
-- 插入订单项
insert into items(itemname,oid) values('item001',1);
insert into items(itemname,oid) values('item002',1);
insert into items(itemname,oid) values('item003',2);
insert into items(itemname,oid) values('item004',2);
insert into items(itemname,oid) values('item005',3);
insert into items(itemname,oid) values('item006',3);
insert into items(itemname,oid) values('item007',4);
insert into items(itemname,oid) values('item008',5);
select * from users ;
select * from orders ;
select * from items ;
二.建立实体类,用于去映射数据库中的表
-
建立类
[Order.java] public class Order { private Integer id ; private String orderNo ; //简历关联关系 private User user ; //get/set } [Item.java] public class Item { private Integer id; private String itemName; //订单项和订单之间的关联关系 private Order order; //get/set }
-
引入映射文件
-
测试类
@Test public void selectOne() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession s = sf.openSession(); Order order = s.selectOne("orders.selectOne",1); System.out.println(order.getOrderNo()); s.commit(); s.close(); } @Test public void selectAll() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession s = sf.openSession(); List
list = s.selectList("orders.selectAll"); for(Order o : list){ System.out.println(o.getOrderNo() + " : " + o.getUser().getName()); } s.commit(); s.close(); }
三.配置一对多的应用
-
1.在User中增加orders集合。
public class User { ... private List
orders ; //get/set } - 2.改造UserMapper.xml
四.组合多对一和一对多关联关系到一个实体(Order)中
-
1.关系
Order() -> (1)User
Order(1) -> ()Item 2.Order.java
class Order{
...
List- items ;
//get/set
}
2'.修改配置文件增加别名
[resources/mybatis-config.xml]
-
3.OrderMapper.xml
insert into orders(orderno,uid) values(#{orderNo},#{user.id}) -
4.测试
@Test public void selectOne() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession s = sf.openSession(); Order order = s.selectOne("orders.selectOne",1); System.out.println(order.getOrderNo() + order.getUser().getName()); for(Item i : order.getItems()){ System.out.println(i.getId() + ":" + i.getItemName()); } s.commit(); s.close(); }