发布时间:2023-09-09 09:30
学习一对多关系,在“SSM框架学习(二)”的基础上,从订单和客户的多对一关系掌握知识
package com.ssm.mybatis.entity;
/**
* 订单实体类
*/
public class Order {
private Integer id;
private String orderno;
private float price;
private int cid;
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", orderno='" + orderno + '\'' +
", price=" + price +
", cid=" + cid +
'}';
}
}
1、在resources下新建OrderMapper.xml文件
2、编写映射文件OrderMapper.xml
1、在OrderMapper.xml中编写SQL插入语句
insert into orders(id,orderno,price,cid) values (#{id},#{orderno},#{price},#{cid})
2、在测试类中编写插入方法insertOrder()
//插入订单
@Test
public void insertOrder () throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
Order order=new Order();
order.setId(1);
order.setOrderno("001");
order.setPrice(5.2f);
order.setCid(2);
s.insert("OrderMapper.insert",order);
s.commit();
s.close();
}
订单表中的cid为此订单的对应客户id,此时的值为手动输入,考虑订单与客户的多对一关系,应该改为面向对象编程
将Order类中的cid更换为Customer的对象
package com.ssm.mybatis.entity;
/**
* 订单实体类
*/
public class Order {
private Integer id;
private String orderno;
private float price;
//private int cid;
private Customer customer;
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", orderno='" + orderno + '\'' +
", price=" + price +
", customer=" + customer +
'}';
}
}
insert into orders(id,orderno,price,cid) values (#{id},#{orderno},#{price},#{customer.id})
原插入语句为:
insert into orders(id,orderno,price,cid) values (#{id},#{orderno},#{price},#{cid})
将cid改为customer.id即表示为Customer实体类中的id属性
//插入订单
@Test
public void insert() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
Order order=new Order();
order.setId(4);
order.setOrderno("002");
order.setPrice(10f);
Customer customer=s.selectOne("CustomerMapper.findbyid",1);
order.setCustomer(customer);
s.insert("OrderMapper.insert",order);
s.commit();
s.close();
}
查看数据库,可以发现2号订单关联上1号客户。
若在新增订单的同时新增客户,需要在插入完客户之后马上获得客户的id。
在CustomerMapper.xml修改SQL插入语句
insert into customers(id,name,age) values (#{id}, #{name},#{age})
编写insert()插入方法
//插入订单
@Test
public void insert() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
Customer customer=new Customer();
customer.setId(4);
customer.setName("jim");
customer.setAge(15);
s.insert("CustomerMapper.insert",customer);
Order order=new Order();
order.setId(3);
order.setOrderno("003");
order.setPrice(10f);
order.setCustomer(customer);
s.insert("OrderMapper.insert",order);
s.commit();
s.close();
}
//查询订单
@Test
public void findbyid() 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("OrderMapper.findbyid",1);
System.out.println(order.toString()+","+order.getCustomer().toString());
s.commit();
s.close();
}
知识点
原因:
查询方法:
select
o.id as oid,
o.orderno as oorderno,
o.price as oprice,
c.id as cid,
c.name as cname,
c.age as cage
from orders o
left outer join customers c on o.cid=c.id
where o.id=#{id}
//查询所有订单
@Test
public void findall() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
List orderList=s.selectList("OrderMapper.findall");
for(Order o:orderList){
System.out.println(o.toString()+o.getCustomer().toString());
}
s.commit();
s.close();
}