提交 a5594849 authored 作者: inroi's avatar inroi

使用DataSourceProperties

上级 03357485
......@@ -6,6 +6,7 @@ import com.shr.common.response.RestResponse;
import java.sql.ResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
......@@ -21,7 +22,7 @@ import com.shr.utils.*;
public class ToDoTaskController {
@Autowired
DataSource dataSource;
DataSourceProperties dataSource;
@AccessLog("待办任务列表总数")
@GetMapping("/assign")
......
......@@ -2,6 +2,8 @@ package com.shr.utils;
import java.sql.*;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
/**
* 直接访问数据库
......@@ -13,15 +15,15 @@ public class DBHelper {
private static DBHelper dbHelper=null;
private String jdbcConnectionStr;
private Connection connection;
private DataSource dataSource;
private DataSourceProperties dataSource;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private DBHelper() {
jdbcConnectionStr = dataSource.url;
jdbcConnectionStr = dataSource.getUrl();
}
public static DBHelper createDBHelper(DataSource dataSource) {
public static DBHelper createDBHelper(DataSourceProperties dataSource) {
dbHelper = new DBHelper();
dbHelper.dataSource = dataSource;
......@@ -32,7 +34,7 @@ public class DBHelper {
public void getConn() {
try {
connection = DriverManager.getConnection(jdbcConnectionStr, dataSource.username, dataSource.password);
connection = DriverManager.getConnection(jdbcConnectionStr, dataSource.getUsername(), dataSource.getPassword());
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
......@@ -103,7 +105,7 @@ public class DBHelper {
private void initDB() {
try {
Class.forName(dataSource.driverclassname);
Class.forName(dataSource.getDriverClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
......
package com.shr.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
@Data
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSource {
public String url;
public String username;
public String password;
@Value("${datasource.driver-class-name}")
public String driverclassname;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论