1,当数据库的字段名与属性名称不一致时,在mybatis中如何处理?

第一种方式: 采用投影对字段重命名<select id="load" parameterType="int" resultMap="addressMap">
        select * user_id as userId  from t_address where id=#{id}
</select>
第二种方式: 使用resultMap

    <resultMap type="Address" id="addressMap">
        <!-- id是用来对主键映射的 -->
        <!-- result是用来对一般属性映射的 -->
        <!-- 只需要对不一致的字段进行映射,其它的没有必要,  id一致是,可以不映射 -->
        <id column="id" property="id"/>
        <result column="postcode" property="postcode"/>
    </resultMap>
    <select id="load" parameterType="int" resultMap="addressMap">
        select *  from t_address where id=#{id}
    </select>

2,resultMap中除了前面的id result两个属性之外,还有很多有趣的属性,

2.1 association取关联对象  (外键关联)

第一种方式 会发N+1条sql,不可取 

<resultMap type="Address" id="addressMap">
        <!-- association 用来作关联   property属性:要关联的对象名称  column属性:用哪一列进行关联
        javaType属性: 要关联的对象类型  select属性:执行的sqlId
        -->
        <!-- association最大的问题就是取关联对象时,会发N条sql 因此以下取关联对象的方式不会使用的 -->
        <association property="user" column="user_id" javaType="User"
         select="com.yangwei.shop.entity.User.load" />
    </resultMap>
    <select id="load" parameterType="int" resultMap="addressMap">
        select *  from t_address where id=#{id}
    </select>
   第二种方式  只会发1条sql ,请使用这种  注意sql的写法  <resultMap type="Address" id="addressMap" autoMapping="true">
        <!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
        <id column="a_id" property="id" />     <-- 取关联对象-->
        <association property="user" javaType="User">
            <!-- 这里面的字段都是user表中的,全部必须手动映射,没有映射的将是null -->
            <id column="id" property="id"/>
            <result column="username" property="username"/>
            <result column="passwd" property="passwd"/>
            <result column="nickname" property="nickname"/>
            <result column="type" property="type"/>
        </association>
    </resultMap>
    <select id="load" parameterType="int" resultMap="addressMap">
        select *,t1.id as a_id  from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
    </select>
第三种方式: 与第二种差不多,只是将结果resultMap单独出来<resultMap type="Address" id="addressMap" autoMapping="true">
        <!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
        <id column="a_id" property="id" />
        <association property="user" javaType="User" resultMap="userMap" />
    </resultMap>

    <resultMap type="User" id="userMap">
            <id column="id" property="id"/>
            <result column="username" property="username"/>
            <result column="passwd" property="passwd"/>
            <result column="nickname" property="nickname"/>
            <result column="type" property="type"/>
    </resultMap>
    <select id="load" parameterType="int" resultMap="addressMap">
        select *,t1.id as a_id  from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
    </select>

2.2 collection 获取对象中关联的集合数据

    <resultMap type="User" id="userMap" autoMapping="true">
        <!--User类中有 List<Address> address属性;-->
        <!--column写不写都无所谓-->
        <!--ofType必须设置 List里面的类型-->          <id column="user_id" property="id"></id> <!--下面的查询会有两个id,需要指明用哪一个映射-->
        <collection property="address" column="user_id" ofType="Address">
            <id column="a_id" property="id" />
            <result column="name" property="name"/>
            <result column="postcode" property="postcode"/>
            <result column="detail" property="detail"/>
            <result column="phone" property="phone"/>
        </collection>
    </resultMap>
    <select id="load" parameterType="int" resultMap="userMap">
        select *,t2.id as a_id from t_user t1 left join t_address t2 on(t1.id=t2.user_id) where t1.id=#{id}
    </select>

最新文章

  1. mac 快捷键拾遗
  2. PDF 补丁丁 0.5.0.2078 测试版发布:不用打字,也能加书签
  3. 微软 Build 2016年开发者大会发布多项功能升级
  4. Something wrong with EnCase index search in Unallocated area
  5. LeetCode4 Median of Two Sorted Arrays
  6. php常见问题辨析(二)
  7. jquery实现二级联动
  8. &lt;META http-equiv=Content-Type content=&quot;text/html; charset=gb2312&quot;&gt;
  9. A. Grasshopper And the String(CF ROUND 378 DIV2)
  10. Filter 解决web网页跳转乱码
  11. JAVA基础--toString, equals方法
  12. Crash 的文明世界
  13. cf1076d 贪心最短路
  14. http协议及长连接和短连接
  15. A1059. Prime Factors
  16. POJ-3635 Full Tank? (记忆化广搜)
  17. 什么是控制反转(IOC)?什么是依赖注入?
  18. linu中解压不同后缀的文件
  19. Activiti实现会签功能
  20. 《锋利的JQuery》读书要点笔记5——jQuery与Ajax的应用

热门文章

  1. java项目编码格式转换(如GBK转UTF-8)
  2. 我了解到的JavaScript异步编程
  3. Fedora25和win10双系统安装及使问题汇总
  4. iOS程序员的React Native开发工具集
  5. LINQ Distinct()
  6. 【Regular Expression】RE分类及案例
  7. SpringMVC(一)--基础、REST、@RequestParam、POST请求乱码等
  8. uval 6657 GCD XOR
  9. python之VSCode安装
  10. 好用的sql