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、修改映射文件–添加代码

<resultMap type="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" />
</resultMap>

<select id="findAll1" resultMap="empMap">
    select id,name,age from emp
</select>

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

    public List<Emp2> 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<Emp2> list = mapper.findAll1();
        for(Emp2 e:list){
            System.out.println(e.getEid()+"--"+e.getEname()+"--"+e.getEage());
        }

        session.close();
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值