SSH注解插入数据库时间类型 时分秒丢失的问题
做一个CS结构的应用时,从客户端传一个时间的字符串到服务器端,结果发现时分秒丢失了。客户端是Android开发的,服务器端是SSH注解做的。 于是在百度中找了各种答案,第一种是说要在实体类的get方法上面添加@Temporal(TemporalType.TIMESTAMP)注解,果断添加
做一个CS结构的应用时,从客户端传一个时间的字符串到服务器端,结果发现时分秒丢失了。客户端是Android开发的,服务器端是SSH注解做的。
于是在百度中找了各种答案,第一种是说要在实体类的get方法上面添加@Temporal(TemporalType.TIMESTAMP)注解,果断添加上去,结果又报了错误。
<span style="font-size:18px;">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.hpsvse.traffic.entity.Income.incomeDate</span>
错误原因是说@Temporal 只支持java.util.Date 或 java.util.Calendar的参数。便又把返回值为java.sql.Timestamp的类型换成java.util.Date的类型,结果是虽然没报错,但是时分秒还是没有插入到数据库。
………………
纠结很久,突然眼前一亮,发现是不是长度设置不够长或者是我传入的日期格式有问题,因为我之前传的是 2015-03-24 10:22(缺少了秒),变将两种可能都尝试了一下:
第一种是吧length的属性值改为了50,发现并没有任何效果,如下:
@Temporal(TemporalType.TIMESTAMP) @Column(name = "income_date", length = 50)
把之前请求服务器的时间字符串2015-03-24 10:22在后面补了两个零,变成2015-03-24 10:22:00标准的yyyy-MM-dd HH-mm-ss的格式,结果奇迹就出现了。总结起来,其实之前都是一场闹剧,就是因为在时间后面少补了两个零。
最后在总结一下,让数据插入数据库其实很简单:
第一种方法,定义一个java.util.Date的类型,然后在生成的get方法上面加上一句@Temporal(TemporalType.TIMESTAMP)的注解,然后把属性length的长度改的长一点,感觉19就够用了。代码如下:
<span style="white-space:pre"> </span>private Date incomeDate; @Temporal(TemporalType.TIMESTAMP) @Column(name = "income_date", length = 19) public Date getIncomeDate() { <span style="white-space:pre"> </span>return this.incomeDate; } public void setIncomeDate(Date incomeDate) { this.incomeDate = incomeDate; }
<span style="white-space:pre"> </span>private Timestamp incomeDate; @Column(name = "income_date", length = 19) public Timestamp getIncomeDate() { return this.incomeDate; } public void setIncomeDate(Timestamp incomeDate) { this.incomeDate = incomeDate; }
在此,非常感谢另一位牛人的博客指点,在此受小弟一拜了。http://blog.sina.com.cn/s/blog_82a09f100101a76j.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Annotations in the JUnit framework are used to declare and configure test methods. The main annotations include: @Test (declaration of test methods), @Before (method run before the test method is executed), @After (method run after the test method is executed), @ BeforeClass (method that runs before all test methods are executed), @AfterClass (method that runs after all test methods are executed), these annotations help organize and simplify the test code, and improve the reliability of the test code by providing clear intentions and configurations. Readability and maintainability.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Annotations are crucial in Google Guice for declaring dependencies, binding providers, and configuring injection behavior. Developers can declare dependencies by annotating fields or constructor parameters with @Inject, mark methods that provide dependencies with the @Provides annotation, and bind providers and configure injection behavior through Guice modules.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Go functions can return multiple values of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.
