Mybatis查询返回对象为null

Mapper.xml中映射sql如下,查询返回对象Production 为null。

 <select id="selectOneByTime" resultType="model.Production">
    select *
    from CNC_Production
    where dataTime &lt; #{date}
    limit 1
  </select>

Mysql中对应表明为 XXX_Production。
表名与其生成的model名不同,resultType需要完全相同才能对应。
需要在mapper.xml文件中增加resultMap的声明。

 <resultMap id="ResultMap" type="model.Production" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="machineID" property="machineid" jdbcType="INTEGER" />
    <result column="yield" property="yield" jdbcType="INTEGER" />
    <result column="programCode" property="programcode" jdbcType="VARCHAR" />
    <result column="StartTime" property="starttime" jdbcType="TIMESTAMP" />
    <result column="EndTime" property="endtime" jdbcType="TIMESTAMP" />
    <result column="dataTime" property="datatime" jdbcType="TIMESTAMP" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="UpdateTime" property="updatetime" jdbcType="TIMESTAMP" />
  </resultMap>
  <select id="selectOneByTime" resultMap="ResultMap">
    select *
    from CNC_Production
    where dataTime &lt; #{date}
    limit 1
  </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,可以通过自定义ResultMap来实现返回自定义对象。首先,你需要在Mapper XML文件中定义一个ResultMap,指定返回对象类型和对应的数据库字段。然后,在查询语句中使用该ResultMap来映射查询结果到自定义对象。 以下是一个示例代码: ```xml <!-- 定义ResultMap --> <resultMap id="CustomResultMap" type="com.example.CustomObject"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <!-- 其他属性映射 --> </resultMap> <!-- 查询语句 --> <select id="getCustomObject" resultMap="CustomResultMap"> SELECT id, name, age FROM your_table WHERE id = #{id} </select> ``` 在上面的示例中,我们定义了一个CustomResultMap来映射查询结果到CustomObject对象。在查询语句中,我们使用了该ResultMap来指定返回对象类型和对应的数据库字段。 请注意,你需要根据自己的实际情况修改CustomObject的属性和数据库字段的映射关系。 希望这个示例能帮到你。如果还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *2* [自定义MyBatis返回Map对象](https://blog.csdn.net/zzh87615/article/details/50828687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Mybatis之TypeHandler(自定义接收返回数组或对象的数据类型)](https://blog.csdn.net/qq_58772217/article/details/125623156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值