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>
posted @
2016-06-24 14:06
markchuan
阅读(
195)
评论()
编辑
收藏
举报