resultMap 映射
Posted 子鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了resultMap 映射相关的知识,希望对你有一定的参考价值。
1. sql的重用:定义一个sql片段,可在任何SQL语句中重用该片段。
- <sql id="personColumns"> name, sex, updateTime</sql>
- <select id="selectPerson" parameterType="int" resultType="hashmap">
- select id, <include refid="personColumns"/> from person where id =#{id};
- </select>
2. javabean别名:不用每次写包路径
- <!-- In Config XML file,定义 -->
- <typeAlias type=”com.someapp.model.User” alias=”User”/>
- <!-- In SQL Mapping XML file,使用 -->
- <select id=”selectUsers” parameterType=”int” resultType=”User”>
- select id, username, hashedPassword from some_table where id = #{id}
- </select>
3. 表与实体列名不匹配的解决
a) SQL的别名
- <select id=”selectUsers” parameterType=”int” resultType=”User”>
- select user_id as "id", user_name as userName, hashed_password as hashedPassword from some_table where id = #{id}
- </select>
b)定义外部的resultMap
- <resultMap id="userResult" type="User">
- <id property="id" column="_id" />
- <result property="name" column="_name" />
- <result property="password" column="_password" />
- </resultMap>
- <select id="selectUser" parameterType="int" resultMap="userResult">
- select _id, _name, _password from _user where _id =#{id};
- </select>
以上是关于resultMap 映射的主要内容,如果未能解决你的问题,请参考以下文章