Mybatis实现订单案例的五表操作

1.pom文件添加juint,mysql,mybatis(3.4.4)

2.设置mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><!--该配置文件中包含一个configuration节点里面有配置信息 分别是环境和映射其中环境里有datasource,里面有我们熟悉的连接数据库的四个字符串
-->
<configuration><properties resource="db.properties" /><!--日志--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${user}"/><property name="password" value="${pass}"/></dataSource></environment></environments><mappers><mapper resource="com.qfedu.mapper/OrderMapper.xml"/><mapper resource="com.qfedu.mapper/UserMapper.xml"/><mapper resource="com.qfedu.mapper/DetailsMapper.xml"/><mapper resource="com.qfedu.mapper/ProductMapper.xml"/><mapper resource="com.qfedu.mapper/TypeMapper.xml"/></mappers>
</configuration>

3.db.properties数据库的配置文件

4.设计pojo类

在这里插入代码片

1.user .java

package com.qfedu.pojo;public class User {private int uid;private String name;private String pass;private String phone;@Overridepublic String toString() {return "User{" +"uid=" + uid +", name='" + name + '\'' +", pass='" + pass + '\'' +", phone='" + phone + '\'' +'}';}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}
}

2.Type.java

package com.qfedu.pojo;public class Types {private String tid;private String name;@Overridepublic String toString() {return "Types{" +"tid='" + tid + '\'' +", name='" + name + '\'' +'}';}public String getTid() {return tid;}public void setTid(String tid) {this.tid = tid;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

3.product.java

package com.qfedu.pojo;public class Product {private String pid;private String name;private String img;private double price;private Types t;@Overridepublic String toString() {return "Product{" +"pid='" + pid + '\'' +", name='" + name + '\'' +", img='" + img + '\'' +", price=" + price +", t=" + t +'}';}public String getPid() {return pid;}public void setPid(String pid) {this.pid = pid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getImg() {return img;}public void setImg(String img) {this.img = img;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public Types getT() {return t;}public void setT(Types t) {this.t = t;}
}

Detail.java

package com.qfedu.pojo;public class Detail {private String did;private int count;private Product pro;@Overridepublic String toString() {return "Detail{" +"did='" + did + '\'' +", count=" + count +", pro=" + pro +'}';}public String getDid() {return did;}public void setDid(String did) {this.did = did;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public Product getPro() {return pro;}public void setPro(Product pro) {this.pro = pro;}
}

Order.java

package com.qfedu.pojo;import java.util.List;public class order {private String oid;private double price;private String addr;private String payType;private User u;private List<Detail> details;@Overridepublic String toString() {return "order{" +"oid='" + oid + '\'' +", price=" + price +", addr='" + addr + '\'' +", payType='" + payType + '\'' +", u=" + u +", details=" + details +'}';}public String getOid() {return oid;}public void setOid(String oid) {this.oid = oid;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}public String getPayType() {return payType;}public void setPayType(String payType) {this.payType = payType;}public User getU() {return u;}public void setU(User u) {this.u = u;}public List<Detail> getDetails() {return details;}public void setDetails(List<Detail> details) {this.details = details;}
}

5.设置映射文件

1.DetailsMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--每个mapper文件都将有一个自己的映射的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id整个项目中所有的namespace.id必须是唯一的
--><mapper namespace="com.qfedu.pojo.DetailMapper"><select id="getDetailsByOid" resultMap="detailsMap">select * from details where oid = #{oid};</select><resultMap id="detailsMap" type="com.qfedu.pojo.Detail"><id property="did" column="did"></id><result property="count" column="count" /><association property="pro" column="pid" select="com.qfedu.pojo.ProductMapper.getProductByDid"></association><!--pro 应该与Details类中 的pro字段保持一致 否则会报错--></resultMap></mapper>

2.OrderMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--每个mapper文件都将有一个自己的映射的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id整个项目中所有的namespace.id必须是唯一的
--><mapper namespace="com.qfedu.pojo.DetailMapper"><select id="getDetailsByOid" resultMap="detailsMap">select * from details where oid = #{oid};</select><resultMap id="detailsMap" type="com.qfedu.pojo.Detail"><id property="did" column="did"></id><result property="count" column="count" /><association property="pro" column="pid" select="com.qfedu.pojo.ProductMapper.getProductByDid"></association><!--pro 应该与Details类中 的pro字段保持一致 否则会报错--></resultMap></mapper>

3.ProductMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--每个mapper文件都将有一个自己的映射的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id整个项目中所有的namespace.id必须是唯一的
--><mapper namespace="com.qfedu.pojo.ProductMapper"><select id="getProductByDid" resultMap="productMap">select * from products where pid = #{pid};</select><resultMap id="productMap" type="com.qfedu.pojo.Product"><id property="pid" column="pid"></id><association property="t" column="tid" select="com.qfedu.pojo.TypeMapper.getTypeByTid"/></resultMap></mapper>

4.TypeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--每个mapper文件都将有一个自己的映射的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id整个项目中所有的namespace.id必须是唯一的
--><mapper namespace="com.qfedu.pojo.TypeMapper"><select id="getTypeByTid" resultType="com.qfedu.pojo.Types">select * from types where tid = #{tid};</select></mapper>

5.UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--每个mapper文件都将有一个自己的映射的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id整个项目中所有的namespace.id必须是唯一的
--><mapper namespace="com.qfedu.pojo.UserMapper"><select id="getUserByuid" resultType="com.qfedu.pojo.User">select * from user where uid = #{uid};</select></mapper>

6.TestOrder.java

package com.qfedu.test;
import com.qfedu.pojo.order;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class Testorders {private SqlSessionFactory sf=null;private SqlSession session=null;@Beforepublic void setUp(){try {sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));session = sf.openSession();} catch (IOException e) {e.printStackTrace();}}@Afterpublic void tearDown(){if(session != null){session.close();session = null;}}@Testpublic void testsession(){System.out.println(session);}@Testpublic void testGetOrderByOid(){order orders= session.selectOne("com.qfedu.pojo.OrderMapper.getOrderByOid","2b674b3062ca11ea8ce9e50b8681f23a");System.out.println(orders);}}

7.运行结果

D:\jdk1.8.0_20\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\ideaiu\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar=57689:D:\ideaiu\IntelliJ IDEA 2017.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\ideaiu\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar;D:\ideaiu\IntelliJ IDEA 2017.1.4\plugins\junit\lib\junit-rt.jar;D:\jdk1.8.0_20\jre\lib\charsets.jar;D:\jdk1.8.0_20\jre\lib\deploy.jar;D:\jdk1.8.0_20\jre\lib\ext\access-bridge.jar;D:\jdk1.8.0_20\jre\lib\ext\cldrdata.jar;D:\jdk1.8.0_20\jre\lib\ext\dnsns.jar;D:\jdk1.8.0_20\jre\lib\ext\jaccess.jar;D:\jdk1.8.0_20\jre\lib\ext\jfxrt.jar;D:\jdk1.8.0_20\jre\lib\ext\localedata.jar;D:\jdk1.8.0_20\jre\lib\ext\nashorn.jar;D:\jdk1.8.0_20\jre\lib\ext\sunec.jar;D:\jdk1.8.0_20\jre\lib\ext\sunjce_provider.jar;D:\jdk1.8.0_20\jre\lib\ext\sunmscapi.jar;D:\jdk1.8.0_20\jre\lib\ext\sunpkcs11.jar;D:\jdk1.8.0_20\jre\lib\ext\zipfs.jar;D:\jdk1.8.0_20\jre\lib\javaws.jar;D:\jdk1.8.0_20\jre\lib\jce.jar;D:\jdk1.8.0_20\jre\lib\jfr.jar;D:\jdk1.8.0_20\jre\lib\jfxswt.jar;D:\jdk1.8.0_20\jre\lib\jsse.jar;D:\jdk1.8.0_20\jre\lib\management-agent.jar;D:\jdk1.8.0_20\jre\lib\plugin.jar;D:\jdk1.8.0_20\jre\lib\resources.jar;D:\jdk1.8.0_20\jre\lib\rt.jar;G:\java2\Days22Mybatis\target\test-classes;G:\java2\Days22Mybatis\target\classes;D:\chaox\maven\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\chaox\maven\repository\junit\junit\4.12\junit-4.12.jar;D:\chaox\maven\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\chaox\maven\repository\org\mybatis\mybatis\3.4.4\mybatis-3.4.4.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.qfedu.test.Testorders,testGetOrderByOid
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 17337681.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1088d51]
==>  Preparing: select * from orders where oid = ?;
==> Parameters: 2b674b3062ca11ea8ce9e50b8681f23a(String)
<==    Columns: oid, price, addr, payType, uid
<==        Row: 2b674b3062ca11ea8ce9e50b8681f23a, 30998, beijingxisanqi, zhifubao, 1
====>  Preparing: select * from user where uid = ?;
====> Parameters: 1(Integer)
<====    Columns: uid, name, pass, phone
<====        Row: 1, wukong, 8888, 11111111111
<====      Total: 1
====>  Preparing: select * from details where oid = ?;
====> Parameters: 2b674b3062ca11ea8ce9e50b8681f23a(String)
<====    Columns: did, count, pid, oid
<====        Row: c77dc0b362cb11ea8ce9e50b8681f23a, 2, a8c544f862c811ea8ce9e50b8681f23a, 2b674b3062ca11ea8ce9e50b8681f23a
======>  Preparing: select * from products where pid = ?;
======> Parameters: a8c544f862c811ea8ce9e50b8681f23a(String)
<======    Columns: pid, name, img, price, tid
<======        Row: a8c544f862c811ea8ce9e50b8681f23a, mac pro, mac.jpg, 21999, b36867bf62c411ea8ce9e50b8681f23a
========>  Preparing: select * from types where tid = ?;
========> Parameters: b36867bf62c411ea8ce9e50b8681f23a(String)
<========    Columns: tid, name
<========        Row: b36867bf62c411ea8ce9e50b8681f23a, digit
<========      Total: 1
<======      Total: 1
<====      Total: 1
<==      Total: 1
order{oid='2b674b3062ca11ea8ce9e50b8681f23a', price=30998.0, addr='beijingxisanqi', payType='zhifubao', u=User{uid=1, name='wukong', pass='8888', phone='11111111111'}, details=[Detail{did='c77dc0b362cb11ea8ce9e50b8681f23a', count=2, pro=Product{pid='a8c544f862c811ea8ce9e50b8681f23a', name='mac pro', img='mac.jpg', price=21999.0, t=Types{tid='b36867bf62c411ea8ce9e50b8681f23a', name='digit'}}}]}
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1088d51]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1088d51]
Returned connection 17337681 to pool.Process finished with exit code 0

Mybatis实现订单案例的五表联合操作相关推荐

  1. 一个mybatis动态 SQL查询的完整小案例。包含多表联合查询。

    多表联合查询 一个根据机场查询航线的例子.有两张表,机场表包含机场信息:机场id.机场名字.机场城市. 航班包含航线信息:航班id.飞机编号.飞行时间.票价.起飞机场id.降落机场id. 需要查询的结 ...

  2. mybatis学习五-mybatis的多表联合查询

    1. 一对一的关系 首先先看数据库 tb_user tb_orders 执行这条sql: 也就是查询所有的订单, 并每一个订单都联系上用户数据, 并再一次附上订单的id 也就是说, 从后面看, 就是每 ...

  3. MyBatis框架 多表联合查询实现

    三种方式: ①业务装配 对两个表编写单表查询语句,在业务层(Serivce)把查询的两个结果进行关联 ②使用Auto Mapping特性 在实现两表联合查询时通过别名完成映射,使用Maybatis的& ...

  4. mybatis多表查询出来的实体如何映射_mybatis进阶案例之多表查询

    mybatis进阶案例之多表查询 一.mybatis中表之间的关系 在数据库中,实体型之间的关系主要有如下几种: 1.一对一 如果对于实体集A中的每一个实体,实体集B中至多有一个(也可以没有)实体与之 ...

  5. springboot+mybatis+mysql 多表联合查询

    ###springboot+mybatis+mysql 多表查询 这里有两张表 用户表和用户信息表user.info 两个实体类展示他们一对一的关系 通过springboot注解的方式实现多表联合查询 ...

  6. SpringBoot+MyBatis多表联合查询

    SpringBoot+MyBatis多表联合查询 写在前面 联合查询在实际工作中用的并不多,因为很多表的数据比较大,或者说未来比较大的表,都要谨慎使用联合查询 数据准备 建表语句 create tab ...

  7. mybatis plus 查询排序,Mybatis Plus带多条件的多表联合、分页、排序查询

    背景 使用mybatis-plus单表操作十分方便,但是多表联合查询感觉又回归到xml时代了,我个人比较喜欢注解的方式,但是xml要更灵活 问题点:多表多条件联合查询 时间段查询 分页查询 sprin ...

  8. MyBatis 多表联合查询及优化

    关于优化 对于优化嘛,我这里简单的提几点,大家可以考虑一下.首先,就是对表的设计,在设计表初期,不仅仅要考虑到数据库的规范性,还好考虑到所谓的业务,以及对性能的影响,比如,如果从规范性角度考虑的话,可 ...

  9. Oracle数据库经典案例之学生选课四表联合查询

    四表联合查询之学生选课查询 问题及描述: 学生和成绩表 学生是主表,成绩是子表 课程和成绩表 课程是主表,成绩是子表 老师和课程表 老师是主表,课程是子表 1.学生表 Student(S#,Sname ...

  10. SQL语法(五) 多表联合查询

    前言 当需要获取的数据分布在多张中,考虑使用联合查询,本章将学习两种查询方式(sql92/sql99) 范例 1.笛卡儿积 将多个表的数据进行一一对应,所得到结果为多表的笛卡尔积.结果的数量为所有表的 ...

最新文章

  1. 用BRAT进行中文情感分析语料标注
  2. android heic图片,如何在Windows / MacOS / Android上打開HEIC照片
  3. 系统异常捕捉处理设计文档
  4. SQL Server数据库大型应用解决方案总结
  5. 使用WIF实现单点登录Part I——Windows Identity Foundation介绍及环境搭建 -摘自网络...
  6. 主流语言实现冒泡排序算法
  7. 开发时浏览器缓存问题
  8. python 网络爬虫 第一天
  9. 高端AP的拆解及保护电路设计
  10. 基于容器的虚拟化资源调度系统的架构设计
  11. codeforces topcoder 指南
  12. Java多线程--1--stop方法
  13. 最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
  14. 抓包软件charles
  15. Increasing trend of scientists to switch between topics论文翻译
  16. 谷歌、百度、必应的搜索技巧
  17. jenkins发送邮件添加附件
  18. memset()函数怎么用?
  19. 手机开机密码忘记了怎么办?
  20. ZDNS参与的《根服务器运行机构不端行为研究报告》正式发布,全球根服务器安全治理又进一步

热门文章

  1. 怎么找到电脑的打印机
  2. 关于T检验的各种问题
  3. VNC登陆灰屏 - fatal IO error 11 (Resource temporarily unavailable) on X server
  4. Flux、Mono、Reactor 实战(史上最全)
  5. EM算法 --入门级详解
  6. 方向键按键转发,模仿笔记本Fn按键
  7. 计算机辅助翻译实训心得,计算机辅助翻译实训报告格式.doc
  8. 【pytorch】微调技术
  9. MySQL基础——多表查询
  10. ES6数组得扩展与对象的扩展