Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java generate date and time and store it in Mysql database

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Java how to generate date and time stored in Mysql database", in daily operation, I believe many people in Java how to generate date and time stored in Mysql database problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Java how to generate date and time stored in Mysql database" doubts helpful! Next, please follow the small series to learn together!

I. Creating database standards

1. Three fields are required for a table: id, gmt_create, gmt_modified

gmt_create is the creation time, gmt_modified is the update time

3. However, the default settings for creation time and update time should not be set by the database, and should be managed by the Handler of Mybatis-plus.

CREATE TABLE `ums_member`( `id` bigint(20) NOT NULL, `username` varchar(64) DEFAULT NULL, `password` varchar(64) DEFAULT NULL, `icon` varchar(500) DEFAULT NULL, `email` varchar(100) DEFAULT NULL, `nick_name` varchar(200) DEFAULT NULL, `note` varchar(500) DEFAULT NULL,`gmt_create` datetime DEFAULT NULL, `gmt_modified` datetime DEFAULT NULL, `login_time` datetime DEFAULT NULL, `status` int(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

III. Use steps

1. entity class

@Data@AllArgsConstructor@NoArgsConstructor@TableName(value = "ums_member")public class UmsMember implements Serializable { @TableId(value = "id", type = IdType.NONE) private Long id; @TableField(value = "username") private String username; @TableField(value = "password") private String password; @TableField(value = "icon") private String icon; @TableField(value = "email") private String email; @TableField(value = "nick_name") private String nickName; @TableField(value = "note") private String note; @TableField(value = "gmt_create",fill = FieldFill.INSERT) private Date gmt_create; @TableField(value = "gmt_modified",fill = FieldFill.INSERT_UPDATE) private Date gmt_modified; @TableField(value = "login_time") private Date loginTime; @TableField(value = "status") private Integer status; private static final long serialVersionUID = 1L;}

2. Entity class Operating database Time tool class

/** * @author: Mr.ZJW * @date: Created 2022/3/2 10:08 * @description: Database automatic insertion time tool class */@Componentpublic class MyHandler implements MetaObjectHandler { public void insertFill(MetaObject metaObject) { System.out.println("Add insertion time"); this.setFieldValByName("gmt_create",new Date(),metaObject); this.setFieldValByName("gmt_modified",new Date(),metaObject); } public void updateFill(MetaObject metaObject) { System.out.println("Update insertion time"); this.setFieldValByName("gmt_modified",new Date(),metaObject); }}

3. test

@SpringBootTest@RunWith(SpringRunner.class)public class UmsMeberTest { @Autowired private UmsMemberMapper umsMemberMapper; @Test public void test01(){ UmsMember umsMember = new UmsMember(); umsMember.setUsername("jowell"); umsMember.setStatus(0); umsMember.setPassword("jowell"); umsMember.setNote("not"); umsMember.setNickName("cike"); umsMember.setEmail("2280252534@qq.com"); umsMemberMapper.insert(umsMember); }

Successful operation:

At this point, the study of "how Java generates date and time into Mysql database" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 225

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report