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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
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.

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

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

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

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

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.

How do annotations in the Mockito framework simplify stub generation and verification? How do annotations in the Mockito framework simplify stub generation and verification? May 06, 2024 pm 05:48 PM

Mockito framework annotations simplify the stub generation and verification process: @Mock: automatically generate and manage mock objects. @Captor: Capture the parameter value passed to the mock method. @InjectMocks: Automatically inject mock objects into the class under test. @Spy: Create some stub objects and retain the original method implementation.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

See all articles