Spring 系列之jdbcTemplate的使用
阿新 • • 發佈:2020-09-26
## Spring系列之 jdbcTemplate
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926101435512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center)
## 啥是jdncTemplate?
t他是spring框架中提供的一個物件,是對原始的jdbcAPI物件的簡單封裝,spring框架為我們提供了很多操作,模板類,比如操作關係型資料庫的jdbcTemplate,操作nosql資料庫的Redis Template,操作訊息佇列的jmsTemplate等等
## JdbcTemplate開發步驟
1.匯入sprign-jdbc和spring-tx座標
2.建立資料庫表和實體
3.建立JdbcTemplate物件
4.執行資料庫操作
**1.匯入sprign-jdbc和spring-tx座標**
```java
org.springframework
spring-test
5.0.2.RELEASE
org.springframework
spring-jdbc
5.0.3.RELEASE
org.springframework
spring-tx
5.0.3.RELEASE
mysql
mysql-connector-java
5.1.32
junit
junit
4.12
c3p0
c3p0
0.9.1.2
com.alibaba
druid
1.0.9
```
2.建立資料庫表和實體
**使用sqlyog建立一個表
語句**
```java
CREATE TABLE test1(
id INT,
NAME VARCHAR(10)
);
```
**建立實體**
```java
package com.pjh;
public class user {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
```
**JbdcTemplate快速入門*,不使用spring框架的時候**
```java
@Test
public void test1() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
//建立jdbcTemplate物件
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
//執行語句
jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}
```
**結果**
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926134949247.png#pic_center)
**抽取配置檔案**
配置檔案程式碼:
```java
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3309/one
jdbc.name=root
jdbc.password=1234
```
**測試函式操作**
```java
@Test
public void test3() throws PropertyVetoException {
//讀取配置檔案
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
//獲取連線池
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//設定引數
comboPooledDataSource.setDriverClass(jdbc.getString("jdbc.driver"));
comboPooledDataSource.setJdbcUrl(jdbc.getString("jdbc.url"));
comboPooledDataSource.setUser(jdbc.getString("jdbc.name"));
comboPooledDataSource.setPassword(jdbc.getString("jdbc.password"));
//建立jdbcTemplate物件
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
jdbcTemplate.update("insert into test1 values(?,?)",13,"three");
}
```
**使用spring建立JdbcTemplate物件**
將資料來源DataSource與JdbcTemplate的建立權交給Spring並在Spring容器內進行依賴注入
配置程式碼:
```java
```
**測試函式**
```java
@Test
public void test2(){
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
jdbcTemplate.update("insert into test1 values(?,?)",11,"two");
}
```
**結果**
成功插入
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926140725466.png#pic_center)
**這個也可以使用讀取配置檔案的方式**
我們首先要匯入context的約束路徑與名稱空間
名稱空間: xmlns:context="http://www.springframework.org/schema/context"
約束路徑:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
**配置檔案修改**
```java
```
**測試程式碼**
```java
@Test
public void test4(){
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
jdbcTemplate.update("insert into test1 values(?,?)",100,"pjh");
}
```
**結果**
成功插入
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926141515865.png#pic_center)
**通過註解的方式來得到JdbcTemplate**
**使用框架**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test7(){
jdbcTemplate.update("insert into test1 values(?,?)",110,"GGB");
}
```
**不使用框架**
```java
public void test1() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
//建立jdbcTemplate物件
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(comboPooledDataSource);
//執行語句
jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}
```
**由二者對比即可看出框架的巨大好處,上面那麼長的程式碼現在只要幾行即可解決**
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020092614203655.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center)
## JDBCTemplate的常用操作
**查詢語句**
**查詢資料庫中的所有內容**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test8(){
String sql="select * from test1 where name=?";
List query = jdbcTemplate.query(sql, new BeanPropertyRowMapper(user.class));
for (user user : query) {
System.out.println(user);
}
}
```
**結果**
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926142625980.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center)
**查詢資料庫中的某條內容**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
@Test
public void test9(){
String sql="select * from test1 where id=?";
List query = jdbcTemplate.query(sql, new BeanPropertyRowMapper(user.class), 10);
for (user user : query) {
System.out.println(user);
}
}
}
```
**查詢資料庫記錄的數量**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test90(){
String sql="select count(*) from test1";
Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println("記錄條數:"+aLong);
}
}
```
**刪除指定記錄**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test11(){
String sql="delete from test1 where id=11";
jdbcTemplate.update(sql);
}
}
```
## 以上就是Spring jdbc操作的一些知識,我會不斷的學習,也會不斷更新我的學習文章,主要有java和資料結構兩個方面,有想要一起學習的夥伴可以私信或則關注我,共勉
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200926145910891.png#pic_