Mybatis列包含下划线返回null

时间:2018-04-02 08:33:36

标签: java spring-mvc mybatis

我使用springmvc和mybatis。 我的mapper.xml喜欢这个:

Calendar calendar = Calendar.getInstance();
    Long max = calendar.getTimeInMillis();
    int currMonth = calendar.get(Calendar.MONTH);
    int currYear = calendar.get(Calendar.YEAR);
    int currDate = calendar.get(Calendar.DAY_OF_MONTH);
    int expMonth = currMonth + 3/*no_of_month*/;
    int expYear = currDate;
    int expDate = calendar.get(Calendar.DAY_OF_MONTH);
    while (expMonth > 11) {
        expMonth = expMonth - 12;
        expYear = expYear + 1; // will increment year as next month falls in next year 
    }
    calendar.set(expYear, expMonth, expDate);
    Long min = calendar.getTimeInMillis();
    Long range = (max - min) / (60 * 60 * 1000); //no_of_hours

当我调试我的java代码时,我发现该列包含下划线返回null。 enter image description here

我的mybatis-config.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.dao.mapper.ReportPersonGroupingMapper">
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
    <resultMap id="BaseResultMap" type="com.model.ReportPersonGrouping">
        <result column="employee_code" property="employeeCode" />
        <result column="employeename" property="employeeName" />
        <result column="working" property="working" />
        <result column="before_work" property="beforeWork" />
    </resultMap>
    <select id="getResultSetByCodeAndName" resultMap="BaseResultMap">
        select a.employee_code employeeCode,
        b.EmployeeName employeeName,
        a.working working,
        a.before_work beforeWork
        from report_person_grouping a inner join dbEmployee b 
        on a.employee_code = b.EmployeeCode
        where 1=1   
        <if test='employeeCode != null and employeeCode!="" and (employeeName == null or employeeName =="")'>
        and a.employee_code=#{employeeCode}
        </if>  
        <if test='employeeName != null and employeeName!="" and (employeeCode == null or employeeCode =="")'>
        and b.EmployeeName like '%${employeeName}%'
        </if>    
        <if test='employeeCode != null and employeeCode!="" and employeeName != null and employeeName !=""'>
        and a.employee_code=#{employeeCode}
        </if>   
    </select>
</mapper>

我该怎么做才能解决空值问题?数据库中的此字段 具有正确的价值。

2 个答案:

答案 0 :(得分:3)

问题是查询重命名列,结果集中的列名为employeeCode

select a.employee_code employeeCode

但在映射中指定列名为employee_code

<result column="employee_code" property="employeeCode" />

您应该不在查询中创建别名或修复映射。

答案 1 :(得分:0)

您在

中提问
<if test='employeeCode != null and employeeCode!="" and (employeeName == null or employeeName =="")'>
    and a.employee_code=#{employeeCode}
    </if>

也许您传递了空值