当前位置:   article > 正文

Springboot 调用mysql的.sql文件,执行mysql语句_springboot 执行sql语句

springboot 执行sql语句

前言

该篇内容介绍是 调用指定的 .sql 文件, 执行里面的mysql语句

 

正文 

 

实战示例准备:

1. 一张 user 表 :


 

2.2个简单 SQL文件(里面就简单写点插入sql语句):

sql文件在项目中的位置:

 

3.pom.xml 依赖:

  1. <!--web-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- mybatis依赖 -->
  7. <dependency>
  8. <groupId>org.mybatis.spring.boot</groupId>
  9. <artifactId>mybatis-spring-boot-starter</artifactId>
  10. <version>1.3.0</version>
  11. </dependency>
  12. <!--mysql驱动-->
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. </dependency>
  17. <!-- druid数据源驱动 1.1.10解决springboot从1.0——2.0版本问题-->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>druid-spring-boot-starter</artifactId>
  21. <version>1.1.10</version>
  22. </dependency>

4.application.yml 配置:
 

  1. server:
  2. port: 8082
  3. spring:
  4. datasource:
  5. druid:
  6. url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=true&rewriteBatchedStatements=true
  7. username: root
  8. password: 123456
  9. initialSize: 5
  10. minIdle: 5
  11. maxActive: 20
  12. maxWait: 60000
  13. timeBetweenEvictionRunsMillis: 60000
  14. minEvictableIdleTimeMillis: 300000
  15. validationQuery: SELECT 1 FROM DUAL
  16. testWhileIdle: true
  17. testOnBorrow: false
  18. testOnReturn: false
  19. poolPreparedStatements: true
  20. maxPoolPreparedStatementPerConnectionSize: 20
  21. useGlobalDataSourceStat: true
  22. connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

5. 新建一个工具类ExecuteSQLUtil.java :

可以看到我的有关mysql连接信息在示例里面是从yml文件读取的或者是在代码写死,这个大家根据具体业务场景和项目去调整,甚至放在数据库里面查询也行。

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.core.io.ClassPathResource;
  3. import org.springframework.core.io.support.EncodedResource;
  4. import org.springframework.jdbc.datasource.init.ScriptUtils;
  5. import org.springframework.stereotype.Component;
  6. import java.sql.*;
  7. /**
  8. * @Author: JCccc
  9. * @Description:
  10. * @Date: 2020/11/16
  11. */
  12. @Component
  13. public class ExecuteSQLUtil {
  14. @Value("${spring.datasource.druid.url}")
  15. private String DB_URL;
  16. @Value("${spring.datasource.druid.username}")
  17. private String DB_USERNAME;
  18. @Value("${spring.datasource.druid.password}")
  19. private String DB_PWD;
  20. public Connection executeSql(String sqlFileName){
  21. Connection connection = null;
  22. try {
  23. String driverClassName = "com.mysql.cj.jdbc.Driver";
  24. // String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=true&rewriteBatchedStatements=true";
  25. // String DB_USERNAME = "root";
  26. // String DB_PWD = "123456";
  27. Class.forName(driverClassName);
  28. connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PWD);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. ClassPathResource rc = new ClassPathResource(sqlFileName);
  33. EncodedResource er = new EncodedResource(rc, "utf-8");
  34. ScriptUtils.executeSqlScript(connection, er);
  35. return connection;
  36. }
  37. }

6. 写个测试接口,使用postman调用一下:

  1. /**
  2. * @Author: JCccc
  3. * @Description:
  4. * @Date: 2020/11/16
  5. */
  6. @RestController
  7. public class TestController {
  8. @Autowired
  9. ExecuteSQLUtil executeSQLUtil;
  10. @GetMapping("/executeMysql")
  11. public String executeMysql(@RequestParam("scriptName") String scriptName) {
  12. executeSQLUtil.executeSql("static/"+scriptName+".sql");
  13. return "ok";
  14. }
  15. }

 

测试:

运行结果:

 

好的该篇内容就到这吧。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/243516
推荐阅读
相关标签
  

闽ICP备14008679号