# ResultMap結果集映射 ###### tags: `Mybatis-配置解析` ## 解決屬性名和字段名不一致的問題 數據庫中的字段  新建一個項目,拷貝之前的,測試實體類字段不一致的情況 ```java= public class User { private int id; private String name; private String password; ``` 測試出現問題  ```java= //select * from mybatis.user where id = #{id} //類型處理器 //select id,name,pwd from mybatis.user where id = #{id} ``` 解決方法: * 起別名 ```xml= <select id="getUserList" resultType="user"> select id,name,pwd as password from mybatis.user </select> ``` ### resultMap 結果集映射 ``` 數據庫:id name pwd 實體類:id name password ``` ```xml= <!--結果集映射--> <resultMap id="UserMap" type="User"> <!-- column:數據庫中的字段 / property:實體類的屬性--> <result column="id" property="id"/> <result column="name" property="name"/> <result column="pwd" property="password"/> </resultMap> <!--select查詢語句--> <select id="getUserList" resultMap="UserMap"> select * from mybatis.user </select> ``` * resultMap 元素是 MyBatis 中最重要最强大的元素。 * ResultMap 的設計思想是,對簡單的語句做到零配置,對於覆雜一點的語句,只需要描述語句之間的關系就行了。 * ResultMap 的优秀之处——你完全可以不用显式地配置它们 只需要去配置數據庫與類的名稱不同的即可 ```xml= <!--結果集映射--> <resultMap id="UserMap" type="User"> <!-- column:數據庫中的字段 / property:實體類的屬性--> <!-- <result column="id" property="id"/>--> <!-- <result column="name" property="name"/>--> <result column="pwd" property="password"/> </resultMap> ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up