因为业务需求,springboot 整合一下通用Mapper
引入Mybatis和通用mapper依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.1.5version>
dependency>
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`user_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`pass_word` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`real_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`update_time` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
create_time也是同理
/**
* 每次update会自动更新时间
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface UpdateTime {
String value() default "";
}
private Integer id;
private String userName;
private String passWord;
private String realName;
@UpdateTime
private Date updateTime;
我们写一个拦截器,当我们每次执行修改/新增的时候,我们自动的给update_time字段附上最新的时间值。不需要每次都手动赋值。
/**
* Mybatis update 拦截器
* @author 北堂飘霜
*/
@Component
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
public class MybatisInterceptor implements Interceptor {
/**
* 正则匹配 insert、delete、update操作
*/
private static final String REGEX = ".*insert\\\\u0020.*|.*delete\\\\u0020.*|.*update\\\\u0020.*";
@Override
public Object intercept(Invocation invocation) throws Throwable {
User user = (User) invocation.getArgs()[1];
Field[] fields = user.getClass().getDeclaredFields();
for (Field field : fields) {
UpdateTime annotation = field.getAnnotation(UpdateTime.class);
if(annotation != null){
// 设置 值
field.setAccessible(true);
field.set(user,new Date());
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
//获取代理权
if (o instanceof Executor) {
//如果是Executor(执行增删改查操作),则拦截下来
return Plugin.wrap(o, this);
} else {
return o;
}
}
@Override
public void setProperties(Properties properties) {
//读取mybatis配置文件中属性
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectAll() {
System.out.println(userMapper);
return userMapper.selectAll();
}
@Override
public User getById(String id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public String update(User user) {
userMapper.insert(user);
return "ok";
}
}
mapper需要继承通用mapper的mapper
public interface UserMapper extends Mapper<User> {
}
完成了以上操作,如果启动会报错,我们需要指定mapper的扫描路径的。
@SpringBootApplication
@MapperScan("com.xxx.xxx.dao")
public class MabatisInterceptorApplication {
public static void main(String[] args) {
SpringApplication.run(MabatisInterceptorApplication.class, args);
}
}
完成以上代码,每次我们去update/insert的时候,可以把时间自动加上去,当然了,因为需求就是加上最新的时间,所以对注解没怎么处理。事实上可以给注解赋值,甚至可以详细到加到秒,例如:
@UpdateTime(value = "now()") // 当前时间
@UpdateTime(value = "pre()") // 前一天零点
@UpdateTime(value = "next()") // 后一天零点
@UpdateTime(value = "yyyy-MM-DD hh:mm:ss") // 任意时间
@UpdateTime(value = "noop") // 不做任何处理
这些如果有兴趣,也可以实现更加细粒度的控制,这个业务算是抛砖引玉了。
下期会做springboot + shiro 的整合 顺便涵盖一下web的基本使用,和基本原理。