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 to solve the problem that the tk.mybatis universal plug-in updateByPrimaryKeySelective cannot update the column automatically?

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

Share

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

This article introduces the knowledge of "tk.mybatis universal plug-in updateByPrimaryKeySelective can not automatically update the column how to solve", in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

There is a table with the following structure (simplified):

CREATE TABLE `tsample` (`id` bigint (20) NOT NULL AUTO_INCREMENT COMMENT 'self-added ID', `empcode` varchar (8) NOT NULL DEFAULT' 'COMMENT' employee ID', `datachange_ lasttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'timestamp', PRIMARY KEY (`id`), UNIQUE KEY `idx_unique_ empcode` (`empcode`), KEY `idx_datachange_ lasttime` (`datachange_ lasttime`) ENGINE=InnoDB AUTO_INCREMENT=561 DEFAULT CHARSET=utf8mb4 COMMENT='test'

There is a column of datachange_lasttime that automatically updates mysql to the current time when update is set, so that as long as there is a change in the record, you can know when it changed through this column (this is also one of the database development specifications of many companies)

Then a convenient method is provided in tk.mybatis: updateByPrimaryKeySelective, which is used as follows:

@ Testpublic void testDataChangeLastTime () {SampleEntity sample = sampleEntityMapper.selectByPrimaryKey; long changeLastTime1 = sample.getDatachangeLasttime (). GetTime (); sample.setEmpcode ("TEST"); int affectedRows = sampleEntityMapper.updateByPrimaryKeySelective (sample); System.out.println (affectedRows); long changeLastTime2 = sample.getDatachangeLasttime (). GetTime (); Assert.assertNotEquals (changeLastTime1, changeLastTime2);}

The code is very simple, first according to the primary key id, take out a record, and then according to the business requirements, modify a column, and then submit. After running, it is found that the datachange_lasttime column is not updated to the current time as expected, and is still the old timestamp. (the above unit test will fail)

Adjust the log level to DEBUG, and observe the final generated update statement, as follows:

22 Preparing 41 1 Preparing 23.933 [main] Preparing-= > UPDATE t_sample SET id = id,empcode =?, datachange_lasttime =? WHERE id =?

22 Parameters 41 1V 23.936 [main] DEBUG-= > Parameters: TEST (String), 2018-09-07 17 Fringe 01 (Timestamp), 560 (Long)

22 main 41 Preparing Preparing 23.981 [main] UPDATE t_sample SET id = id,empcode =? WHERE id =?

22 Parameters 06.300 [main] DEBUG-> Parameters: TEST (String), 560 (Long)

22PUR 06.342 [main] DEBUG-

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: 0

*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