一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 超細致講解Spring框架 JdbcTemplate的使用

超細致講解Spring框架 JdbcTemplate的使用

2022-01-11 13:25Tian.X、 Java教程

在之前的Javaweb學習中,學習了手動封裝JdbcTemplate,其好處是通過(sql語句+參數(shù))模板化了編程。而真正的JdbcTemplate類,是Spring框架為我們寫好的。它是 Spring 框架中提供的一個對象,是對原始 Jdbc API 對象的簡單封裝。

JdbcTemplate基本使用

1-JdbcTemplate基本使用-概述(了解)

JdbcTemplate是spring框架中提供的一個對象,是對原始繁瑣的Jdbc API對象的簡單封裝。spring框架為我們提供
了很多的操作模板類。例如:操作關系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的
RedisTemplate,操作消息隊列的JmsTemplate等等。

2-JdbcTemplate基本使用-開發(fā)步驟(理解)

①導入spring-jdbc和spring-tx坐標
②創(chuàng)建數(shù)據(jù)庫表和實體
③創(chuàng)建JdbcTemplate對象
④執(zhí)行數(shù)據(jù)庫操作

3-JdbcTemplate基本使用-快速入門代碼實現(xiàn)(應用)

導入spring-jdbc和spring-tx坐標

?
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
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring‐context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring‐test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring‐jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring‐tx</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!‐‐織入包‐‐>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
 <!‐‐數(shù)據(jù)源相關‐‐>
<!‐‐ Druid連接池 ‐‐>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!‐‐ mysql驅動 ‐‐>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql‐connector‐java</artifactId>
<version>5.1.39</version>
</dependency>
<!‐‐servlet相關‐‐>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet‐api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp‐api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

創(chuàng)建數(shù)據(jù)庫表和實體

?
1
2
3
4
5
6
7
8
9
DROP TABLE IF EXISTS account;
CREATE TABLE account (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(50) NOT NULL,
money DOUBLE NOT NULL
);
INSERT INTO account VALUES (NULL,'旺財',1000);
INSERT INTO account VALUES (NULL,'小強',1000);
SELECT * FROM account;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.summer.domain;
public class Account {
private int id;
private String name;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}

創(chuàng)建JdbcTemplate對象
執(zhí)行數(shù)據(jù)庫操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//測試JdbcTemplate的開發(fā)步驟
@Test
public void test1(){
//創(chuàng)建數(shù)據(jù)源
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUsername("root");
dataSource.setPassword("root");
//創(chuàng)建 模板對象
JdbcTemplate template = new JdbcTemplate();
//設置數(shù)據(jù)源
template.setDataSource(dataSource);
//執(zhí)行更新操作 (添加 修改 刪除)
int i = template.update("INSERT INTO account VALUES (NULL,?,?);", "如花", 1000);
System.out.println(i);
}

4-JdbcTemplate基本使用-spring產(chǎn)生模板對象分析(理解)

我們可以將JdbcTemplate的創(chuàng)建權交給Spring,將數(shù)據(jù)源DataSource的創(chuàng)建權也交給Spring,在Spring容器內部
將數(shù)據(jù)源DataSource注入到JdbcTemplate模版對象中,然后通過Spring容器獲得JdbcTemplate對象來執(zhí)行操作

5-JdbcTemplate基本使用-spring產(chǎn)生模板對象代碼實現(xiàn)(應用)

配置如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
<!‐‐配置數(shù)據(jù)源 將數(shù)據(jù)的創(chuàng)建 交給spring容器‐‐>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!‐‐創(chuàng)建 jdbc模板對象‐‐>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!‐‐配置數(shù)據(jù)源‐‐>
<property name="dataSource" ref="dataSource" />
</bean>

測試代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//將spring和 junit整合s
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
//測試spring管理 JdbcTemplate
@Test
    public void test2(){
int i = jdbcTemplate.update("INSERT INTO account VALUES (NULL,?,?);", "秋香", 1000);
System.out.println(i);
}
}

6-JdbcTemplate基本使用-spring產(chǎn)生模板對象代碼實現(xiàn)

將數(shù)據(jù)庫的連接信息抽取到外部配置文件中,和spring的配置文件分離開,有利于后期維護

?
1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

配置文件修改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF‐8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring‐beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring‐context.xsd
">
<!‐‐加載properties配置文件‐‐>
<context:property‐placeholder location="classpath:jdbc.properties"/>
<!‐‐配置數(shù)據(jù)源 將數(shù)據(jù)的創(chuàng)建 交給spring容器‐‐>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!‐‐創(chuàng)建 jdbc模板對象‐‐>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!‐‐配置數(shù)據(jù)源‐‐>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

7-JdbcTemplate基本使用-常用操作-更新操作(應用)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//將spring和 junit整合s
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
//注入jdbc模板
@Autowired
private JdbcTemplate jdbcTemplate;
//測試修改操作
@Test
public void testUpdate(){
jdbcTemplate.update("update account set money = ? where id = ?;",800,1);
}
//測試刪除
@Test
public void testDelete(){
jdbcTemplate.update("DELETE from account where id = ?",1);
}
}

8-JdbcTemplate基本使用-常用操作-查詢操作(應用)

?
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
package com.summer.test;
import com.summer.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
//將spring和 junit整合s
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
//注入jdbc模板
@Autowired
private JdbcTemplate jdbcTemplate;
//測試修改操作
@Test
public void testUpdate(){
    jdbcTemplate.update("update account set money = ? where id = ?;",800,1);
}
//測試刪除
@Test
public void testDelete(){
jdbcTemplate.update("DELETE from account where id = ?",1);
} /
/聚合查詢
@Test
public void testQueryCount(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
} /
/查詢一個
@Test
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where name=?", new
BeanPropertyRowMapper<Account>(Account.class), "旺財");
System.out.println(account);
} /
/查詢所有
@Test
public void testQueryAll(){
List<Account> accountList = jdbcTemplate.query("select * from account", new
BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
}

9-JdbcTemplate基本使用-知識要點(理解,記憶)

①導入spring-jdbc和spring-tx坐標
②創(chuàng)建數(shù)據(jù)庫表和實體
③創(chuàng)建JdbcTemplate對象

?
1
2
JdbcTemplate jdbcTemplate = newJdbcTemplate();
jdbcTemplate.setDataSource(dataSource);

④執(zhí)行數(shù)據(jù)庫操作

更新操作:

?
1
jdbcTemplate.update (sql,params)

查詢操作:

?
1
jdbcTemplate.query (sql,Mapper,params)
?
1
jdbcTemplate.queryForObject(sql,Mapper,params)

聲明式事務控制

1.事務的概念

概念:

事務是一組操作的執(zhí)行單元,相對于數(shù)據(jù)庫操作來講,事務管理的是一組SQL指令,比如增加,修改,刪除等,事
務的一致性,要求,這個事務內的操
作必須全部執(zhí)行成功,如果在此過程種出現(xiàn)了差錯,比如有一條SQL語句沒有執(zhí)行成功,那么這一組操作都將全部
回滾。
僅用四個詞解釋事務(ACID)
1.atomic(原子性):要么都發(fā)生,要么都不發(fā)生。
2.consistent(一致性):數(shù)據(jù)應該不被破壞。
3.Isolate(隔離性):用戶間操作不相混淆
4.Durable(持久性):永久保存,例如保存到數(shù)據(jù)庫中等

2 .基于注解的聲明式事務控制

2.1 什么是聲明式事務控制

Spring 的聲明式事務顧名思義就是采用聲明的方式來處理事務。這里所說的聲明,就是指在配置文件中聲明,用在Spring 配置文件中聲明式的處理事務來代替代碼式的處理事務

聲明式事務處理的作用

  • 事務管理不侵入開發(fā)的組件。具體來說,業(yè)務邏輯對象就不會意識到正在事務管理之中,事實上也應該如此,因為事務管理是屬于系統(tǒng)層面的服務,而不是業(yè)務邏輯的一部分,如果想要改變事務管理策劃的話,也只需要在定義文件中重新配置即可
  • 在不需要事務管理的時候,只要在設定文件上修改一下,即可移去事務管理服務,無需改變代碼重新編譯,這樣維護起來極其方便

注意:Spring 聲明式事務控制底層就是AOP。

引入aop、tx命名空間

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring‐context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring‐aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring‐tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring‐beans.xsd">

2.2 使用注解配置聲明式事務控制

編寫 AccoutDao

?
1
2
3
4
5
6
7
8
9
10
11
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money‐? where name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
}
}

2.編寫 AccoutService

?
1
2
3
4
5
6
7
8
9
10
11
12
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
}

編寫 applicationContext.xml 配置文件

?
1
2
3
4
5
6
7
8
<!‐‐之前省略datsSource、jdbcTemplate‐‐>
<!‐‐平臺事務管理器‐‐>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!‐‐事務的注解驅動‐‐>
<tx:annotation‐driven/>

2.3 注解配置聲明式事務控制解析

① 使用 @Transactional 在需要進行事務控制的類或是方法上修飾,注解可用的屬性同 xml 配置方式,例如隔離級別、傳播行為等。
②注解使用在類上,那么該類下的所有方法都使用同一套注解參數(shù)配置。
③使用在方法上,不同的方法可以采用不同的事務參數(shù)配置。
④ Xml 配置文件中要開啟事務的注解驅動 <tx:annotation-driven />

2.4 知識要點

注解聲明式事務控制的配置要點

  • 事務通知的配置 (@Transactional注解配置 )
  • 事務注解驅動的配置 <tx:annotation-driven/>

到此這篇關于超細致講解Spring框架 JdbcTemplate的使用的文章就介紹到這了,更多相關Spring JdbcTemplate內容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/xt623/article/details/120396471

延伸 · 閱讀

精彩推薦
  • Java教程Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關于小米推送Java代碼,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程Java實現(xiàn)搶紅包功能

    Java實現(xiàn)搶紅包功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經(jīng)有好久沒有升過級了。升級完畢重啟之后,突然發(fā)現(xiàn)好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發(fā)項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關于Java8中S...

    阿杜7482021-02-04
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程xml與Java對象的轉換詳解

    xml與Java對象的轉換詳解

    這篇文章主要介紹了xml與Java對象的轉換詳解的相關資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
主站蜘蛛池模板: 四虎免费看| 亚洲一级片在线播放 | 国产激情久久久久影院小草 | 欧洲肥女大肥臀 | 国产香蕉97碰碰在线视频 | 日本草草视频 | 三级aa久久 | 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 | 黄色a| 日韩xx00 | 网址在线观看你懂我意思吧免费的 | 毛片在线免费视频 | 国产91无毒不卡在线观看 | 四虎影院免费视频 | 无人视频在线观看完整版高清 | 欧美色成人tv在线播放 | 国产区香蕉精品系列在线观看不卡 | 日韩精品免费一区二区三区 | 糖心在线观看网 | 99在线视频精品费观看视 | 日本zzzzwww大片免费 | 国产欧美日韩综合二区三区 | 欧美男男xxx激情做受 | 成人免费体验区福利云点播 | 黄绝一级 | 女bbwxxxx非洲黑人 | 国产在线视频欧美亚综合 | 性做久久久久久 | 韩国一区二区三区 | 9自拍视频在线观看 | 久久国内精品 | 桃乃木香奈ipx在线播放 | 天天综合天天综合色在线 | 日本草草视频 | 国产91精品在线播放 | 亚洲九九精品 | www.爱操 | 白发在线视频播放观看免费 | 胸大的姑娘中文字幕视频 | 久久黄色精品视频 | 欧美精品综合一区二区三区 |