一、pom.xml添加依赖
<!-- mybatis plus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-support</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- 阿里巴巴 数据源 Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!-- lombok 工具 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<!-- 阿里巴巴 json 解析 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
二、配置application.properties
########################################################
###MySql (MySqlConfiguration)
########################################################
spring.druid.driverClassName=com.mysql.jdbc.Driver
spring.druid.url=jdbc:mysql://192.168.8.229/mini_wechat?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.druid.username=root
spring.druid.password=1234
spring.druid.maxActive=30
spring.druid.minIdle=5
spring.druid.maxWait=10000
spring.druid.validationQuery="SELECT X"
三、Config注册bean
因为我没有直接在application.properties配置数据库,而只是单纯记录数据,所以需要配置下bean
@Configuration
@EnableConfigurationProperties(value = {MybatisPlusProperties.class})
public class MybatisPlusConfig {
private MybatisPlusProperties mybatisPlusProperties;
public MybatisPlusConfig(MybatisPlusProperties mybatisPlusProperties) {
this.mybatisPlusProperties = mybatisPlusProperties;
}
/**
* 数据源
* @return
*/
@ConfigurationProperties(prefix = "spring.druid")
@Bean(initMethod = "init",destroyMethod = "close")
public DruidDataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
List<Filter> list = new ArrayList<>();
list.add(statFilter());
druidDataSource.setProxyFilters(list);
return druidDataSource;
}
/**
* 打印慢日志的Filter
*/
@Bean
public Filter statFilter(){
StatFilter statFilter = new StatFilter();
statFilter.setSlowSqlMillis(1000);
statFilter.setLogSlowSql(true);
statFilter.setMergeSql(true);
return statFilter;
}
/**
* Druid 监控
*/
@Bean("mybatisSqlSession")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfiguration globalConfiguration) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setTypeAliasesPackage(mybatisPlusProperties.getTypeAliasesPackage());
sqlSessionFactory.setTypeEnumsPackage(mybatisPlusProperties.getTypeEnumsPackage());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
sqlSessionFactory.setConfiguration(configuration);
sqlSessionFactory.setPlugins(new Interceptor[]{
new PaginationInterceptor(),
new PerformanceInterceptor(),
new OptimisticLockerInterceptor()
});
sqlSessionFactory.setGlobalConfig(globalConfiguration);
return sqlSessionFactory.getObject();
}
@Bean
public GlobalConfiguration globalConfiguration() {
GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
conf.setIdType(IdType.AUTO.getKey());
return conf;
}
}
四、测试
@RestController
@RequestMapping("/test")
public class TestMain {
@Autowired
private LunarMapper lunarMapper;
@Autowired
private DayMapper dayMapper;
@Autowired
private MonthMapper monthMapper;
@GetMapping(value = "/show")
public JSONObject testEnum() {
EntityWrapper<LunarDO> wrapper = new EntityWrapper<>();
LunarDO lunarDOS = lunarMapper.selectById(1);
System.out.println(lunarDOS);
return null;
}
}