对resultMap中column的理解
resultMap中column的值:
總之,column是指查詢出來的字段名。
1.如果是單表映射,column默認是對應數據庫字段
//pojo屬性與數據庫字段對應一致時,<resultMap>中可以不用寫映射
//如果有個別的字段不一致,可以只寫不一致的字段
//例如:只寫<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>
//結果為: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>
//結果為: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>
//結果為:User{id=1, username='null', age=18, bir=Tue Oct 08 00:00:00 CST 2019}
//對username起了別名u,column還是默認是數據庫字段,沒有修改,輸出的結果此字段username為null!
總結:column值實際上是指查詢出的字段!默認情況下是數據庫字段。
2.如果是多表關聯(lián)查詢(特征就是resultMap中使用association或collection標簽),可能會出現(xiàn)兩個表某字段一致的情況!需要對column值進行修改
a. 首先多表關聯(lián)查詢時,主表查詢出的字段必須要寫全(與單表查詢不同,多表查詢不能省略,即便數據庫列名與pojo屬性名一致也要寫!否則該字段查詢出的數據為null!)
(1)也就是說,凡是查詢出來的字段都要寫,沒有查詢的字段不需要寫,因為都沒有查詢這個字段,寫了也沒用。但是一般都寫全,因為一個xml文件一般只配置一個<resultMap>(可以配多個),那么不同的查詢語句可能查詢出來的字段不一致,盡量補全。
(2)單表查詢時可以省略一致的字段。
(3)property的值是屬性名,屬性名不是類里面定義的變量名,而是set/get方法的方法名去掉set/get,然后首字母小寫。
以下以<association>為例,<collection>與<association>的colum值的含義相同。
例如:
? <resultMap id="ordermap" type="Order">
? ? ? ? <!-- 字段一定要寫全 -->
? ? ? ? <id property="id" column="id" />
? ? ? ? <result property="user_id" column="user_id" />
? ? ? ? <result property="price" column="price" />
? ? ? ? <result property="name" column="name" />
? ? ? ? <!-- property是指order類中關聯(lián)的另一個pojo屬性(User),user是它的變量名 -->
? ? ?? <!-- javaType一定要寫,同理<collection>內的ofType也要寫,否則會報空指針異常! -->
? ? ? ? <association property="user" javaType="User">
? ? ? ? </association>
? ? </resultMap>
反例:
<resultMap id="ordermap" type="Order">
? ? ? ? <!-- 少寫了name -->
? ? ? ? <id property="id" column="id" />
? ? ? ? <result property="user_id" column="user_id" />
? ? ? ? <result property="price" column="price" />
? ? ? ? <!-- property是指order類中關聯(lián)的另一個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);
? ? ? ? }
輸出結果為:
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}
查詢關聯(lián)表user的信息:
b. 同樣需要把字段補全,少寫一個字段,則該字段就為null!
? ? ? ?? <association property="user" javaType="User">
? ? ? ? ? ? <!-- 這是的property是指User實體內的屬性,column是指查詢出來的字段,如果有別名就是別名 -->
? ? ? ? ? ? <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>
注意:此時兩個表關聯(lián)查詢出的所有字段,其中order的id與user表的id字段名一致,輸出時需要對其中一個字段改名!
例如:在數據庫中輸入此查詢語句,輸出的user的id自動改為id1
那么user表id的column值不能寫id,因為這樣與主表id字段重復,最終輸出的user表的id會是主表的id值!!!解決方法就是對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());
查詢結果為:
User{id=1, username='zhangsan', age=18, bir=null}
總結
以上是生活随笔為你收集整理的对resultMap中column的理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BOLL布林带定向策略
- 下一篇: matlab中删除矩阵中的某些行