Mybatis:映射解决列名不匹配的方法

表格

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `namee` varchar(64) NOT NULL DEFAULT '',
  `phone` varchar(16) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

模型

//User.java
public class User {
    private int id;
    private String name;
    private String dept;
    private String phonee;
    private String website;
    //省略getter和setter方法
}

方法一:使用别名

映射文件




<mapper namespace="com.model.UserMapper">
    <select id="GetUserByID" parameterType="int"  resultType="User" >
        select id,namee as "name",phone as "phonee" from `user` where id=#{id}
    select>
mapper>

方法二:使用resultMap

映射文件




<mapper namespace="com.model.UserMapper">
    <select id="GetUserByID" parameterType="int"  resultMap="UserResultMap" >
          select * from `user` where id=#{id} 
    select>
    <resultMap type="User" id="UserResultMap">
        <result property="id" column="id"/>
        <result property="name" column="namee"/>
        <result property="phonee" column="phone"/>
    resultMap>
mapper>

你可能感兴趣的:(Mybatis)