Home Database Mysql Tutorial SSH注解插入数据库时间类型 时分秒丢失的问题

SSH注解插入数据库时间类型 时分秒丢失的问题

Jun 07, 2016 pm 03:48 PM
ssh one lost insert database time annotation type question

做一个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>
Copy after login

错误原因是说@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)
Copy after login
Copy after login
然后,尝试第二种:

把之前请求服务器的时间字符串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;
	}
Copy after login
 第二种方法,直接定义一个java.sql.Timestamp的类型,如下:
<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;
	}
Copy after login
特别注意的是,传入的值一定要是标准的yyyy-MM-dd HH-mm-ss(如:2015-03-24 10:22:00)的格式,不能缺少任何一部分,要不然时分秒还是会丢失哦~

在此,非常感谢另一位牛人的博客指点,在此受小弟一拜了。http://blog.sina.com.cn/s/blog_82a09f100101a76j.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How are annotations used for test methods in the JUnit framework? How are annotations used for test methods in the JUnit framework? May 06, 2024 pm 05:33 PM

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.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

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

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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())

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

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.

The role and use of annotations in the Google Guice framework The role and use of annotations in the Google Guice framework May 06, 2024 pm 04:21 PM

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.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

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.

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

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.

See all articles