发布时间: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();
}
Redis实战案例及问题分析之redis解决商户缓存以及相应缓存问题解决方案(穿透、雪崩、击穿)
mysql分页3种方式,Mysql的limit用法与几种分页形式
python OpenCV的imread不能读取中文路径问题及解决
sqlserver提权失败_SqlServer SA 弱口令提权
linux金山打字通游戏版,金山打字游戏2010电脑版-金山打字游戏(最全大集合安装)下载V8.1.0.1官方版-西西软件下载...
vue3【列表渲染】v-for 详细介绍(vue中的“循环”)
用Python制作可视化GUI界面,一键提取图片当中的数据并保存至Excel当中
乐视员工:神仙哪有我舒坦;AI终于能写正则表达式了!传统组织管理学也被AI抢饭碗?算法工程师面试题总结;几篇前沿论文 | ShowMeAI资讯日报