Java通过mybatis插入Oracle数据库中Date格式不显示到时分秒问题

我在用mybatis generator生成代码后,执行查询语句时,Oracle里的Date类型字段只精确到年月日,后面时分秒都为零。

后来发现是jdbcType问题,改成 jdbcType="TIMESTAMP" 就可以。(原先默认生成时是jdbcType="DATE")

ps:实体类里Date是Java.util.Date包里的,不是java.sql.Date,否则也会只精确到年月日


实体类

package com.pcmall.domain.sale.erps;

import java.util.Date;

public class Synnexlogs {
    private String sender;

    private String receiver;

    private Date calltime;

    private String inputparam;

    private String outputparam;

    private String type;

    private String remark;

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender == null ? null : sender.trim();
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver == null ? null : receiver.trim();
    }

    public Date getCalltime() {
        return calltime;
    }

    public void setCalltime(Date calltime) {
        this.calltime = calltime;
    }

    public String getInputparam() {
        return inputparam;
    }

    public void setInputparam(String inputparam) {
        this.inputparam = inputparam == null ? null : inputparam.trim();
    }

    public String getOutputparam() {
        return outputparam;
    }

    public void setOutputparam(String outputparam) {
        this.outputparam = outputparam == null ? null : outputparam.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type == null ? null : type.trim();
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark == null ? null : remark.trim();
    }
}

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.pcmall.dao.sale.erps.SynnexlogsMapper" >
  <resultMap id="BaseResultMap" type="com.pcmall.domain.sale.erps.Synnexlogs" >
    <result column="SENDER" property="sender" jdbcType="VARCHAR" />
    <result column="RECEIVER" property="receiver" jdbcType="VARCHAR" />
    <result column="CALLTIME" property="calltime" jdbcType="TIMESTAMP" />
    <result column="INPUTPARAM" property="inputparam" jdbcType="VARCHAR" />
    <result column="OUTPUTPARAM" property="outputparam" jdbcType="VARCHAR" />
    <result column="TYPE" property="type" jdbcType="VARCHAR" />
    <result column="REMARK" property="remark" jdbcType="VARCHAR" />
  </resultMap>
  <insert id="insert" parameterType="com.pcmall.domain.sale.erps.Synnexlogs" >
    insert into SYNNEXLOGS (SENDER, RECEIVER, CALLTIME, 
      INPUTPARAM, OUTPUTPARAM, TYPE, 
      REMARK)
    values (#{sender,jdbcType=VARCHAR}, #{receiver,jdbcType=VARCHAR}, #{calltime,jdbcType=TIMESTAMP}, 
      #{inputparam,jdbcType=VARCHAR}, #{outputparam,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, 
      #{remark,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.pcmall.domain.sale.erps.Synnexlogs" >
    insert into SYNNEXLOGS
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="sender != null" >
        SENDER,
      </if>
      <if test="receiver != null" >
        RECEIVER,
      </if>
      <if test="calltime != null" >
        CALLTIME,
      </if>
      <if test="inputparam != null" >
        INPUTPARAM,
      </if>
      <if test="outputparam != null" >
        OUTPUTPARAM,
      </if>
      <if test="type != null" >
        TYPE,
      </if>
      <if test="remark != null" >
        REMARK,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="sender != null" >
        #{sender,jdbcType=VARCHAR},
      </if>
      <if test="receiver != null" >
        #{receiver,jdbcType=VARCHAR},
      </if>
      <if test="calltime != null" >
        #{calltime,jdbcType=TIMESTAMP},
      </if>
      <if test="inputparam != null" >
        #{inputparam,jdbcType=VARCHAR},
      </if>
      <if test="outputparam != null" >
        #{outputparam,jdbcType=VARCHAR},
      </if>
      <if test="type != null" >
        #{type,jdbcType=VARCHAR},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
</mapper>



  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用MyBatis的参数映射功能将Javajava.sql.Date类型的数据添加到数据库。首先,确保你的实体类的属性类型与数据库字段类型一致。然后,在执行插入操作时,使用MyBatis的#{...}语法将Java对象的属性与数据库字段进行映射。 以下是一个示例代码片段,展示了如何使用MyBatisjava.sql.Date类型的数据添加到数据库: 首先,在你的实体类定义一个java.sql.Date类型的属性,例如: ```java public class YourEntity { private java.sql.Date date; // 其他属性和方法... public java.sql.Date getDate() { return date; } public void setDate(java.sql.Date date) { this.date = date; } } ``` 接下来,在你的Mapper XML文件,编写插入操作的SQL语句,使用#{...}语法将Java对象的属性与数据库字段进行映射,例如: ```xml <insert id="insertEntity" parameterType="YourEntity"> INSERT INTO your_table (date_column) VALUES (#{date}) </insert> ``` 最后,在你的Mapper接口定义一个对应的方法,例如: ```java public interface YourMapper { void insertEntity(YourEntity entity); } ``` 通过调用上述方法并传递带有java.sql.Date属性的实体对象,即可将数据添加到数据库: ```java YourMapper mapper = sqlSession.getMapper(YourMapper.class); YourEntity entity = new YourEntity(); entity.setDate(new java.sql.Date(System.currentTimeMillis())); mapper.insertEntity(entity); sqlSession.commit(); ``` 这样,你就成功将Javajava.sql.Date类型的数据通过MyBatis添加到数据库了。希望对你有所帮助!如有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值