很久沒寫文章了,一方面是最近幾個月比較忙,沒太多時間,另一方面是最近拖延癥嚴重,寫文章的想法總是一拖再拖。今天找一個小案例寫一下,與懶惰對抗一下。
首先說一下背景,我們在項目中做數據持久化一般都是用mybatis或者hibernate開發框架,進行數據庫連接和操作,最近做GIS仿真產品研發,根據需求需要保存三部分數據:1、業務數據,數據量比較小;2、GIS數據,需要用到空間關系;3、物聯數據,數據量大,在我們開發自測階段數據量就可以達到每天百萬以上。根據以上數據特點,我們使用了傳統的MySQL數據庫、空間數據庫PostgreSQL、TD engine時序數據庫,項目中做了spring boot多數據源動態切換。今天的重點不是多數據源的實現,這個應用以后會在另外一篇文章中介紹;在研發過程中我們需要與其他系統對接的場景,連接SQLServer拉取數據,項目本身已經做了多數據源,如果繼續添加數據源就加大了系統的難度,所以就用jdbc連接數據庫的方式連接外部數據源,下面看代碼。
一、引入依賴jar包
項目用的是Spring Boot,創建好項目以后,引入下面依賴:
1
2
3
4
5
6
7
8
9
|
<dependencies> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency> </dependencies> |
二、Utils開發
1、創建實體類,實現org.springframework.jdbc.core.RowMappe接口的mapRow(ResultSet rs, int rowNum)方法。
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
|
package com.johan.handler.task.iotSync.bean; import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; /** * @author: Johan * @date: 2021/10/18 * @desc: IOT信息 */ @Data public class IotDG implements RowMapper { /** * 標簽名稱 */ private String tagName; /** * 標簽描述 */ private String tagDesc; /** * 標簽值 */ private Double tagVal; /** * 標簽單位 */ private String tagUnit; /** * 類型,0 壓力,1 流量 */ private String type; @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { IotDG iotDG = new IotDG(); iotDG.setTagName(rs.getString( "TagName" )); iotDG.setTagDesc(rs.getString( "TagDesc" )); iotDG.setTagVal(rs.getDouble( "Value" )); iotDG.setTagUnit(rs.getString( "TagUnit" )); iotDG.setType(rs.getString( "Type" )); return iotDG; } } |
2、連接數據庫,讀取表數據
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
|
package com.johan.handler.task.iotSync.iotConvert; import com.johan.handler.task.iotSync.bean.IotDG; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import java.sql.*; import java.util.List; /** * @author johan * @Description SQL server * @time 2021/10/18 18:26 */ public class JDBCUtils { private static JdbcTemplate jdbcTemplate; static { String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; String url = "jdbc:sqlserver://10.25.23.172:1433;databaseName=SCADA_Data_3040" ;//連接地址 String user = "SLSL" ; //用戶 String password = "Admin@3040" ; //密碼 DriverManagerDataSource dataSource= new DriverManagerDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driver); dataSource.setUsername(user); dataSource.setPassword(password); jdbcTemplate= new JdbcTemplate(dataSource); } public static List<IotDG> listAll( int type){ String sql = "SELECT * FROM RealData where Type=" + type; // System.out.println(iotDGList); return jdbcTemplate.query(sql, new IotDG()); } } |
3、測試
方法是靜態的,直接調用即可。
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
|
package com.johan.domain.iot; import com.johan.handler.task.iotSync.bean.IotDG; import com.johan.handler.task.iotSync.iotConvert.JDBCUtils; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; /** * @author johan * @time 2021/10/22 17:57 */ @SpringBootTest @Slf4j public class IotDomainTest { @Test public void jdbcTest(){ List<IotDG> iotDGList = JDBCUtils.listAll( 0 ); System.out.println(iotDGList); } } |
不只是SQLServer,我們常用的MySQL、Oracle等都可以用JdbcTemplate連接。
到此這篇關于Java中String的JdbcTemplate連接SQLServer數據庫的文章就介紹到這了,更多相關java中JdbcTemplate連接SQLServer數據庫內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/JohanChan/p/15469453.html