欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-10 11:55:17
javabean: mapper.xml 代码 图文解释: 测试: 结果: 22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.iba ......

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>

图文解释:m'ybatis 一对一 一对多 配置详解

测试:

//關聯查詢 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