8/4/2018 11:03:14 PM
今天测试项目出现使用MyBatis select数据库查出有数据 但返回对象为null
mybatis配置文件:
<!-- 根据主键ID查询订单信息-->
<select id="getPayOrderId" parameterType="java.lang.String" resultType="PayOrder">
SELECT
a.ID AS 'id',
a.USER_ID AS 'user_id',
a.MERCHANT_ID AS 'merchant_id',
a.PAY_ORDER_ID AS 'pay_order_id',
a.USER_NAME AS 'user_name',
a.MERCHANT_NAME AS 'merchant_name',
a.PAY_TYPE AS 'pay_type',
a.PAY_MONEY AS 'pay_money',
a.PAY_ZD_MONEY AS 'pay_zd_money',
a.PAY_BALANCE_MONEY AS 'pay_balance_money',
a.PAY_APPRECIATION_MONEY AS 'pay_appreciation_money',
a.PAY_BANKCARD_MONEY AS 'pay_bankcard_money',
a.STATUS AS 'status',
a.FINISH_TIME AS 'finish_time',
a.CREATE_TIME AS 'create_time'
FROM
pay_order a
WHERE a.ID = #{_id,jdbcType=VARCHAR}
</select>
对应查询的java实体类:
public class PayOrder {
private static final long serialVersionUID = 1L;
private String id;
private String user_id;
private String merchant_id;
private String pay_order_id;
private String pay_type;
private String pay_money;
private String pay_zd_money;
private String pay_balance_money;
private String pay_appreciation_money;
private String pay_bankcard_money;
private String status;
private Date create_time;
private Date finish_time;
private String merchant_name;
private String user_name;
private String pay_pwd;
//自动生成set、get方法
}
之后就出现通过ID进行查询返回对象一直为null,但是自己的sql语句在数据库中测试时显示有数据。
解决办法
1.修改Dao对应的xml配置
<resultMap id="setPayOrder" type="order.PayOrder">
<id column="ID" property="id" jdbcType="VARCHAR"/>
<result column="USER_ID" property="user_id" jdbcType="VARCHAR"/>
<result column="MERCHANT_ID" property="merchant_id" jdbcType="VARCHAR"/>
<result column="PAY_ORDER_ID" property="pay_order_id" jdbcType="VARCHAR"/>
<result column="USER_NAME" property="user_name" jdbcType="VARCHAR"/>
<result column="MERCHANT_NAME" property="merchant_name" jdbcType="VARCHAR"/>
<result column="PAY_TYPE" property="pay_type" jdbcType="VARCHAR"/>
<result column="PAY_MONEY" property="pay_money" jdbcType="VARCHAR"/>
<result column="PAY_ZD_MONEY" property="pay_zd_money" jdbcType="VARCHAR"/>
<result column="PAY_BALANCE_MONEY" property="pay_balance_money" jdbcType="VARCHAR"/>
<result column="PAY_APPRECIATION_MONEY" property="pay_appreciation_money" jdbcType="VARCHAR"/>
<result column="PAY_BANKCARD_MONEY" property="pay_bankcard_money" jdbcType="VARCHAR"/>
<result column="STATUS" property="status" jdbcType="VARCHAR"/>
<result column="CREATE_TIME" property="create_time" jdbcType="TIMESTAMP"/>
<result column="FINISH_TIME" property="finish_time" jdbcType="TIMESTAMP"/>
</resultMap>
<!-- 根据主键ID查询订单信息-->
<select id="getPayOrderId" parameterType="java.lang.String" resultMap="setPayOrder">
SELECT
a.ID AS 'id',
a.USER_ID AS 'user_id',
a.MERCHANT_ID AS 'merchant_id',
a.PAY_ORDER_ID AS 'pay_order_id',
a.USER_NAME AS 'user_name',
a.MERCHANT_NAME AS 'merchant_name',
a.PAY_TYPE AS 'pay_type',
a.PAY_MONEY AS 'pay_money',
a.PAY_ZD_MONEY AS 'pay_zd_money',
a.PAY_BALANCE_MONEY AS 'pay_balance_money',
a.PAY_APPRECIATION_MONEY AS 'pay_appreciation_money',
a.PAY_BANKCARD_MONEY AS 'pay_bankcard_money',
a.STATUS AS 'status',
a.FINISH_TIME AS 'finish_time',
a.CREATE_TIME AS 'create_time'
FROM
pay_order a
WHERE a.ID = #{_id,jdbcType=VARCHAR}
</select>
问题原因:实体类中属性与表中字段不一致时,添加resultMap进行重新关联实体类。