MyBatis实现多表查询——案例:订单

一、数据库表创建

  • users用户表、products商品表、types商品类型表、orders订单表、details订单详情表
  • 表结构.png
  • details.png
  • orders.png
  • products.png
  • types.png
  • users.png

二、使用MyBatis完成相应的查询功能

  • pom.xml 文件添加 junit,mysql, mybatis 依赖。
    
    
        4.0.0
        com
        Mybatis
        1.0-SNAPSHOT
        
            
                mysql
                mysql-connector-java
                8.0.18
            
            
                junit
                junit
                4.12
            
            
                org.mybatis
                mybatis
                3.4.6
            
        
    
    
  • 设置MyBatis的配置文件 mybatis.xml
    
    
    
        
            
        
        
            
        
        
            
        
        
            
                
                
                    
                    
                    
                    
                
            
        
        
            
            
            
            
            
        
    
    
  • db.properties数据库的配置文件
    • mysql版本不同driver要修改
    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3307/mysqls?serverTimezone=UTC
    user=root
    pass=123456
    
  • 设计 pojo 相关类
    • Users.java
      public class Users {
          private int uid;
          private String name;
          private String pass;
          private String 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; }
          @Override
          public String toString() {
              final StringBuffer sb = new StringBuffer("Users{");
              sb.append("uid=").append(uid);
              sb.append(", name='").append(name).append('\'');
              sb.append(", pass='").append(pass).append('\'');
              sb.append(", phone='").append(phone).append('\'');
              sb.append('}');
              return sb.toString();
          }
      }
      
    • Order.java
      public class Order {
          private String oid;
          private double price;
          private String addr;
          private String payType;
          private Users u;
          private List 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 Users getU() { return u; }
          public void setU(Users u) { this.u = u; }
          public List getDetails() { return details; }
          public void setDetails(List details) { this.details = details; }
          @Override
          public String toString() {
              final StringBuffer sb = new StringBuffer("Order{");
              sb.append("oid='").append(oid).append('\'');
              sb.append(", price=").append(price);
              sb.append(", addr='").append(addr).append('\'');
              sb.append(", payType='").append(payType).append('\'');
              sb.append(", u=").append(u);
              sb.append(", details=").append(details);
              sb.append('}');
              return sb.toString();
          }
      }
      
    • Product.java
      public class Product {
          private String pid;
          private String name;
          private String img;
          private double price;
          private Types 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; }
          @Override
          public String toString() {
              final StringBuffer sb = new StringBuffer("Product{");
              sb.append("pid='").append(pid).append('\'');
              sb.append(", name='").append(name).append('\'');
              sb.append(", img='").append(img).append('\'');
              sb.append(", price=").append(price);
              sb.append(", t=").append(t);
              sb.append('}');
              return sb.toString();
          }
      }
      
    • Types.java
      public class Types {
          private String tid;
          private String 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; }
          @Override
          public String toString() {
              final StringBuffer sb = new StringBuffer("Types{");
              sb.append("tid='").append(tid).append('\'');
              sb.append(", name='").append(name).append('\'');
              sb.append('}');
              return sb.toString();
          }
      }
      
    • Detail.java
      public class Detail {
          private String did;
          private int count;
          private Product 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; }
          @Override
          public String toString() {
              final StringBuffer sb = new StringBuffer("Detail{");
              sb.append("did='").append(did).append('\'');
              sb.append(", count=").append(count);
              sb.append(", pro=").append(pro);
              sb.append('}');
              return sb.toString();
          }
      }
      
  • 设置映射文件mapper
    • namespace和id组合唯一,可自定义
    • 使用resultMap进行联合查询,对应pojo内的属性名(property)和数据库表中的列名相同,可省略不写result标签
    • 使用association标签进行对一的表结构联合查询
    • 使用collection标签进行对多的表结构联合查询
    • UserMapper.xml
      
      
      
          
      
      
    • OrderMapper.xml
      
      
      
          
          
              
              
              
              
              
              
          
      
      
    • DetailMapper.xml
      
      
      
          
          
              
              
              
          
      
      
    • TypeMapper.xml
      
      
      
          
      
      
    • ProductMapper.xml
      
      
      
          
          
              
              
          
      
      
  • 测试类 Test.java
    public class TestOrders {
        private SqlSessionFactory sf = null;
        private SqlSession session = null;
        @Before
        public void setUp(){
            try {
                sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
                session = sf.openSession();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @After
        public void tearDown(){
            if(session != null){
                session.close();
                session = null;
            }
        }
        @Test
        public void testGetOrderByOid(){
            Order order = session.selectOne("com.pojo.OrderMapper.getOrderByOid", "bfa2974d62cc11eaa62b8cec4b26e06a");
            System.out.println(order);
        }
    }
    
  • 测试结果
    Opening JDBC Connection
    Created connection 1881561036.
    Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@702657cc]
    ==>  Preparing: select * from orders where oid = ? 
    ==> Parameters: bfa2974d62cc11eaa62b8cec4b26e06a(String)
    <==    Columns: oid, uid, price, payType, addr
    <==        Row: bfa2974d62cc11eaa62b8cec4b26e06a, 1, 4799.0, AAA, 支付宝
    ====>  Preparing: select * from users where uid = ? 
    ====> Parameters: 1(Integer)
    <====    Columns: uid, name, pass, phone
    <====        Row: 1, LKT, 123456, 18111111111
    <====      Total: 1
    ====>  Preparing: select * from details where oid = ? 
    ====> Parameters: bfa2974d62cc11eaa62b8cec4b26e06a(String)
    <====    Columns: did, count, oid, pid
    <====        Row: 304adbc162cd11eaa62b8cec4b26e06a, 2, bfa2974d62cc11eaa62b8cec4b26e06a, 4445ff1c62cc11eaa62b8cec4b26e06a
    ======>  Preparing: select * from products where pid = ? 
    ======> Parameters: 4445ff1c62cc11eaa62b8cec4b26e06a(String)
    <======    Columns: pid, name, img, price, tid
    <======        Row: 4445ff1c62cc11eaa62b8cec4b26e06a, BlackShark2pro, BlackShark.jpg, 3299.0, baa46abb62cb11eaa62b8cec4b26e06a
    ========>  Preparing: select * from types where tid = ? 
    ========> Parameters: baa46abb62cb11eaa62b8cec4b26e06a(String)
    <========    Columns: tid, name
    <========        Row: baa46abb62cb11eaa62b8cec4b26e06a, AAA
    <========      Total: 1
    <======      Total: 1
    <====        Row: 304bca1b62cd11eaa62b8cec4b26e06a, 1, bfa2974d62cc11eaa62b8cec4b26e06a, 70106ee762cc11eaa62b8cec4b26e06a
    ======>  Preparing: select * from products where pid = ? 
    ======> Parameters: 70106ee762cc11eaa62b8cec4b26e06a(String)
    <======    Columns: pid, name, img, price, tid
    <======        Row: 70106ee762cc11eaa62b8cec4b26e06a, BlackShark3pro, BlackShark3pro.jpg, 4799.0, baa49b9762cb11eaa62b8cec4b26e06a
    ========>  Preparing: select * from types where tid = ? 
    ========> Parameters: baa49b9762cb11eaa62b8cec4b26e06a(String)
    <========    Columns: tid, name
    <========        Row: baa49b9762cb11eaa62b8cec4b26e06a, BBB
    <========      Total: 1
    <======      Total: 1
    <====        Row: 88d56e6a62cd11eaa62b8cec4b26e06a, 1, bfa2974d62cc11eaa62b8cec4b26e06a, 70106ee762cc11eaa62b8cec4b26e06a
    <====      Total: 3
    <==      Total: 1
    Order{oid='bfa2974d62cc11eaa62b8cec4b26e06a', price=4799.0, addr='支付宝', payType='AAA', u=Users{uid=1, name='LKT', pass='123456', phone='18111111111'}, details=[Detail{did='304adbc162cd11eaa62b8cec4b26e06a', count=2, pro=Product{pid='4445ff1c62cc11eaa62b8cec4b26e06a', name='BlackShark2pro', img='BlackShark.jpg', price=3299.0, t=Types{tid='baa46abb62cb11eaa62b8cec4b26e06a', name='AAA'}}}, Detail{did='304bca1b62cd11eaa62b8cec4b26e06a', count=1, pro=Product{pid='70106ee762cc11eaa62b8cec4b26e06a', name='BlackShark3pro', img='BlackShark3pro.jpg', price=4799.0, t=Types{tid='baa49b9762cb11eaa62b8cec4b26e06a', name='BBB'}}}, Detail{did='88d56e6a62cd11eaa62b8cec4b26e06a', count=1, pro=Product{pid='70106ee762cc11eaa62b8cec4b26e06a', name='BlackShark3pro', img='BlackShark3pro.jpg', price=4799.0, t=Types{tid='baa49b9762cb11eaa62b8cec4b26e06a', name='BBB'}}}]}
    Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@702657cc]
    Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@702657cc]
    Returned connection 1881561036 to pool.
    

你可能感兴趣的:(MyBatis实现多表查询——案例:订单)