浅入浅出MyBatis(03):如果Bean中成员变量和表中字段命名不一致

2015-01-14

该文章是《专题:浅入浅出MyBatis》系列文章的一篇。点击进入该专题目录

工具与环境:Intellij IDEA 14,JDK 1.7,MyBatis 3.2,MySQL 5.5。

还是以浅入浅出MyBatis(01):查询id为1的用户的信息为基础。

修改letian.mybatis.bean下的User.java

package letian.mybatis.bean;

public class User {

    private int id;
    private String username;
    private String email;
    private String password;

    public void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

User类中的变量name改成了username,这和blog_db数据库中user表中的name字段对应不起来。

Main.java内容如下:

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import letian.mybatis.bean.User;
import letian.mybatis.dao.UserMapper;


public class Main {

    public static void main(String[] args) throws IOException {

        SqlSessionFactory sessionFactory;
        sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsReader("mybatis-config.xml"));

        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findById(1);
        System.out.println(user);

    }
}

执行结果如下:

User{id=1, username='null', email='letian@111.com', password='123'}

username的值为null

如何修改

要解决上面的不一致的问题,可以将letian.mybatis.mapper下的UserMapper.xml的内容修改为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="letian.mybatis.dao.UserMapper">

    <resultMap id="userResult" type="letian.mybatis.bean.User">
        <result property="username" column="name" javaType="String" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="findById" parameterType="HashMap" resultMap="userResult" resultType="letian.mybatis.bean.User">
        select
        * from blog_db.user where id=#{id}
    </select>

</mapper>

<resultMap>中,User类中的username和表中的name对应了起来(建立了映射关系)。<resultMap>的id是userResult<select>中也添加了属性resultMap,值为userResult,用来指向上面的<resultMap>

此时,运行Main.java,结果为:

User{id=1, username='letian', email='letian@111.com', password='123'}

如果在Main.java中找id为3的用户的信息,即:

        User user = userMapper.findById(3); //不存在id为3的用户
        System.out.println(user);

那么,输出为:

null