mybatis查询数据库返回数据全为null

异常过程

springboot框架加mybatis的整合在查询数据时返回数据正确条数但内容全为null

mapper.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="com.ly.mybatis.mapper.EmpMapper">

<!--        List<Emp> getAllEmp();-->
    <select id="getAllEmp" resultType="emp">
        select * from emp
    </select>

</mapper>

Emp.java

    private Integer eId;
    private String eName;
    private Integer eAge;
    private String eSex;
    private String eEmail;

数据库字段名

test.java

@Test
    public void testGetAllEmp(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.getAllEmp();
        emps.forEach(emp-> System.out.println(emp));
     }

 异常结果截图

修改途径

1.在写查询语句时为字段名取别名且别名与属性名相同

mapper.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="com.ly.mybatis.mapper.EmpMapper">

<!--        List<Emp> getAllEmp();-->
    <select id="getAllEmp" resultType="emp">
        select e_id eId,e_name eName,e_age eAge,e_sex eSex,e_email eEmail from emp
    </select>

</mapper>

 结果截图

 2.设置mybatis的全局配置

配置文件

mapper.xml

    <select id="getAllEmp" resultType="emp">
<!--         select e_id eId,e_name eName,e_age eAge,e_sex eSex,e_email eEmail from emp-->
        select * from emp
    </select>

 结果截图

3.使用resultMap自定义映射关系

mapper.xml

    <resultMap id="empResultMap" type="Emp">
        <id property="eId" column="e_id"></id>
        <result property="eName" column="e_name"></result>
        <result property="eAge" column="e_age"></result>
        <result property="eSex" column="e_sex"></result>
        <result property="eEmail" column="e_email"></result>
    </resultMap>

<!--    List<Emp> getAllEmp();-->
    <select id="getAllEmp" resultMap="empResultMap">
        select * from emp
    </select>

结果截图

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值