10.mybatis_mapping_1tomore(传智播客)

需求:查询所有订单信息关联查询下单用户及订单明细信息。

需求分析:订单信息与订单明细为一对多关系,订单信息关联查询用户信息为一对一关系。
在orders.java类中添加List属性,最终会将订单信息映射到orders中,订单所对应的订单明细映射到orders中的orderDetails属性中。

开发步骤

1.mapper接口

public interface OrderUserOrderDetailMapper {
    List<Orders> getOrderUserOrderDetail();
}

2.sql映射文件

<?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="mapper.OrderUserOrderDetailMapper">
	<select id="getOrderUserOrderDetail" resultMap="orderUserOrderDetailMap">
	    select orders.*,user.username,user.address,orderDetail.orderDetail_id,orderDetail.item_id,orderDetail.item_num
	    from orders inner join user on orders.user_id = user.id
	    inner join orderDetail on orders.order_id = orderDetail.order_id
	</select>
	
	<resultMap  id="orderUserMap" type="Orders">
	    <id column="order_id" property="order_id"/>
	    <result column="order_no" property="order_no"/>
	    <result column="note" property="note"/>
	    <association javaType="User" property="user">
	        <id column="user_id" property="id"/>
	        <result column="username" property="username"/>
	        <result column="address" property="address"/>
	    </association>
	</resultMap>
	
	<resultMap id="orderUserOrderDetailMap" extends="orderUserMap" type="Orders">
	    <collection ofType="OrderDetail" property="orderDetailList">
	        <id column="orderDetail_id" property="orderDetail_id"/>
	        <result column="orderDetail_id" property="orderDetail_id"/>
	        <result column="order_id" property="order_id"/>
	        <result column="item_id" property="item_id"/>
	        <result column="item_num" property="item_num"/>
	    </collection>
	</resultMap>
</mapper>

3.编写po类

public class Orders {
    private int order_id;
    private int user_id;
    private String order_no;
    private String createTime;
    private String note;
    private User user;
    private List<OrderDetail> orderDetailList;
    //get和set方法......
}

public class User {
    //属性名要和数据库表的字段对应
    private int id;
    private String username;// 用户姓名
    private String sex;// 性别
    private String birthday;// 生日
    private String address;// 地址
    //get和set方法......
}

public class OrderDetail {
    private int orderDetail_id;
    private int order_id;
    private int item_id;
    private int item_num;
    //get和set方法......
}

4.测试

public class Test {
    private static final Logger log = Logger.getLogger(Test.class.getName());
    public static void main(String[] args) throws Exception {
        // 1.获取mybatis配置文件的路径
        String resource = "config/mybatis.xml";
        // 2.得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 3.根据mybatis配置文件的信息创建会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderUserOrderDetailMapper orderUserOrderDetailMapper = sqlSession.getMapper(OrderUserOrderDetailMapper.class);

        List<Orders> ordersList = orderUserOrderDetailMapper.getOrderUserOrderDetail();
        Iterator<Orders> iterator = ordersList.iterator();
        while(iterator.hasNext()){
            Orders order = iterator.next();
            System.out.println("username:"+order.getUser().getUsername());
            System.out.println("address:"+order.getUser().getAddress());
            System.out.println("order_no:"+ order.getOrder_no());

            List<OrderDetail> orderDetailList = order.getOrderDetailList();
            System.out.println("orderDetailList......"+orderDetailList.size());
            Iterator<OrderDetail> iterator1 = orderDetailList.iterator();
            while(iterator1.hasNext()){
                OrderDetail orderDetail =  iterator1.next();
                System.out.print("order_id:"+orderDetail.getOrder_id());
                System.out.print(",item_id:"+orderDetail.getItem_id());
                System.out.println(",item_num:"+orderDetail.getItem_num());
            }
            System.out.println();
        }
        sqlSession.close();
    }
}

运行测试结果如下

"C:\Program Files\Java\jdk1.8.0_51\bin\java.exe" "-javaagent:D:\IDEA\IntelliJ IDEA 2018.2\lib\idea_rt.jar=57970:D:\IDEA\IntelliJ IDEA 2018.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_51\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_51\jre\lib\rt.jar;E:\代码存档\mybatis\9.mybatis_mapping_1tomore\target\test-classes;E:\代码存档\mybatis\9.mybatis_mapping_1tomore\target\classes;E:\repository\junit\junit\4.11\junit-4.11.jar;E:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar;E:\repository\org\mybatis\mybatis\3.2.2\mybatis-3.2.2.jar;E:\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar" Test
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po
DEBUG [main] - Reader entry: OrderDetail.class
DEBUG [main] - Reader entry: Orders.class
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Listing file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/OrderDetail.class
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/OrderDetail.class
DEBUG [main] - Reader entry: ����   3 @
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/Orders.class
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/Orders.class
DEBUG [main] - Reader entry: ����   3 D
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/User.class
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/po/User.class
DEBUG [main] - Reader entry: ����   3 H
DEBUG [main] - Checking to see if class po.OrderDetail matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class po.Orders matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class po.User matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper
DEBUG [main] - Reader entry: OrderUserOrderDetailMapper.class
DEBUG [main] - Reader entry: OrderUserOrderDetailMapper.xml
DEBUG [main] - Listing file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper/OrderUserOrderDetailMapper.class
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper/OrderUserOrderDetailMapper.class
DEBUG [main] - Reader entry: ����   3  	 
DEBUG [main] - Find JAR URL: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper/OrderUserOrderDetailMapper.xml
DEBUG [main] - Not a JAR: file:/E:/%e4%bb%a3%e7%a0%81%e5%ad%98%e6%a1%a3/mybatis/9.mybatis_mapping_1tomore/target/classes/mapper/OrderUserOrderDetailMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class mapper.OrderUserOrderDetailMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 152134087.
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@91161c7]
DEBUG [main] - ==>  Preparing: select orders.*,user.username,user.address,orderDetail.orderDetail_id,orderDetail.item_id,orderDetail.item_num from orders inner join user on orders.user_id = user.id inner join orderDetail on orders.order_id = orderDetail.order_id 
DEBUG [main] - ==> Parameters: 
username:steven
address:nanjing
order_no:2018080400001
orderDetailList......2
order_id:1,item_id:1,item_num:1
order_id:1,item_id:2,item_num:1

username:cherry
address:shanghai
order_no:2018080600001
orderDetailList......3
order_id:2,item_id:1,item_num:1
order_id:2,item_id:2,item_num:1
order_id:2,item_id:3,item_num:1

DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@91161c7]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@91161c7]
DEBUG [main] - Returned connection 152134087 to pool.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值