2)编写Starter
2.1 pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
2.2 编写Starter内容,已mybatis拦截器举例
package com.lhy.mybatis.plugin.interceptor;
import com.lhy.mybatis.plugin.store.UserStore;
import com.lhy.mybatis.plugin.annotation.CreateTime;
import com.lhy.mybatis.plugin.annotation.UpdateTime;
import com.lhy.mybatis.plugin.annotation.CreateBy;
import com.lhy.mybatis.plugin.annotation.UpdateBy;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;
import javax.annotation.Resource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class CreateAndUpdateInterceptor implements Interceptor {
@Resource
private UserStore userStore;
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
// 获取 SQL 命令
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
// 获取参数
Object parameter = invocation.getArgs()[1];
// 获取私有成员变量
Field[] declaredFields = parameter.getClass().getDeclaredFields();
for (Field field : declaredFields) {
if (field.getAnnotation(CreateTime.class) != null) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, new Date());
}
}
if (field.getAnnotation(CreateBy.class) != null) {
Long currUserId = userStore.getCurrUserId();
if (SqlCommandType.INSERT.equals(sqlCommandType) && null != currUserId) {
field.setAccessible(true);
field.set(parameter, currUserId);
}
}
if (field.getAnnotation(UpdateTime.class) != null) {
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, new Date());
}
}
if (field.getAnnotation(UpdateBy.class) != null) {
Long currUserId = userStore.getCurrUserId();
if ((SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType))
&& null != currUserId) {
field.setAccessible(true);
field.set(parameter, currUserId);
}
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o, this);
}
@Override
public void setProperties(Properties properties) {
}
}
2.3 编写AutoConfiguration
package com.lhy.mybatis.plugin;
import com.lhy.mybatis.plugin.interceptor.CreateAndUpdateInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PluginAutoConfiguration {
@Bean
public CreateAndUpdateInterceptor myCreateAndUpdateInterceptor() {
CreateAndUpdateInterceptor createAndUpdateInterceptor = new CreateAndUpdateInterceptor();
return createAndUpdateInterceptor;
}
}
2.4 配置spring.factories(resources/META-INFspring.factories)
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lhy.mybatis.plugin.PlginAutoConfiguration