006.Springboot Starter @上源码

1)springboot简单启动

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值