Springboot调⽤mysql的.sql⽂件,执⾏mysql语句前⾔
该篇内容介绍是 调⽤指定的 .sql ⽂件, 执⾏⾥⾯的mysql语句。
正⽂
实战⽰例准备:
1. ⼀张 user 表 :
2.2个简单 SQL⽂件(⾥⾯就简单写点插⼊sql语句):
sql⽂件在项⽬中的位置:
l 依赖:
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据源驱动 1.1.10解决springboot从1.0——2.0版本问题-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
l 配置:
server:
port: 8082
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=true&rewriteBatchedStatements username: root
password: 123456
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: Sql=true;druid.stat.slowSqlMillis=5000
5. 新建⼀个⼯具类ExecuteSQLUtil.java :
可以看到我的有关mysql连接信息在⽰例⾥⾯是从yml⽂件读取的或者是在代码写死,这个⼤家根据具体业务场景和项⽬去调整,甚⾄放在
数据库⾥⾯查询也⾏。
import org.springframework.beans.factory.annotation.Value;
import io.ClassPathResource;
import io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Component;
import java.sql.*;
/**
* @Author: JCccc
* @Description:
* @Date: 2020/11/16
*/
@Component
public class ExecuteSQLUtil {
@Value("${spring.datasource.druid.url}")
private String DB_URL;
@Value("${spring.datasource.druid.username}")
private String DB_USERNAME;
@Value("${spring.datasource.druid.password}")
private String DB_PWD;
public Connection executeSql(String sqlFileName){
Connection connection = null;
try {
String driverClassName = "sql.cj.jdbc.Driver";
// String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=true&rewrite // String DB_USERNAME = "root";
// String DB_PWD = "123456";
Class.forName(driverClassName);
connection = Connection(DB_URL, DB_USERNAME, DB_PWD);
} catch (Exception e) {
e.printStackTrace();
}
ClassPathResource rc = new ClassPathResource(sqlFileName);
EncodedResource er = new EncodedResource(rc, "utf-8");
return connection;
}
}
6. 写个测试接⼝,使⽤postman调⽤⼀下:
/**
* @Author: JCccc
* @Description:
* @Date: 2020/11/16
*/
@RestController
public class TestController {
@Autowired
简单的mysql语句ExecuteSQLUtil executeSQLUtil;
@GetMapping("/executeMysql")
public String executeMysql(@RequestParam("scriptName") String scriptName) { uteSql("static/"+scriptName+".sql");
return "ok";
}
}
测试:
运⾏结果:
好的该篇内容就到这吧。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论