1.1作用
1:建表语句 2:运行插入的sql语句
1.2源码解读
只要两个条件符合就执行,DataSourceInitializationMode属性,看起来是通过配置文件获取的
private boolean isEnabled() {
DataSourceInitializationMode mode = this.properties.getInitializationMode();
if (mode == DataSourceInitializationMode.NEVER) {
return false;
} else {
return mode != DataSourceInitializationMode.EMBEDDED || this.isEmbedded();
}
}
private boolean isEmbedded() {
try {
return EmbeddedDatabaseConnection.isEmbedded(this.dataSource);
} catch (Exception var2) {
logger.debug("Could not determine if datasource is embedded", var2);
return false;
}
}
从指定的路径获取脚本
private List<Resource> getScripts(String propertyName, List<String> resources, String fallback) {
if (resources != null) {
return this.getResources(propertyName, resources, true);
} else {
String platform = this.properties.getPlatform();
List<String> fallbackResources = new ArrayList();
fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
fallbackResources.add("classpath*:" + fallback + ".sql");
return this.getResources(propertyName, fallbackResources, false);
}
}
初始化Schema.,其第三个参数为
data
void initSchema() {
List<Resource> scripts = this.getScripts("spring.datasource.data", this.properties.getData(), "data");
if (!scripts.isEmpty()) {
if (!this.isEnabled()) {
logger.debug("Initialization disabled (not running data scripts)");
return;
}
String username = this.properties.getDataUsername();
String password = this.properties.getDataPassword();
this.runScripts(scripts, username, password);
}
}
进行判断,其中第三个参数为
schema
boolean createSchema() {
List<Resource> scripts = this.getScripts("spring.datasource.schema", this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) {
if (!this.isEnabled()) {
logger.debug("Initialization disabled (not running DDL scripts)");
return false;
}
String username = this.properties.getSchemaUsername();
String password = this.properties.getSchemaPassword();
this.runScripts(scripts, username, password);
}
return !scripts.isEmpty();
}
从getScripts(….)可知,其中其默认的文件路径为
在2个默认路径下实现sql的建表
classpath*:schema-all.sql
或者 classpath*:schema.sql
List<String> fallbackResources = new ArrayList();
fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
fallbackResources.add("classpath*:" + fallback + ".sql");
测试:在路径下新建sql文件
sql文件:建立一张普通表.(已实现数据库的连接)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
1.1从database中可以看出已经生成了department表 注!2.0必须在配置文件中加入initialization-mode: always,不然无法生效.
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200801232005573.PNG)
1.2也可以通过schema指定指定的路径而不使用其默认的文件路径[List]
spring:
datasource:
username: root
password: 12xxsd
url: jdbc:mysql://localhost:xxxx/student?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
initialization-mode: always
schema:
- classpath:department.sql
2:同理也可以操作实行
从JdbcTemplateConfiguration中看出springboot已自动帮我们配置了jdbctemplate,我们可以直接使用即可.
@RestController
public class ControllerTest {
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping("/hello")
public Map<String, Object> hello(){
List<Map<String, Object>> maps = jdbcTemplate.queryForList("select *from account");
System.out.println(maps);
return maps.get(0);//返回第一条
}
}
总结
1:**在2个默认路径下实现sql的建表**
classpath*:schema-all.sql
或者 classpath*:schema.sql
2
必须配置initialization-mode: always
3
可修改其访问文件名
:schema:
– classpath:department.sql
4.
springboot已自动配置Template可直接使用.进行执行sql
版权声明:本文为weixin_45089503原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。