2.集成mybatis-plus
引入mybatis-plus所需依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--单元测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
新建配置文件
# 服务端口
# 服务端口
server:
port: 8081
# mysql数据库连接
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://121.37.241.78:13306/demo?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
username: chase
password: 123456
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*.xml
global-config:
# 逻辑删除配置
db-config:
logic-delete-field: deleted
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
id-type: auto
xml文件扫描
新建xml文件
package com.chase.frame.handler;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author chase
* @Desc
* @date 2022/8/18 16:28
*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
paginationInnerInterceptor.setOptimizeJoin(true);
OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
mybatisPlusInterceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
return mybatisPlusInterceptor;
}
@Override
public void insertFill(MetaObject metaObject) {
// 执行插入操作时执行该逻辑
// 实体类属性名称和要自动填充的值
this.setFieldValByName("createTime", new Date(), metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
// 执行更新操作时执行该逻辑
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
新建mybatis-plus配置类
/**
* @author chase
* @Desc
* @date 2022/8/18 16:26
*/
@RestController
@RequestMapping("/ruleDefineType")
public class RuleDefineTypeController {
@Autowired
private RuleDefineTypeMapper ruleDefineTypeMapper;
@GetMapping("/all")
public List<RuleDefineType> all() {
LambdaQueryWrapper<RuleDefineType> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(RuleDefineType::getRemark, "1");
List<RuleDefineType> ruleDefineTypes = ruleDefineTypeMapper.selectList(queryWrapper);
return ruleDefineTypes;
}
}
创建一个controller
/**
* <p>
* 规则定义类型表 Mapper 接口
* </p>
*
* @author chase
* @since 2022-08-18
*/
@Component
public interface RuleDefineTypeMapper extends BaseMapper<RuleDefineType> {
}
创建一个mapper接口
测试结果,mybatis-puls引入成功 可以进行开发了