对resultMap中column的理解

resultMap中column的值:

总之,column是指查询出来的字段名。

对resultMap中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.如果是多表关联查询(特征就是resultMap中使用association或collection标签),可能会出现两个表某字段一致的情况!需要对column值进行修改

a. 首先多表关联查询时,主表查询出的字段必须要写全(与单表查询不同,多表查询不能省略,即便数据库列名与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类中关联的另一个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类中关联的另一个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}

查询关联表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>
注意:此时两个表关联查询出的所有字段,其中order的id与user表的id字段名一致,输出时需要对其中一个字段改名!
例如:在数据库中输入此查询语句,输出的user的id自动改为id1

对resultMap中column的理解

那么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}