1. 程式人生 > >2018 IDEA mybatis 使用者-訂單-訂單詳情 → 互查

2018 IDEA mybatis 使用者-訂單-訂單詳情 → 互查

注:該篇演示的是通過一方條件獲得其餘兩方與之匹配的資料

一、sql

四、Dao

一、sql

/*
Navicat MySQL Data Transfer

Source Server         : 127
Source Server Version : 50022
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50022
File Encoding         : 65001

Date: 2018-10-12 21:28:25
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tb_order
-- ----------------------------
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE `tb_order` (
  `order_id` varchar(50) NOT NULL COMMENT '訂單ID',
  `order_totalprice` varchar(50) default NULL COMMENT '訂單總價',
  `order_address` varchar(50) default NULL COMMENT '收貨地址',
  `user_id` int(11) default NULL COMMENT '使用者ID',
  PRIMARY KEY  (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單表';

-- ----------------------------
-- Records of tb_order
-- ----------------------------
INSERT INTO `tb_order` VALUES ('VX20181012-01', '500.00', '湖南省長沙市嶽麓區', '1');
INSERT INTO `tb_order` VALUES ('VX20181012-02', '600.00', '湖南省長沙市天心區', '1');
INSERT INTO `tb_order` VALUES ('VX20181012-03', '800.00', '湖南省長沙市芙蓉區', '1');
INSERT INTO `tb_order` VALUES ('VX20181012-04', '225.00', '湖南省長沙市雨花區', '3');
INSERT INTO `tb_order` VALUES ('VX20181012-05', '450.00', '湖南省長沙市開福區', '3');

-- ----------------------------
-- Table structure for tb_orderinfo
-- ----------------------------
DROP TABLE IF EXISTS `tb_orderinfo`;
CREATE TABLE `tb_orderinfo` (
  `orderInfo_id` int(11) NOT NULL auto_increment COMMENT '訂單詳情',
  `orderInfo_shopname` varchar(20) default NULL COMMENT '商品名稱',
  `orderInfo_num` int(11) default NULL COMMENT '商品數量',
  `orderInfo_shoprice` double(20,0) default NULL COMMENT '商品單價',
  `order_id` varchar(50) default NULL COMMENT '訂單ID',
  PRIMARY KEY  (`orderInfo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單詳情表';

-- ----------------------------
-- Records of tb_orderinfo
-- ----------------------------
INSERT INTO `tb_orderinfo` VALUES ('1', '薯片', '100', '4', 'VX20181012-01');
INSERT INTO `tb_orderinfo` VALUES ('2', '蘋果', '10', '5', 'VX20181012-01');
INSERT INTO `tb_orderinfo` VALUES ('3', '香橙', '10', '5', 'VX20181012-01');
INSERT INTO `tb_orderinfo` VALUES ('4', '蘋果', '100', '5', 'VX20181012-02');
INSERT INTO `tb_orderinfo` VALUES ('5', '香橙', '20', '5', 'VX20181012-02');
INSERT INTO `tb_orderinfo` VALUES ('6', '核桃', '100', '8', 'VX20181012-03');
INSERT INTO `tb_orderinfo` VALUES ('7', '香橙', '5', '5', 'VX20181012-04');
INSERT INTO `tb_orderinfo` VALUES ('8', '蘋果', '40', '5', 'VX20181012-04');
INSERT INTO `tb_orderinfo` VALUES ('9', '核桃', '50', '8', 'VX20181012-05');
INSERT INTO `tb_orderinfo` VALUES ('10', '蘋果', '10', '5', 'VX20181012-05');

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `user_id` int(11) NOT NULL auto_increment COMMENT '使用者ID',
  `user_name` varchar(255) NOT NULL COMMENT '使用者名稱稱',
  `user_upwd` varchar(255) default NULL COMMENT '使用者密碼',
  PRIMARY KEY  (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='使用者表';

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', '檸澤', '123456');
INSERT INTO `tb_user` VALUES ('3', '檸萌', '123456');

二、pom.xml

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--引入mybatis依賴-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <!--引入mysql連線依賴-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>

    </dependencies>

三、Pojo

1.TbUser.java

package cn.kaxlm6.mybatis.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * (TbUser)TableInfo(obj=table:tb_user, name=TbUser, comment=null, fullColumn=[ColumnInfo(obj=column:user_id, name=userId, comment=使用者ID, type=java.lang.Integer, shortType=Integer, custom=false, ext={}), ColumnInfo(obj=column:user_name, name=userName, comment=使用者名稱稱, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:user_upwd, name=userUpwd, comment=使用者密碼, type=java.lang.String, shortType=String, custom=false, ext={})], pkColumn=[], otherColumn=[], savePackageName=cn.kaxlm6.mybatis, savePath=./src/main/java/cn/kaxlm6/mybatis, saveModelName=mybatisProject)
 *
 * @author xlm
 * @since 2018-10-12 18:31:15
 */
public class TbUser implements Serializable {

    private static final long serialVersionUID = 773064479460702874L;

    /**
     * 使用者ID
     */
    private Integer userId;

    /**
     * 使用者名稱稱
     */
    private String userName;

    /**
     * 使用者密碼
     */
    private String userUpwd;

    private List<TbOrder> tbOrderList;
    
    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getUserUpwd() {
        return userUpwd;
    }

    public void setUserUpwd(String userUpwd) {
        this.userUpwd = userUpwd;
    }

    public List<TbOrder> getTbOrderList() {
        return tbOrderList;
    }

    public void setTbOrderList(List<TbOrder> tbOrderList) {
        this.tbOrderList = tbOrderList;
    }

    @Override
    public String toString() {
        return "TbUser{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userUpwd='" + userUpwd + '\'' +
                '}';
    }
}

2.TbOrder.java

package cn.kaxlm6.mybatis.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * (TbOrder)TableInfo(obj=table:tb_order, name=TbOrder, comment=null, fullColumn=[ColumnInfo(obj=column:order_id, name=orderId, comment=訂單ID, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:order_totalprice, name=orderTotalprice, comment=訂單總價, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:order_address, name=orderAddress, comment=收貨地址, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:user_id, name=userId, comment=使用者ID, type=java.lang.Integer, shortType=null, custom=false, ext={})], pkColumn=[ColumnInfo(obj=column:order_id, name=orderId, comment=訂單ID, type=java.lang.String, shortType=String, custom=false, ext={})], otherColumn=[ColumnInfo(obj=column:order_totalprice, name=orderTotalprice, comment=訂單總價, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:order_address, name=orderAddress, comment=收貨地址, type=java.lang.String, shortType=String, custom=false, ext={}), ColumnInfo(obj=column:user_id, name=userId, comment=使用者ID, type=java.lang.Integer, shortType=null, custom=false, ext={})], savePackageName=cn.kaxlm6.mybatis, savePath=./src/main/java/cn/kaxlm6/mybatis, saveModelName=mybatisProject)
 *
 * @author xlm
 * @since 2018-10-12 18:31:15
 */
public class TbOrder implements Serializable {

    private static final long serialVersionUID = -16223985121815681L;

    /**
     * 訂單ID
     */
    private String orderId;

    /**
     * 訂單總價
     */
    private String orderTotalprice;

    /**
     * 收貨地址
     */
    private String orderAddress;

    /**
     * 使用者ID
     */
    private Integer userId;

    private List<TbUser> tbUserList;

    private List<TbOrderinfo> tbOrderinfoList;

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getOrderTotalprice() {
        return orderTotalprice;
    }

    public void setOrderTotalprice(String orderTotalprice) {
        this.orderTotalprice = orderTotalprice;
    }

    public String getOrderAddress() {
        return orderAddress;
    }

    public void setOrderAddress(String orderAddress) {
        this.orderAddress = orderAddress;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public List<TbUser> getTbUserList() {
        return tbUserList;
    }

    public void setTbUserList(List<TbUser> tbUserList) {
        this.tbUserList = tbUserList;
    }

    public List<TbOrderinfo> getTbOrderinfoList() {
        return tbOrderinfoList;
    }

    public void setTbOrderinfoList(List<TbOrderinfo> tbOrderinfoList) {
        this.tbOrderinfoList = tbOrderinfoList;
    }

    @Override
    public String toString() {
        return "TbOrder{" +
                "orderId='" + orderId + '\'' +
                ", orderTotalprice='" + orderTotalprice + '\'' +
                ", orderAddress='" + orderAddress + '\'' +
                ", userId=" + userId +
                '}';
    }
}

3.TbOrderinfo.java

package cn.kaxlm6.mybatis.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * (TbOrderinfo)TableInfo(obj=table:tb_orderinfo, name=TbOrderinfo, comment=null, fullColumn=[ColumnInfo(obj=column:orderInfo_id, name=orderinfoId, comment=訂單詳情, type=java.lang.Integer, shortType=Integer, custom=false, ext={}), ColumnInfo(obj=column:orderInfo_shopname, name=orderinfoShopname, comment=商品名稱, type=java.lang.String, shortType=null, custom=false, ext={}), ColumnInfo(obj=column:orderInfo_num, name=orderinfoNum, comment=商品數量, type=java.lang.Integer, shortType=Integer, custom=false, ext={}), ColumnInfo(obj=column:orderInfo_shoprice, name=orderinfoShoprice, comment=商品單價, type=java.lang.Object, shortType=null, custom=false, ext={}), ColumnInfo(obj=column:order_id, name=orderId, comment=訂單ID, type=java.lang.String, shortType=null, custom=false, ext={})], pkColumn=[ColumnInfo(obj=column:orderInfo_id, name=orderinfoId, comment=訂單詳情, type=java.lang.Integer, shortType=Integer, custom=false, ext={})], otherColumn=[ColumnInfo(obj=column:orderInfo_shopname, name=orderinfoShopname, comment=商品名稱, type=java.lang.String, shortType=null, custom=false, ext={}), ColumnInfo(obj=column:orderInfo_num, name=orderinfoNum, comment=商品數量, type=java.lang.Integer, shortType=Integer, custom=false, ext={}), ColumnInfo(obj=column:orderInfo_shoprice, name=orderinfoShoprice, comment=商品單價, type=java.lang.Object, shortType=null, custom=false, ext={}), ColumnInfo(obj=column:order_id, name=orderId, comment=訂單ID, type=java.lang.String, shortType=null, custom=false, ext={})], savePackageName=cn.kaxlm6.mybatis, savePath=./src/main/java/cn/kaxlm6/mybatis, saveModelName=mybatisProject)
 *
 * @author xlm
 * @since 2018-10-12 20:27:15
 */
public class TbOrderinfo implements Serializable {

    private static final long serialVersionUID = -29512783426681042L;

    /**
     * 訂單詳情
     */
    private Integer orderinfoId;

    /**
     * 商品名稱
     */
    private String orderinfoShopname;

    /**
     * 商品數量
     */
    private Integer orderinfoNum;

    /**
     * 商品單價
     */
    private Object orderinfoShoprice;

    /**
     * 訂單ID
     */
    private String orderId;

    private List<TbOrder> tbOrderList;
    
    public Integer getOrderinfoId() {
        return orderinfoId;
    }

    public void setOrderinfoId(Integer orderinfoId) {
        this.orderinfoId = orderinfoId;
    }
    
    public String getOrderinfoShopname() {
        return orderinfoShopname;
    }

    public void setOrderinfoShopname(String orderinfoShopname) {
        this.orderinfoShopname = orderinfoShopname;
    }
    
    public Integer getOrderinfoNum() {
        return orderinfoNum;
    }

    public void setOrderinfoNum(Integer orderinfoNum) {
        this.orderinfoNum = orderinfoNum;
    }
    
    public Object getOrderinfoShoprice() {
        return orderinfoShoprice;
    }

    public void setOrderinfoShoprice(Object orderinfoShoprice) {
        this.orderinfoShoprice = orderinfoShoprice;
    }
    
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public List<TbOrder> getTbOrderList() {
        return tbOrderList;
    }

    public void setTbOrderList(List<TbOrder> tbOrderList) {
        this.tbOrderList = tbOrderList;
    }

    @Override
    public String toString() {
        return "TbOrderinfo{" +
                "orderinfoId=" + orderinfoId +
                ", orderinfoShopname='" + orderinfoShopname + '\'' +
                ", orderinfoNum=" + orderinfoNum +
                ", orderinfoShoprice=" + orderinfoShoprice +
                ", orderId='" + orderId + '\'' +
                '}';
    }
}

四、Dao

1.TbUserDao.java

package cn.kaxlm6.mybatis.dao;

import cn.kaxlm6.mybatis.pojo.TbUser;

/**
 * (TbUser)表資料庫訪問層
 *
 * @author xlm
 * @since 2018-10-12 18:51:26
 */
public interface TbUserDao {

    /**
     * 通過ID查詢單條資料
     *
     * @param userId 主鍵
     * @return 例項物件
     */
    TbUser sel(Integer userId);

    /**
     * 通過ID查詢單條資料
     *
     * @param userId 主鍵
     * @return 例項物件
     */
    TbUser queryById(Integer userId);

}

2.TbOrderDao.java

package cn.kaxlm6.mybatis.dao;

import cn.kaxlm6.mybatis.pojo.TbOrder;

/**
 * (TbOrder)表資料庫訪問層
 *
 * @author xlm
 * @since 2018-10-12 18:31:15
 */
public interface TbOrderDao {

    /**
     * 通過ID查詢單條資料
     *
     * @param userId 主鍵
     * @return 例項物件
     */
    TbOrder s1(String userId);

    /**
     * 通過ID查詢單條資料
     *
     * @param orderId 主鍵
     * @return 例項物件
     */
    TbOrder sel(String orderId);

    /**
     * 通過ID查詢單條資料
     *
     * @param orderId 主鍵
     * @return 例項物件
     */
    TbOrder queryById(String orderId);

}

3.TbOrderinfoDao.java

package cn.kaxlm6.mybatis.dao;

import cn.kaxlm6.mybatis.pojo.TbOrderinfo;
import java.util.List;

/**
 * (TbOrderinfo)表資料庫訪問層
 *
 * @author xlm
 * @since 2018-10-12 18:31:15
 */
public interface TbOrderinfoDao {

    /**
     * 通過ID查詢單條資料
     *
     * @param orderId 主鍵
     * @return 例項物件
     */
    TbOrderinfo s2(String orderId);

    /**
     * 通過ID查詢單條資料
     *
     * @param orderinfoShopname 名稱
     * @return 例項物件
     */
    List<TbOrderinfo> sel(String orderinfoShopname);

    /**
     * 通過ID查詢單條資料
     *
     * @param orderinfoId 主鍵
     * @return 例項物件
     */
    TbOrderinfo queryById(Integer orderinfoId);

}

五、Mapper

1.TbUserMapper.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="cn.kaxlm6.mybatis.dao.TbUserDao">

    <!-- class -->
    <resultMap type="cn.kaxlm6.mybatis.pojo.TbUser" id="TbUserClass">
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userUpwd" column="user_upwd" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap type="cn.kaxlm6.mybatis.pojo.TbUser" id="TbUserMapper">
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userUpwd" column="user_upwd" jdbcType="VARCHAR"/>
        <collection property="tbOrderList" column="user_id" ofType="cn.kaxlm6.mybatis.pojo.TbOrder" fetchType="lazy" select="cn.kaxlm6.mybatis.dao.TbOrderDao.s1" />
    </resultMap>

    <select id="sel" resultMap="TbUserMapper">
        select
          user_id, user_name, user_upwd
        from test.tb_user
        where user_id = #{userId}
    </select>

    <!--查詢單個-->
    <select id="queryById" resultMap="TbUserClass">
        select
          user_id, user_name, user_upwd
        from test.tb_user
        where user_id = #{userId}
    </select>

</mapper>

2.TbOrderMapper.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="cn.kaxlm6.mybatis.dao.TbOrderDao">

    <!-- class -->
    <resultMap type="cn.kaxlm6.mybatis.pojo.TbOrder" id="TbOrderClass">
        <result property="orderId" column="order_id" jdbcType="VARCHAR"/>
        <result property="orderTotalprice" column="order_totalprice" jdbcType="VARCHAR"/>
        <result property="orderAddress" column="order_address" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
    </resultMap>

    <resultMap type="cn.kaxlm6.mybatis.pojo.TbOrder" id="TbOrderMapper">
        <result property="orderId" column="order_id" jdbcType="VARCHAR"/>
        <result property="orderTotalprice" column="order_totalprice" jdbcType="VARCHAR"/>
        <result property="orderAddress" column="order_address" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <collection property="tbUserList" column="user_id" ofType="cn.kaxlm6.mybatis.pojo.TbUser" fetchType="lazy" select="cn.kaxlm6.mybatis.dao.TbUserDao.queryById" />
        <collection property="tbOrderinfoList" column="order_id" ofType="cn.kaxlm6.mybatis.pojo.TbOrderinfo" fetchType="lazy" select="cn.kaxlm6.mybatis.dao.TbOrderinfoDao.s2" />
    </resultMap>

    <select id="s1" resultMap="TbOrderMapper">
        select
          order_id, order_totalprice, order_address, user_id
        from test.tb_order
        where user_id = #{userId}
    </select>

    <select id="sel" resultMap="TbOrderMapper">
        select
          order_id, order_totalprice, order_address, user_id
        from test.tb_order
        where order_id = #{orderId}
    </select>

    <!--查詢單個-->
    <select id="queryById" resultMap="TbOrderClass">
        select
          order_id, order_totalprice, order_address, user_id
        from test.tb_order
        where order_id = #{orderId}
    </select>

</mapper>

3.TbOrderinfoMapper.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="cn.kaxlm6.mybatis.dao.TbOrderinfoDao">

    <!-- class -->
    <resultMap type="cn.kaxlm6.mybatis.pojo.TbOrderinfo" id="TbOrderinfoClass">
        <result property="orderinfoId" column="orderInfo_id" jdbcType="INTEGER"/>
        <result property="orderinfoShopname" column="orderInfo_shopname" jdbcType="VARCHAR"/>
        <result property="orderinfoNum" column="orderInfo_num" jdbcType="INTEGER"/>
        <result property="orderinfoShoprice" column="orderInfo_shoprice" jdbcType="OTHER"/>
        <result property="orderId" column="order_id" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap type="cn.kaxlm6.mybatis.pojo.TbOrderinfo" id="TbOrderinfoMapper">
        <result property="orderinfoId" column="orderInfo_id" jdbcType="INTEGER"/>
        <result property="orderinfoShopname" column="orderInfo_shopname" jdbcType="VARCHAR"/>
        <result property="orderinfoNum" column="orderInfo_num" jdbcType="INTEGER"/>
        <result property="orderinfoShoprice" column="orderInfo_shoprice" jdbcType="OTHER"/>
        <result property="orderId" column="order_id" jdbcType="VARCHAR"/>
        <collection property="tbOrderList" column="order_id" ofType="cn.kaxlm6.mybatis.pojo.TbOrder" fetchType="lazy" select="cn.kaxlm6.mybatis.dao.TbOrderDao.sel" />
    </resultMap>

    <select id="sel" resultMap="TbOrderinfoMapper">
        select
          orderInfo_id, orderInfo_shopname, orderInfo_num, orderInfo_shoprice, order_id
        from test.tb_orderinfo
        where orderInfo_shopname = #{orderinfoShopname}
    </select>

    <select id="s2" resultMap="TbOrderinfoClass">
        select
          orderInfo_id, orderInfo_shopname, orderInfo_num, orderInfo_shoprice, order_id
        from test.tb_orderinfo
        where order_id = #{orderId}
    </select>

    <!--查詢單個-->
    <select id="queryById" resultMap="TbOrderinfoClass">
        select
          orderInfo_id, orderInfo_shopname, orderInfo_num, orderInfo_shoprice, order_id
        from test.tb_orderinfo
        where orderInfo_id = #{orderinfoId}
    </select>

</mapper>

六、resources.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.user=root
jdbc.password=200206

七、mybatisConfig.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>

    <!--0. 引入外部資原始檔 -->
    <properties resource="resources.properties" />

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>

    <!--1. 配置資料庫環境,預設使用development資料庫構建環境-->
    <environments default="development">
        <!--配置環境變數-->
        <environment id="development">
            <!--配置事務管理器型別
            type="JDBC" 指直接簡單使用了JDBC的提交和滾回設定
            type="MANAGED" 指讓容器實現對事務的管理
            -->
            <transactionManager type="JDBC"/>
            <!--配置資料來源-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClass}"/>
                <property name="url" value="${jdbc.jdbcUrl}"/>
                <property name="username" value="${jdbc.user}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--2. 載入對映器:實體類物件/關係對映檔案-->
    <mappers>
        <!-- 注:整合後才能用*載入多個 -->
        <mapper resource="cn/kaxlm6/mybatis/mapper/TbUserMapper.xml"/>
        <mapper resource="cn/kaxlm6/mybatis/mapper/TbOrderMapper.xml"/>
        <mapper resource="cn/kaxlm6/mybatis/mapper/TbOrderinfoMapper.xml"/>
    </mappers>

</configuration>

八、Tests.java - 測試

package cn.kaxlm6.mybatis.test;

import cn.kaxlm6.mybatis.dao.TbOrderDao;
import cn.kaxlm6.mybatis.dao.TbOrderinfoDao;
import cn.kaxlm6.mybatis.dao.TbUserDao;
import cn.kaxlm6.mybatis.pojo.TbOrder;
import cn.kaxlm6.mybatis.pojo.TbOrderinfo;
import cn.kaxlm6.mybatis.pojo.TbUser;
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 java.io.InputStream;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author xlm
 * description:
 * path: mybatisProject-cn.kaxlm6.mybatis.test-Tests
 * date: 2018/10/3 18:51
 * version: 02.06
 * To change this template use File | Settings | File Templates.
 */
public class Tests {

    public static void main(String[] args) throws Exception {

        InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        System.out.println();

        // 1.根據使用者ID獲得使用者訂單及該訂單詳情

        System.out.println("1.根據使用者ID獲得使用者訂單及該訂單詳情\n");

        TbUserDao mapper = sqlSession.getMapper(TbUserDao.class);

        Integer userId = 1;

        TbUser tbUser = mapper.sel(userId);

        System.out.println("\"" + tbUser.getUserName() + "\" 使用者的訂單如下\n");

        for (TbOrder tb:tbUser.getTbOrderList()){
            System.out.println(tb);
            System.out.println("\t" + tb.getOrderId() + "訂單詳情如下:");
            for (TbOrderinfo ti:tb.getTbOrderinfoList() ){
                System.out.println("\t\t" + ti);
            }
        }

        System.out.println("\n");

        // 2.根據訂單號獲得使用者資訊及訂單資訊

        System.out.println("2.根據訂單號獲得使用者資訊及訂單資訊\n");

        TbOrderDao mapper1 = sqlSession.getMapper(TbOrderDao.class);

        String orderNo = "VX20181012-01";

        TbOrder tbOrder = mapper1.sel(orderNo);

        System.out.println(orderNo + "訂單的使用者為:" + tbOrder.getTbUserList().get(0).getUserName());

        System.out.println(tbOrder.getOrderId() + "訂單詳情如下:");
        for (TbOrderinfo ti:tbOrder.getTbOrderinfoList() ){
            System.out.println("\t" + ti);
        }

        System.out.println("\n");

        // 3.根據商品名稱獲得已購買該商品的訂單及使用者

        System.out.println("3.根據商品名稱獲得已購買該商品的訂單及使用者\n");

        TbOrderinfoDao mapper2 = sqlSession.getMapper(TbOrderinfoDao.class);

        String orderinfoShopname = "核桃";

        List<TbOrderinfo> tbOrderinfoList = mapper2.sel(orderinfoShopname);

        System.out.println("購買名為 \"" + orderinfoShopname + "\" 商品的訂單如下:\n");
        for (TbOrderinfo tbOrderinfo:tbOrderinfoList ){
            for (TbOrder to:tbOrderinfo.getTbOrderList() ){
                System.out.println("\t使用者為:" + to.getTbUserList().get(0).getUserName());
                System.out.println("\t訂單為:" + to);
                System.out.println();
            }
        }

        sqlSession.commit();
        sqlSession.close();

    }
}

九、結果檢視