zoukankan      html  css  js  c++  java
  • (七)mybatis之多对一关系(复杂)

     

    一、需求分析

    • 需求:查询所有消费者信息,关联查询订单及商品信息,订单明细信息中关联查询查商品信息。
    • 分析:一个消费者有多条订单,一条订单只有一个消费者但是有多条订单明细,一条订单明细只有一个商品信息。

    二、建数据库和实体对象

     Customer.java

    package com.shyroke.entity;
    
    import java.util.List;
    
    public class Customer {
        private int id;
        private String name;
        private String sex;
        private String birthday;
    
    //一个消费者有多条订单
    private List<Order> orderList;

    //隐藏get和set方法
    Order.java
    package com.shyroke.entity;
    
    import java.util.List;
    
    public class Order {
        private int orderId;
        private String Number;
        private String createTime;
        private String note;
    
        // 关联消费者信息,是一对一关系
        private Customer customer;
    
        // 关联订单明细信息,是一对多关系
        private List<OrderDesc> orderDescList;
    //隐藏get和set方法

    }

     OrderDesc.java

    package com.shyroke.entity;
    
    import java.lang.annotation.Inherited;
    
    public class OrderDesc {
        private int id;
        private String num;
        private String itemsId;
        private String ordersId;
    
        //关联商品信息,一对一关系
        private Items item;

    //隐藏get和set方法

    }
    Items.java
    package com.shyroke.entity;
    
    public class Items {
        private int id;
        private String name;
        private String time;
    //隐藏get和set方法
    }

    三、创建 CustomerMapper.java接口和 CustomerMapper.xml文件

    CustomerMapper.java

    package com.shyroke.mapper;
    
    import java.util.List;
    
    import com.shyroke.entity.Customer;
    
    public interface CustomerMapper {
            public List<Customer> selectCustomAllInfo();
    }

    CustomerMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.shyroke.mapper.CustomerMapper">
        <resultMap type="com.shyroke.entity.Customer" id="customerMap">
            <id column="user_id" property="id" />
            <result column="user_name" property="name" />
            <result column="user_sex" property="sex" />
            <result column="user_birthday" property="birthday" />
    
    <!-- Customer的成员属性orderList,即一个Customer消费者含有多个Order订单 --> <collection property="orderList" ofType="com.shyroke.entity.Order"> <id column="order_id" property="orderId" /> <result column="order_number" property="Number" /> <result column="order_createTime" property="createTime" /> <result column="order_note" property="note" />
    <!-- Order的成员属性orderDescList,即一个Order订单含有多个OrderDesc订单明细 --> <collection property="orderDescList" ofType="com.shyroke.entity.OrderDesc"> <id column="orders_desc_id" property="id" /> <result column="orders_desc_num" property="num" /> <result column="orders_desc_items_id" property="itemsId" /> <result column="orders_desc_orders_id" property="ordersId" />
    <!-- OrderDesc的成员属性item,即一个OrderDesc订单对应一个Items商品 --> <association property="item" javaType="com.shyroke.entity.Items"> <id column="item_id" property="id" /> <result column="item_name" property="name" /> <result column="item_time" property="time" /> </association> </collection> </collection> </resultMap> <select id="selectCustomAllInfo" resultMap="customerMap"> select customer.*,item.*,orders.*,ordersDesc.* from customer,item,orders,ordersDesc where orders.user_id=customer.user_id and ordersDesc.orders_desc_orders_id=orders.order_id and ordersDesc.orders_desc_items_id=item.item_id </select> </mapper>

    四、添加mapper到总配置文件

      mybatis-config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <!-- 配置数据库连接信息 -->
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver" />
                    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC" />
                    <property name="username" value="root" />
                    <property name="password" value="" />
                </dataSource>
            </environment>
        </environments>
        
        <mappers>
            <mapper class="com.shyroke.mapper.CustomerMapper"/>
        </mappers>
       
    </configuration>

    五、测试

    package com.shyrolk.firstMybatis;
    
    import java.awt.image.ImageProducer;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.plaf.synth.SynthSeparatorUI;
    
    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 com.shyroke.entity.Customer;
    import com.shyroke.entity.Order;
    import com.shyroke.mapper.CustomerMapper;
    
    /**
     * Hello world!
     *
     */
    public class App {
        public static void main(String[] args) throws IOException {
            String resource = "resource/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            SqlSession session = sessionFactory.openSession();
    
            CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
            List<Customer> customersList = customerMapper.selectCustomAllInfo();
    
            System.out.println(customersList);
    
        }
    }
  • 相关阅读:
    201301 JAVA题目0-1级, 华为笔试题
    病毒传播, 美团笔试题
    OC基础 字符串
    IOS OC语言基础(一)类的创建
    pyautogui 鼠标精灵模拟登陆
    ajax hook注入
    aiohttp异步请求
    IOS自学教程大全
    提高python工具包
    Kafka与Flume区别
  • 原文地址:https://www.cnblogs.com/shyroke/p/7606323.html
Copyright © 2011-2022 走看看