How to insert milliseconds into MySQL in MyBatis?

Source: Internet
Author: User
Tags mysql client mysql in



Because of the business scenario requirements, you need to record the exact time, but don't want to use the int type to store the time because it's less readable.



How do I insert a millisecond-level time into a database in MyBatis?



First, let's look at how to insert milliseconds into the database. This is the key issue, and if the time to insert directly into the database is not complete, then do not want to use other tools to complete.
You can use the MySQL client tool to insert a time, set to DateTime, and try it:


CREATE TABLE `t_job_record` (
`ID ` int (11) unsigned not null auto increment comment 'primary key ID',
`Job_name ` varchar (50) not null default '' comment 'the name of the job. It is recommended to use the job method name',
`Status ` tinyint (2) not null default '0' comment 'status, 0: not executed, 1: executing, 2: execution interrupted, 5: execution completed',
`Exec "date" start ` varchar (30) not null default '"comment" start date of execution,
`Exec ﹣ date ﹣ end ` varchar (30) not null default '' comment 'execution end date',
`Req? Params ` varchar (1000) not null default '' comment 'original request parameter',
`Job_params ` varchar (500) not null default '' comment 'parameter to execute job',
`Remark ` varchar (500) not null default 'comment' remarks, such as brief description in execution,
`Update_times ` int (11) not null default '0' comment 'the number of times the record has been updated',
`Add_ip ` varchar (32) not null default '' comment 'the original IP of the request,
`Exec ﹣ server ﹣ IP ` varchar (32) not null default '' comment 'IP to execute task',
`Req_server_ip ` varchar (32) not null default '' comment 'request execution machine IP',
`Job end time ` datetime() not null default '1970-01-01 00:00:00' comment 'end time of job execution, start time is create time',
`Create "time ` datetime not null default current" timestamp comment "create time",
`Update 'time ` datetime not null default current' timestamp on update current 'timestamp comment' modification time ',
PRIMARY KEY (`id`),
KEY `exec_date` (`exec_date_start`,`job_name`),
KEY `idx_create_time` (`create_time`),
KEY `idx_update_time` (`update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO t_job_record SET job_name = ‘999‘, job_end_time = ‘2017-10-27 12:15:42.664‘ ; 


After the insertion is found, Job_end_time = ' 2017-10-27 12:15:43 '; The change Time format is datetime (3) and 3 decimal places are not satisfied


ALTER TABLE DATETIME (3DEFAULT'1970-01-01 00:00:00.000'notNULL'  The end time of the job execution, the start time is create_time'


Then the same insertion is performed, and a write of milliseconds is completed.



You can use the direct Insert method to record, just consider how to use the tool to complete the insertion.


// get the millisecond time string, and then insert New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. S "). Format (new Date ());
//Insert database
<insert id="addJobExecRecord" parameterType="map" useGeneratedKeys="true" keyProperty="id">
INSERT INTO t_job_record SET job_name = #{jobName}
<if test="jobEndTime != null">
,job_end_time = #{jobEndTime}
</if>
</insert> 


In this way, you can record the exact time.



Of course, there is another way to do this is to set the time to varchar or char, and then write it as a string.



Note: When recording amounts, be sure to use decimal instead of float or dubble or use integers to record, and then convert the format in your code, because decimal is more accurate.



Because of the special needs scene, record the nostalgia.



How to insert milliseconds into MySQL in MyBatis?


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.