Preface
During daily insertion and modification, you need to insert time frequently, waste time. You can implement the Intercepts annotation of mybatis to obtain the entity and insert the date in the entity.
1. Implement the Interceptor interface and write related logic
package ; import ; import ; import ; import ; import ; import ; import .*; import ; import ; import ; /** * Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) Methods to intercept executors ParameterHandler (getParameterObject, setParameters) Handling of intercepting parameters ResultSetHandler (handleResultSets, handleOutputParameters) Processing of intercepting result sets StatementHandler (prepare, parameterize, batch, update, query) Processing of intercepting Sql syntax construction * @author Administrator * */ @Intercepts({@Signature(type = ,method = "update",args = {,})}) @Component public class HandleTimeInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = (); MappedStatement mappedStatement = (MappedStatement) args[0]; SqlCommandType sqlCommandType = (); if(sqlCommandType== ) { if(args[1] instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) args[1]; (new Date()); String userId=""; try { userId= ().toString(); }catch (Exception e){ // throw new BaseException("No logged in at present"); } (userId); } }else if(sqlCommandType==) { if(args[1] instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) args[1]; (new Date()); String userId=""; try { userId= ().toString(); }catch (Exception e){ //throw new BaseException("No logged in at present"); } (userId); } } return (); } @Override public Object plugin(Object target) { return (target, this); } @Override public void setProperties(Properties properties) { } }
2. Register the plugin to the configuration file of mybatis
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/"> <configuration> <!-- Global Parameters --> <settings> <!-- Make the global mapper enable or disable cache --> <setting name="cacheEnabled" value="true" /> <!-- allowJDBC Support automatic generation of primary keys --> <setting name="useGeneratedKeys" value="true" /> <!-- Configure the default executor.SIMPLEIt's an ordinary actuator;REUSEThe executor reuses preprocessing statements(prepared statements);BATCHThe executor will reuse the statement and perform batch updates --> <setting name="defaultExecutorType" value="SIMPLE" /> <!-- Specify MyBatis The specific implementation of the logs used --> <setting name="logImpl" value="SLF4J" /> <!-- Convert fields using camel nomenclature --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <plugins> <plugin interceptor=""></plugin> </plugins> </configuration>
Summarize
Then you can automatically set the creation time and modification time in the entity
The above is personal experience. I hope you can give you a reference and I hope you can support me more.