mybatis中一對(duì)一關(guān)系association標(biāo)簽的使用
一對(duì)一關(guān)系association標(biāo)簽使用
association字面翻譯為聯(lián)合之意,Java項(xiàng)目開發(fā)中常遇到一對(duì)一關(guān)系的結(jié)果,例如,一個(gè)商品對(duì)應(yīng)一個(gè)生產(chǎn)商,在查詢結(jié)果中如果某兩個(gè)對(duì)象是一對(duì)一關(guān)系一般使用association標(biāo)簽,用法有兩種:
1、嵌套的resultMap
一次性查詢出所有結(jié)果的相關(guān)字段,結(jié)果把所有字段映射到不同的對(duì)象的類變量中
<resultMap id="map01" type="Model01"> ? ? <id column="..." property="..."/> ? ? <result column="..." property="..."> ? ? ... ? ? <!--Model01和Model02為一對(duì)一關(guān)系--> ? ? <association property="數(shù)據(jù)類型為Model02在Model01的類變量名稱" javaType="Model02" resultMap="map02"/> </resultMap> ? <resultMap id="map02" type="Model02"> ? ? <id column="..." property="..."/> ? ? <result column="..." property="..."> ? ? ... </resultMap> ? <select id="select01" resultMap="map01"> ? ? select ...最多查詢出Model01,Model02所對(duì)應(yīng)的所有字段? ? ? from table1(,table2可能需要)? ? ? where ...? ? ? order by ... </select>
說明:
分別有兩個(gè)類Model01,Model02,但Model01中有一個(gè)類變量的數(shù)據(jù)類型為Model02 ,id為 select01 的SQL語句所查詢的結(jié)果映射到 map01 的對(duì)應(yīng) Model01 對(duì)象的各類變量中,因 map01 中使用 association 標(biāo)簽,其 property屬性 指定 Model01 中的數(shù)據(jù)類型為 Model02 的類變量,JavaType屬性指定該類變量的數(shù)據(jù)類型,即Model02,resultMap屬性指定對(duì)應(yīng)的結(jié)果映射為 map02,map02中列出了相應(yīng)的表子段和類變量的映射關(guān)系,所以一次查出所有需要的字段,只是按不同形式映射到相應(yīng)各個(gè)類的類變量中
2、嵌套的select語句
這種方式實(shí)為嵌套一個(gè)子查詢語句查出關(guān)聯(lián)的實(shí)體數(shù)據(jù)(會(huì)產(chǎn)生N+1問題,在多次循環(huán)中不好,建議在java層面進(jìn)行業(yè)務(wù)分離)
例子如下:
<resultMap id="map03" type="Model03"> ? ? <id column="..." property="..."/> ? ? <result column="..." property="..."> ? ? ... ? ? <association property="數(shù)據(jù)類型為Model04在Model03的類變量名稱" javaType="Model04" column="對(duì)應(yīng)map03查詢結(jié)果的某字段,并且該字段正是子查詢select04對(duì)應(yīng)需要的參數(shù)值" select="X命名空間.select04"/> </resultMap> ? <resultMap id="map04" type="Model04"> ? ? <id column="..." property="..."/> ? ? <result column="..." property="..."> ? ? ... </resultMap> ? <select id="select03" parameterType="Java某數(shù)據(jù)類型" resultMap="map03"> ? ? select ...最多查詢出Model03所對(duì)應(yīng)的所有(不含Model04對(duì)應(yīng)的字段)字段? ? ? from table1? ? ? where ...? ? ? order by ... </select> ? <select id="select04" parameterType="Java某數(shù)據(jù)類型" resultMap="map04"> ? ? select ...最多查詢出Model04所對(duì)應(yīng)的所有字段? ? ? from table2? ? ? where ...? ? ? order by ... </select>
說明:
分別有兩個(gè)類 Model03,Model04,但Model03中有一個(gè)類變量的數(shù)據(jù)類型為Model04 ,id 為 select03 的SQL語句所查詢的結(jié)果映射到 map03 的對(duì)應(yīng) Model03 對(duì)象的各類變量中,因 map03 中使用 association 標(biāo)簽,其 property屬性 指定 Model03 中的數(shù)據(jù)類型為 Model04 的類變量,column屬性為 map03 中的某字段,該字段值正是子查詢select04所需的參數(shù),select屬性為指定需要的子查詢,即ID為select04的子查詢,map04中列出本查詢結(jié)果相應(yīng)的表子段和類變量的映射關(guān)系,所以首先查出父對(duì)象所有需要的所有字段,完成映射,同時(shí)使用嵌套的子查詢查出所需的字段并映射到相應(yīng)的類,再把該類付給父級(jí)對(duì)象對(duì)應(yīng)的變量
association標(biāo)簽三種用法
`father`表
CREATE TABLE `father` ( ? `ID` int(11) NOT NULL, ? `NAME` varchar(255) DEFAULT NULL, ? PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? INSERT INTO `father` VALUES ('1', '李靖'); INSERT INTO `father` VALUES ('2', '康熙');
`son`表
CREATE TABLE `son` ( ? `ID` int(11) NOT NULL, ? `FATHER_ID` int(11) DEFAULT NULL, ? `NAME` varchar(255) DEFAULT NULL, ? PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? INSERT INTO `son` VALUES ('1', '2', '雍正'); INSERT INTO `son` VALUES ('2', '1', '哪吒');
Father.java
public class Father { ?? ?private Integer id;? ?? ?private String name;? ?? ?public Integer getId() { ?? ??? ?return id; ?? ?} ? ?? ?public void setId(Integer id) { ?? ??? ?this.id = id; ?? ?} ? ?? ?public String getName() { ?? ??? ?return name; ?? ?} ? ?? ?public void setName(String name) { ?? ??? ?this.name = name == null ? null : name.trim(); ?? ?} }
Son.java
public class Son { ?? ?private Integer id;? ?? ?private Father father;? ?? ?private String name;? ?? ?public Integer getId() { ?? ??? ?return id; ?? ?} ? ?? ?public void setId(Integer id) { ?? ??? ?this.id = id; ?? ?} ? ?? ?public Father getFather() { ?? ??? ?return father; ?? ?} ? ?? ?public void setFather(Father father) { ?? ??? ?this.father = father; ?? ?} ? ?? ?public String getName() { ?? ??? ?return name; ?? ?} ? ?? ?public void setName(String name) { ?? ??? ?this.name = name == null ? null : name.trim(); ?? ?} }
FatherMapper.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="com.ksy.kuaishiyan.mapper.FatherMapper" > ?? ? <resultMap id="BaseResultMap" type="com.ksy.kuaishiyan.entity.Father" > ? ? <id column="ID" property="id" jdbcType="INTEGER" /> ? ? <result column="NAME" property="name" jdbcType="VARCHAR" /> ? </resultMap> ?? ? <sql id="Base_Column_List" > ? ? ID, NAME ? </sql> ?? ? <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from father ? ? where ID = #{id,jdbcType=INTEGER} ? </select> ?? </mapper>
SonMapper.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="com.ksy.kuaishiyan.mapper.SonMapper" > ? ? <resultMap id="BaseResultMap" type="com.ksy.kuaishiyan.entity.Son" > ? ? <id column="ID" property="id" jdbcType="INTEGER" /> ? ? <result column="FATHER_ID" property="fatherId" jdbcType="INTEGER" /> ? ? <result column="NAME" property="name" jdbcType="VARCHAR" /> ? </resultMap> ?? ? <sql id="Base_Column_List" > ? ? ID, FATHER_ID, NAME ? </sql> ?? ? <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from son ? ? where ID = #{id,jdbcType=INTEGER} ? </select> ? </mapper>
association的用法一
直接在SonMapper.xml中的association標(biāo)簽里寫對(duì)應(yīng)的列名, 且列明需要寫別名, 例如: father.ID AS F_ID
<?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="com.ksy.kuaishiyan.mapper.SonMapper"> ? ? <resultMap id="BaseResultMap" type="com.ksy.kuaishiyan.entity.Son"> ? ? <id column="ID" property="id" jdbcType="INTEGER" /> ? ? <result column="NAME" property="name" jdbcType="VARCHAR" /> ? ?? ? ? <association property="father" javaType="com.ksy.kuaishiyan.entity.Father">? ? ? ? <id column="F_ID" property="id" jdbcType="INTEGER" /> ? ? ? ? <result column="F_NAME" property="name" jdbcType="VARCHAR" /> ? ? </association> ? </resultMap> ? ? <sql id="Base_Column_List"> ? ? son.ID, son.NAME, father.ID AS F_ID, father.NAME AS F_NAME ? </sql> ? ? <select id="selectByPrimaryKey" resultMap="BaseResultMap" ? ? parameterType="java.lang.Integer"> ? ? select ? ? <include refid="Base_Column_List" /> ? ? from son, father ? ? where son.FATHER_ID=father.ID AND son.ID = #{id,jdbcType=INTEGER} ? </select> ? </mapper>
調(diào)用selectByPrimaryKey傳入id=2, 查詢結(jié)果如下
{
"id": 2,
"father": {
"id": 1,
"name": "李靖"
},
"name": "哪吒"
}
association的用法二
association傳入一個(gè)ResultMap, 改寫SonMapper.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="com.ksy.kuaishiyan.mapper.SonMapper"> ? ? <resultMap id="BaseResultMap" type="com.ksy.kuaishiyan.entity.Son"> ? ? <id column="ID" property="id" jdbcType="INTEGER" /> ? ? <result column="NAME" property="name" jdbcType="VARCHAR" /> ? ?? ? ? <association property="father" javaType="com.ksy.kuaishiyan.entity.Father" resultMap="com.ksy.kuaishiyan.mapper.FatherMapper.BaseResultMap"></association> ? </resultMap> ? ? <sql id="Base_Column_List"> ? ? son.ID, son.NAME, father.ID AS F_ID, father.NAME AS F_NAME ? </sql> ? ? <select id="selectByPrimaryKey" resultMap="BaseResultMap" ? ? parameterType="java.lang.Integer"> ? ? select ? ? <include refid="Base_Column_List" /> ? ? from son, father ? ? where son.FATHER_ID=father.ID AND son.ID = #{id,jdbcType=INTEGER} ? </select> ? </mapper>
association 標(biāo)簽中resultMap屬性指向FatherMapper.xml中的BaseResultMap, 這種情況下要求father表和son表沒有相同名字的字段, 否則會(huì)失敗. 調(diào)用selectByPrimaryKey傳入id=2, 查詢結(jié)果失敗如下:
{
"id": 2,
"father": {
"id": 2,
"name": "哪吒"
},
"name": "哪吒"
}
association的用法三
給association傳入一個(gè)select
<?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="com.ksy.kuaishiyan.mapper.SonMapper"> ? ? <resultMap id="BaseResultMap" type="com.ksy.kuaishiyan.entity.Son"> ? ? <id column="ID" property="id" jdbcType="INTEGER" /> ? ? <result column="NAME" property="name" jdbcType="VARCHAR" /> ? ?? ? ? <association column="FATHER_ID" property="father" javaType="com.ksy.kuaishiyan.entity.Father" select="com.ksy.kuaishiyan.mapper.FatherMapper.selectByPrimaryKey"></association> ? </resultMap> ? ? <sql id="Base_Column_List"> ? ? ID, NAME, FATHER_ID ? </sql> ? ? <select id="selectByPrimaryKey" resultMap="BaseResultMap" ? ? parameterType="java.lang.Integer"> ? ? select ? ? <include refid="Base_Column_List" /> ? ? from son ? ? where ID = #{id,jdbcType=INTEGER} ? </select> ? </mapper>
這種方式相當(dāng)于將原來的
<result column="FATHER_ID" property="fatherId" jdbcType="INTEGER" />
替換成
<association column="FATHER_ID" property="father" javaType="com.ksy.kuaishiyan.entity.Father" select="com.ksy.kuaishiyan.mapper.FatherMapper.selectByPrimaryKey"></association>
改動(dòng)最小, 需要assonciation標(biāo)簽放到所有result標(biāo)簽之后, select語句還可以延遲加載.
在一個(gè)<resultMap/>中,屬性出現(xiàn)的先后順序必須是:constructor-->id --> result--> association-->collection -->discriminator。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了Java實(shí)現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了java使用JDBC實(shí)現(xiàn)針對(duì)mysql數(shù)據(jù)庫的連接、查詢、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Springcloud中Feign傳遞參數(shù)的過程解析
這篇文章主要介紹了Springcloud中Feign傳遞參數(shù)的過程,單個(gè)參數(shù)的傳值有兩種方式,第一種使用@RequestParam/@PathVariable進(jìn)行傳值,傳遞多個(gè)參數(shù):多個(gè)參數(shù)的傳值可以使用多個(gè)@RequestParam來進(jìn)行傳參,需要的朋友可以參考下2023-09-09使用JPA進(jìn)行CriteriaQuery進(jìn)行查詢的注意事項(xiàng)
這篇文章主要介紹了使用JPA進(jìn)行CriteriaQuery進(jìn)行查詢的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java中BufferedReader與Scanner讀入的區(qū)別詳解
這篇文章主要介紹了Java中BufferedReader與Scanner讀入的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10