码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis对象关联关系---- association与collection

时间:2015-08-03 16:45:57      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

Mybatis处理“一对多”的关系时,需要用到associasion元素。处理”多对一“用collection元素来实现(这两个元素在之前mapper文件中提到过)。

本例子中,假设一名User可以有多个Orders,用associasion来实现关联关系

首先数据库表结构

CREATE TABLE `user` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_bin NOT NULL,
  `usernumber` varchar(20) COLLATE utf8_bin NOT NULL,
  `loginname` varchar(20) COLLATE utf8_bin NOT NULL,
  `loginpassword` varchar(20) COLLATE utf8_bin NOT NULL,
  `sex` varchar(4) COLLATE utf8_bin DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
//orders表中为user_id添加外键,指向user表的id CREATE TABLE `orders` ( `oid`
int(8) NOT NULL AUTO_INCREMENT, `orderid` varchar(20) COLLATE utf8_bin NOT NULL, `message` varchar(20) COLLATE utf8_bin DEFAULT NULL, `user_id` int(8) NOT NULL, PRIMARY KEY (`oid`), KEY `1001` (`user_id`), CONSTRAINT `1001` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Order实体类

        public int oid;
        public String orderid;
        public String message;
        public User user;
//省略get/set方法

Order对象的sql映射文件,order.xml

<mapper namespace="com.mybaits.dao.impl.OrdersImpl">
        <resultMap type="com.mybaits.bean.User" id="userResult">
                <id property="id" column="id" />
                <result property="username" column="username"/>
                <result property="usernumber" column="usernumber"/>
                <result property="loginname" column="loginname"/>
                <result property="loginpassword" column="loginpassword"/>
                <result property="sex" column="sex"/>
                <result property="birthday" column="birthday" />
        </resultMap>
        
        <resultMap type="com.mybaits.bean.Orders" id="orderResult">
            <id property="oid" column="oid"/>
            <result property="orderid" column="orderid"/>
            <result property="message" column="message"/>
        <!--使用resultMap属性引用上面的User实体映射--> <association property="user" column="user_id" javaType="com.mybaits.bean.User" jdbcType="INTEGER" resultMap="userResult"> </association> </resultMap> <select id="findAllOrders" resultMap="orderResult"> select * from orders o left join user u on o.user_id=u.id </select> <insert id="saveOrder" parameterType="Orders"> insert into orders (orderid,message,user_id) values(#{orderid},#{message},#{user.id}) </insert> </mapper>

OrderTest类

@Test
    public void Test2(){
        List<Orders> l=order.findOrders();
        for(int i=0;i<l.size();i++){
            System.out.println(l.get(i));
        }
    }
订单编号:10001    订单信息:订单1    下单用户:BN
订单编号:10002    订单信息:订单2    下单用户:BN
订单编号:10003    订单信息:订单3    下单用户:qwe
订单编号:10004    订单信息:订单4    下单用户:qwe
订单编号:10005    订单信息:订单5    下单用户:JAVA

利用collection在User对象中关联

1.在User中添加 List<Orders> orders 属性

private List<Orders> orders=new ArrayList<Orders>();//get/set方法

2.User的sql映射文件user.xml

     <resultMap type="com.mybaits.bean.User" id="userResult">
                <id property="id" column="id" />
                <result property="username" column="username"/>
                <result property="usernumber" column="usernumber"/>
                <result property="loginname" column="loginname"/>
                <result property="loginpassword" column="loginpassword"/>
                <result property="sex" column="sex"/>
                <result property="birthday" column="birthday" />
//collection元素映射 user对象中order的集合属性,resultMap指向下面的order的resultMap <collection property="orders" ofType="Orders" resultMap="orderResult"></collection> </resultMap>
       <resultMap type="com.mybaits.bean.Orders" id="orderResult">
            <id property="oid" column="oid"/>
            <result property="orderid" column="orderid"/>
            <result property="message" column="message"/>
        </resultMap>
      <select id="getUOBN" parameterType="string" resultMap="userResult">
             select * from user u left join orders o on o.user_id=u.id where u.usernumber=#{usernumber}    
          </select>

3.测试类

@Test
    public void Test11(){
        User u=userDao.findUserOrders("01111001");
        System.out.println("用户:"+u.getUsername()+"\n订单总数:"+u.getOrders().size());
        for(Orders o:u.getOrders()){
            System.out.println("订单号:"+o.getOrderid()+"\t订单信息:"+o.getMessage());
        }
        
    }

用户:BN
订单总数:2
订单号:10001    订单信息:订单1
订单号:10002    订单信息:订单2

 

MyBatis对象关联关系---- association与collection

标签:

原文地址:http://www.cnblogs.com/nww57/p/4699307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!