utils 中 @Value 返回为null

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
 * @Title: JDBCUtils.java
 * @Description:
 * @author  zhangkai
 * @date 2020年4月3日上午9:56:32
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Component
public class JDBCUtils {
 
     
    private static  String user;
    private static String password;
    private static String url;
    private static String driver;
 
    /**
     * 功能:用于获取连接
     *
     * @return Connection连接对象 @ throws Exception
     *
     */
    public Connection getConnection() {
        // 获取连接
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 功能:释放资源 通用的释放资源方法,无用参数留null
     *
     * @param connection
     * @param statement
     *            PreparedStatement是其子类,使用多态,也可引用
     * @param resultSet
     */
    public void close(Connection connection, Statement statement, ResultSet resultSet) {
        // 使用try-catch方式处理异常,免去调用者再次处理
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            // 编译时异常转为运行时异常
            throw new RuntimeException(e);
        }
    }
 
    public int toUpdateDelStatus(String tableName) throws Exception {
        Connection connection = null;
        Statement statement = null;
        int i = 0;
        try {
            String sql = "UPDATE " + tableName + " SET del_status = '1' ";
            connection = this.getConnection();
            statement = connection.createStatement();
            i = statement.executeUpdate(sql);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close(connection, statement, null);
            return i;
        }
    }
    @Value("${spring.datasource.master.username}")
    public  void setUser(String users) {
        user = users;
    }
    @Value("${spring.datasource.master.username}")
    public  void setPassword(String passwords) {
        password = passwords;
    }
    @Value("${spring.datasource.master.jdbc-url}")
    public  void setUrl(String urls) {
        url = urls;
    }
    @Value("${spring.datasource.master.driver-class-name}")
    public  void setDriver(String drivers) {
        driver = drivers;
    }
 
}

 需要注意 :

  1>类上面需要@component  

  2>需要编写set 方法

 

查考文章:https://blog.csdn.net/mononoke111/article/details/81088472#commentBox

 

posted @   自己的太阳  阅读(615)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示