|
@@ -1,5 +1,8 @@
|
|
package com.qmth.boot.mybatis.service;
|
|
package com.qmth.boot.mybatis.service;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
|
+import com.qmth.boot.mybatis.config.MybatisProperties;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -20,30 +23,52 @@ public class DatabaseInitializeService implements ApplicationListener<ContextRef
|
|
|
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
- public DatabaseInitializeService(JdbcTemplate jdbcTemplate) {
|
|
|
|
|
|
+ private MybatisProperties mybatisProperties;
|
|
|
|
+
|
|
|
|
+ public DatabaseInitializeService(JdbcTemplate jdbcTemplate, MybatisProperties mybatisProperties) {
|
|
this.jdbcTemplate = jdbcTemplate;
|
|
this.jdbcTemplate = jdbcTemplate;
|
|
|
|
+ this.mybatisProperties = mybatisProperties;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public synchronized void onApplicationEvent(ContextRefreshedEvent event) {
|
|
public synchronized void onApplicationEvent(ContextRefreshedEvent event) {
|
|
if (event.getApplicationContext().getParent() == null) {
|
|
if (event.getApplicationContext().getParent() == null) {
|
|
- Map<String, SqlProvider> beans = event.getApplicationContext().getBeansOfType(SqlProvider.class);
|
|
|
|
- for (SqlProvider provider : beans.values()) {
|
|
|
|
- String beanName = AopUtils.getTargetClass(provider).getName();
|
|
|
|
- String content = StringUtils.trimToNull(provider.get());
|
|
|
|
- if (content == null) {
|
|
|
|
- throw new RuntimeException("Blank content from SqlProvider: " + beanName);
|
|
|
|
- }
|
|
|
|
- String[] lines = StringUtils.split(content, SQL_SPLITE);
|
|
|
|
- int count = 0;
|
|
|
|
- for (String sql : lines) {
|
|
|
|
- if (StringUtils.isNotBlank(sql)) {
|
|
|
|
- jdbcTemplate.execute(sql);
|
|
|
|
- count++;
|
|
|
|
- }
|
|
|
|
|
|
+ //获取容器中的SqlProvider执行初始化sql
|
|
|
|
+ initBySql(event);
|
|
|
|
+ //若开启schema校验,则读取所有BaseMapper分别尝试select一条记录
|
|
|
|
+ if (mybatisProperties.isSchemaValidate()) {
|
|
|
|
+ schemaValidate(event);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initBySql(ContextRefreshedEvent event) {
|
|
|
|
+ Map<String, SqlProvider> beans = event.getApplicationContext().getBeansOfType(SqlProvider.class);
|
|
|
|
+ for (SqlProvider provider : beans.values()) {
|
|
|
|
+ String beanName = AopUtils.getTargetClass(provider).getName();
|
|
|
|
+ String content = StringUtils.trimToNull(provider.get());
|
|
|
|
+ if (content == null) {
|
|
|
|
+ throw new RuntimeException("Blank content from SqlProvider: " + beanName);
|
|
|
|
+ }
|
|
|
|
+ String[] lines = StringUtils.split(content, SQL_SPLITE);
|
|
|
|
+ int count = 0;
|
|
|
|
+ for (String sql : lines) {
|
|
|
|
+ if (StringUtils.isNotBlank(sql)) {
|
|
|
|
+ jdbcTemplate.execute(sql);
|
|
|
|
+ count++;
|
|
}
|
|
}
|
|
- log.info("Database initialize from SqlProvider: {}, execute count: {}", beanName, count);
|
|
|
|
}
|
|
}
|
|
|
|
+ log.info("Database initialize from SqlProvider: {}, execute count: {}", beanName, count);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void schemaValidate(ContextRefreshedEvent event) {
|
|
|
|
+ Map<String, BaseMapper> beans = event.getApplicationContext().getBeansOfType(BaseMapper.class);
|
|
|
|
+ for (BaseMapper mapper : beans.values()) {
|
|
|
|
+ QueryWrapper wrapper = new QueryWrapper();
|
|
|
|
+ wrapper.last("limit 1");
|
|
|
|
+ mapper.selectOne(wrapper);
|
|
|
|
+ log.info("MybatisPlus schema validate: {}", mapper.getClass().getGenericInterfaces()[0].getTypeName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|