Mybatis利用ResultMap自定义映射案例

这种情况主要用于解决实体类属性名与数据库表中字段名不一致的情况下,解决将查询结果映射成实体对象。
之前案例:http://blog.csdn.net/u013821825/article/details/51620371
1、准备实体类

package org.xsx.entity;

public class Emp2 {
    private Integer eid;
    private String ename;
    private Integer eage;
    public Integer getEid() {
        return eid;
    }
    public void setEid(Integer eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Integer getEage() {
        return eage;
    }
    public void setEage(Integer eage) {
        this.eage = eage;
    }

}

2、修改映射文件–添加代码

"org.xsx.entity.Emp2" id="empMap">
    <result property="eid" column="id" jdbcType="INTEGER" javaType="int" />
    <result property="ename" column="name" jdbcType="VARCHAR" javaType="string" />
    <result property="eage" column="age" jdbcType="INTEGER" javaType="int" />


3、修改映射类接口–添加代码

    public List findAll1();

4、测试类

package org.xsx.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.xsx.entity.Emp;
import org.xsx.entity.Emp2;
import org.xsx.entity.EmpMapper;

public class TestResultMap extends MybatisHelper {

    static{
        conf = "SqlMapConfig.xml";
    }

    @Test
    public void testFindAll(){
        SqlSession session = factory.openSession();

        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List list = mapper.findAll1();
        for(Emp2 e:list){
            System.out.println(e.getEid()+"--"+e.getEname()+"--"+e.getEage());
        }

        session.close();
    }
}

你可能感兴趣的:(java)