对resultMap中column的理解
resultMap中column的值:
總之,column是指查詢出來(lái)的字段名。
1.如果是單表映射,column默認(rèn)是對(duì)應(yīng)數(shù)據(jù)庫(kù)字段
//pojo屬性與數(shù)據(jù)庫(kù)字段對(duì)應(yīng)一致時(shí),<resultMap>中可以不用寫(xiě)映射
//如果有個(gè)別的字段不一致,可以只寫(xiě)不一致的字段
//例如:只寫(xiě)<result property="username" column="user_name" />
? ?<resultMap id="usermap" type="User">
? ? ? ? ?<result property="username" column="user_name" />
? ? </resultMap>
? ? <select id="selectUserById" parameterType="integer" ? ? ?resultMap="usermap">
? ? ? ? ? select * from `user` where id = #{value}
? ? </select>
//結(jié)果為:User{id=1, username='zhangsan', age=18, bir=Tue Oct 08 00:00:00 CST 2019}
?
如果查詢的字段起了別名,那么column就是別名!!
例如:
? ? <resultMap id="usermap" type="User">
? ? ? ? ? <result property="username" column="u"></result>
? ? </resultMap>
? ? <select id="findUserById" parameterType="integer" resultMap="usermap">
? ? ? ? ? select id,user_name u,age,bir from `user` where id = #{value}
? ? </select>
//結(jié)果為:User{id=1, username='zhangsan', age=18, bir=Tue Oct 08 00:00:00 CST 2019}
反例:
? ? <resultMap id="usermap" type="User">
? ? ? ? ?<result property="username" column="user_name"></result>
? ? </resultMap>
? ? <select id="findUserById" parameterType="integer" resultMap="usermap">
? ? ? ? ?select id,user_name u,age,bir from `user` where id = #{value}
? ? </select>
//結(jié)果為:User{id=1, username='null', age=18, bir=Tue Oct 08 00:00:00 CST 2019}
//對(duì)username起了別名u,column還是默認(rèn)是數(shù)據(jù)庫(kù)字段,沒(méi)有修改,輸出的結(jié)果此字段username為null!
總結(jié):column值實(shí)際上是指查詢出的字段!默認(rèn)情況下是數(shù)據(jù)庫(kù)字段。
2.如果是多表關(guān)聯(lián)查詢(特征就是resultMap中使用association或collection標(biāo)簽),可能會(huì)出現(xiàn)兩個(gè)表某字段一致的情況!需要對(duì)column值進(jìn)行修改
a. 首先多表關(guān)聯(lián)查詢時(shí),主表查詢出的字段必須要寫(xiě)全(與單表查詢不同,多表查詢不能省略,即便數(shù)據(jù)庫(kù)列名與pojo屬性名一致也要寫(xiě)!否則該字段查詢出的數(shù)據(jù)為null!)
(1)也就是說(shuō),凡是查詢出來(lái)的字段都要寫(xiě),沒(méi)有查詢的字段不需要寫(xiě),因?yàn)槎紱](méi)有查詢這個(gè)字段,寫(xiě)了也沒(méi)用。但是一般都寫(xiě)全,因?yàn)橐粋€(gè)xml文件一般只配置一個(gè)<resultMap>(可以配多個(gè)),那么不同的查詢語(yǔ)句可能查詢出來(lái)的字段不一致,盡量補(bǔ)全。
(2)單表查詢時(shí)可以省略一致的字段。
(3)property的值是屬性名,屬性名不是類(lèi)里面定義的變量名,而是set/get方法的方法名去掉set/get,然后首字母小寫(xiě)。
以下以<association>為例,<collection>與<association>的colum值的含義相同。
例如:
? <resultMap id="ordermap" type="Order">
? ? ? ? <!-- 字段一定要寫(xiě)全 -->
? ? ? ? <id property="id" column="id" />
? ? ? ? <result property="user_id" column="user_id" />
? ? ? ? <result property="price" column="price" />
? ? ? ? <result property="name" column="name" />
? ? ? ? <!-- property是指order類(lèi)中關(guān)聯(lián)的另一個(gè)pojo屬性(User),user是它的變量名 -->
? ? ?? <!-- javaType一定要寫(xiě),同理<collection>內(nèi)的ofType也要寫(xiě),否則會(huì)報(bào)空指針異常! -->
? ? ? ? <association property="user" javaType="User">
? ? ? ? </association>
? ? </resultMap>
反例:
<resultMap id="ordermap" type="Order">
? ? ? ? <!-- 少寫(xiě)了name -->
? ? ? ? <id property="id" column="id" />
? ? ? ? <result property="user_id" column="user_id" />
? ? ? ? <result property="price" column="price" />
? ? ? ? <!-- property是指order類(lèi)中關(guān)聯(lián)的另一個(gè)pojo屬性(User),user是它的變量名 -->
? ? ? ? <association property="user" javaType="User">
? ? ? ? </association>
? ? </resultMap>
? ? <select id="findOrderAndUser" resultMap="ordermap">
? ? ? ? select o.* from `order` o,`user` u where u.id = o.user_id
? ? </select>
? ? ? ? List<Order> list = sqlSession.selectList("findOrderAndUser");
? ? ? ? for (Order or : list ) {
? ? ? ? ? ? System.out.println(or);
? ? ? ? }
輸出結(jié)果為:
Order{id=1, user_id=1, name='null', price=2}
Order{id=2, user_id=1, name='null', price=3}
Order{id=3, user_id=2, name='null', price=3}
Order{id=4, user_id=2, name='null', price=4}
查詢關(guān)聯(lián)表user的信息:
b. 同樣需要把字段補(bǔ)全,少寫(xiě)一個(gè)字段,則該字段就為null!
? ? ? ?? <association property="user" javaType="User">
? ? ? ? ? ? <!-- 這是的property是指User實(shí)體內(nèi)的屬性,column是指查詢出來(lái)的字段,如果有別名就是別名 -->
? ? ? ? ? ? <id property="id" column="uid" />
? ? ? ? ? ? <result property="username" column="user_name" />
? ? ? ? ? ? <result property="age" column="age" />
? ? ? ? ? ? <result property="bir" column="bir" />
? ? ? ? </association>
? ? <select id="findOrderAndUser" resultMap="ordermap">
? ? ? ? ? select? ??? from `order` o,`user` u where u.id = o.user_id
? ? </select>
注意:此時(shí)兩個(gè)表關(guān)聯(lián)查詢出的所有字段,其中order的id與user表的id字段名一致,輸出時(shí)需要對(duì)其中一個(gè)字段改名!
例如:在數(shù)據(jù)庫(kù)中輸入此查詢語(yǔ)句,輸出的user的id自動(dòng)改為id1
那么user表id的column值不能寫(xiě)id,因?yàn)檫@樣與主表id字段重復(fù),最終輸出的user表的id會(huì)是主表的id值!!!解決方法就是對(duì)user表的id起別名!
例如:
? ?<select id="findOrderAndUser" resultMap="ordermap">
? ? ? ? SELECT
? ? ? ? o.*,
? ? ? ? u.id uid,
? ? ? ? u.user_name,
? ? ? ? u.age
? ? ? ? FROM
? ? ? ? `order` o ,`user` u where o.user_id = u.id
? ? </select>
? ? ? ? List<Order> list = sqlSession.selectList("findOrderAndUser");
? ? ? ? System.out.println(list.get(0).getUser());
查詢結(jié)果為:
User{id=1, username='zhangsan', age=18, bir=null}
總結(jié)
以上是生活随笔為你收集整理的对resultMap中column的理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: BOLL布林带定向策略
- 下一篇: 17 内存规整(memory compa