m'ybatis 一对一 一对多 配置详解

2019-02-20 00:45:38来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

javabean:

package com.me.model;

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

public class User implements Serializable {  
      
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;  
    private String username;  
    private Date birthday;  
    private String sex;  
    private String address; 
    //一對一 放入對象
    private Morder morder;
    //一對多 放入對象集合
    private List<Home> homeList;
    
    
      
   
    public List<Home> getHomeList() {
        return homeList;
    }
    public void setHomeList(List<Home> homeList) {
        this.homeList = homeList;
    }
    public Morder getMorder() {
        return morder;
    }
    public void setMorder(Morder morder) {
        this.morder = morder;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getUsername() {  
        return username;  
    }  
    public void setUsername(String username) {  
        this.username = username;  
    }  
    public Date getBirthday() {  
        return birthday;  
    }  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
    public String getSex() {  
        return sex;  
    }  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
    public String getAddress() {  
        return address;  
    }  
    public void setAddress(String address) {  
        this.address = address;  
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", birthday="
                + birthday + ", sex=" + sex + ", address=" + address
                + ", morder=" + morder + ", homeList=" + homeList + "]";
    }
    
    
      
}  
package com.me.model;

public class Morder {
    
    private int orderId;
    private String orderName;
    private String orderMessage;
    public int getOrderId() {
        return orderId;
    }
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }
    public String getOrderName() {
        return orderName;
    }
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
    public String getOrderMessage() {
        return orderMessage;
    }
    public void setOrderMessage(String orderMessage) {
        this.orderMessage = orderMessage;
    }
    
    

}
package com.me.model;

public class Home {
	
	private int homeId;
	private String homeName;
	public int getHomeId() {
		return homeId;
	}
	public void setHomeId(int homeId) {
		this.homeId = homeId;
	}
	public String getHomeName() {
		return homeName;
	}
	public void setHomeName(String homeName) {
		this.homeName = homeName;
	}
	
	

}

  mapper.xml 代码

<!-- collection :collection属性的值有三个分别是list、array、map三种, 分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array 
        item : 表示在迭代过程中每一个元素的别名 index :表示在迭代过程中每次迭代到的位置(下标) open :前缀 close :后缀 separator 
        :分隔符,表示迭代时每个元素之间以什么分隔 -->
    <delete id="deleteSome">
        delete from user where id in
        <foreach collection="list" item="id" index="index" open="("
            close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    
<!-- 关联查询 -->    
    <!-- 關聯查詢 1對1 -->
    <select id="selectGL" resultMap="userRsultMap">
        select * from user u,morder m
        WHERE u.oid=m.order_id
    </select>
    <resultMap type="com.me.model.User" id="userRsultMap">
        <id property="id" column="id" />
        <result column="username" property="username" />
        <result column="birthday" property="birthday" />
        <result column="sex" property="sex" />
        <result column="address" property="address" />

        <association property="morder" javaType="com.me.model.Morder">
            <id column="order_id" property="orderId" />
            <result column="order_name" property="orderName" />
            <result column="order_message" property="orderMessage" />
        </association>
    </resultMap>
    <!-- 關聯查詢 1對多 -->
    <select id="selectGL2" resultMap="userRsultMap2">
        select * from user u,home h where u.hid=h.home_id;    
    </select>
    <resultMap type="com.me.model.User" id="userRsultMap2">
        <id property="id" column="id" />
        <result column="username" property="username" />
        <result column="birthday" property="birthday" />
        <result column="sex" property="sex" />
        <result column="address" property="address" />

        <collection property="homeList" ofType="com.me.model.Home">
            <id property="homeId" column="home_id" />
            <result property="homeName" column="home_name" />
        </collection>
    </resultMap>

图文解释:

测试:

//關聯查詢 1 to 多
    @Test
    public void selectGL2(){
        try {
            inputStream = Resources.getResourceAsStream(resource);
            // 创建会话工厂,传入MyBatis的配置文件信息
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                    .build(inputStream);
            // 通过工厂得到SqlSession
            sqlSession = sqlSessionFactory.openSession();
            List<User> list = sqlSession.selectList("test.selectGL2");
            for (User u : list) {
                System.err.println(u.getHomeList().get(0).getHomeName());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            sqlSession.close();
        }
    }

结果:

22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.215 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
22:47:17.420 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 518522822.
22:47:17.420 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.421 [main] DEBUG test.selectGL2 - ==> Preparing: select * from user u,home h where u.hid=h.home_id;
22:47:17.444 [main] DEBUG test.selectGL2 - ==> Parameters:
22:47:17.461 [main] DEBUG test.selectGL2 - <== Total: 4
sasadasd
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.463 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 518522822 to pool.

 

更多可以参考:https://www.cnblogs.com/xdp-gacl/p/4264440.html


原文链接:https://www.cnblogs.com/zhangheliang/p/10398431.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java中replace与replaceAll区别

下一篇:简单介绍Java的静态分派和动态分派