异常过程

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>

结果截图

更多推荐