Spring?JDBC配置與使用的實(shí)現(xiàn)
Spring JDBC 框架負(fù)責(zé)所有的低層細(xì)節(jié),從開始打開連接,準(zhǔn)備和執(zhí)行 SQL 語(yǔ)句,處理異常,處理事務(wù),到最后關(guān)閉連接。大大簡(jiǎn)化了開發(fā)人員對(duì)數(shù)據(jù)庫(kù)的操作,使得開發(fā)人員可以從繁瑣的數(shù)據(jù)庫(kù)操作中解脫出來,從而將更多的精力投入到編寫業(yè)務(wù)邏輯中。
Spring框架提供了JdbcTemplate類,該類是Spring框架數(shù)據(jù)抽象層的基礎(chǔ)這是管理所有數(shù)據(jù)庫(kù)通信和異常處理的中央框架類。
JdbcTemplate
是spring框架中提供的一個(gè)對(duì)象,是對(duì)原始繁瑣的Jdbc API對(duì)象的簡(jiǎn)單封裝。spring框架為我們提供了很多的操作模板類。
JdbcTemplate雖然簡(jiǎn)單,功能卻非常強(qiáng)大。它提供了非常豐富、實(shí)用的方法,歸納起來主要有以下幾種類型的方法:
- execute方法:可以用于執(zhí)行任何SQL語(yǔ)句,一般用于執(zhí)行DDL語(yǔ)句。
- update、batchUpdate方法:用于執(zhí)行新增、修改與刪除等語(yǔ)句。
- query和queryForXXX方法:用于執(zhí)行查詢相關(guān)的語(yǔ)句。
- call方法:用于執(zhí)行數(shù)據(jù)庫(kù)存儲(chǔ)過程和函數(shù)相關(guān)的語(yǔ)句。
總的來說,新增、刪除與修改三種類型的操作主要使用update和batchUpdate方法來完成query和queryForObject方法中主要用來完成查詢功能。execute方法可以用來執(zhí)行任意的SQL、call方法來調(diào)用存儲(chǔ)過程,
Spring JDBC 示例
一、數(shù)據(jù)庫(kù)表
create database mybatis_demo; use mybatis_demo; CREATE TABLE `user` ( `id` int(11) NOT NULL auto_increment, `username` varchar(32) NOT NULL COMMENT '用戶名稱', `birthday` datetime default NULL COMMENT '生日', `sex` char(1) default NULL COMMENT '性別', `address` varchar(256) default NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (1,'老王','2018-02-27 17:47:08','男','北京'),(2,'熊大','2018-03-02 15:09:37','女','上海'),(3,'熊二','2018-03-04 11:34:34','女','深圳'),(4,'光頭強(qiáng)','2018-03-04 12:04:06','男','廣州');
二、添加依賴
在pom.xml中加入Spring 和mysql相關(guān)的包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
三、創(chuàng)建實(shí)體類
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
//get set方法
}四、在Spring.xml當(dāng)中配置數(shù)據(jù)庫(kù)的鏈接和idbcTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置連接DriverManagerDataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo"/>
<property name="username" value="root"/>
<property name="password" value="2020"/>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
五、創(chuàng)建類實(shí)現(xiàn)增刪改查操作
import com.qcby.entity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class SpringTest {
ApplicationContext ctx=new ClassPathXmlApplicationContext("Application.xml");;
JdbcTemplate jdbcTemplate= (JdbcTemplate) ctx.getBean("jdbcTemplate");
@Test
public void testInsert(){
String sql="insert into user(username,address) values('李連杰','上海')";
jdbcTemplate.execute(sql);
}
@Test
public void testUpdate(){
String sql="update user set username='穩(wěn)杰',address='南海' where id=?";
int res=jdbcTemplate.update(sql,2);
System.out.println(res);
}
@Test
public void testDelete(){
String sql="delete from user where id=?";
int res=jdbcTemplate.update(sql,18);
System.out.println(res);
}
//查詢列表
@Test
public void testQueryList(){
String sql = "select * from user where address like '%京%'";
List<User> userList= (List<User>) jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
System.out.println("查詢List: ");
for (User user : userList) {
System.out.println(user);
}
System.out.println("數(shù)量: "+userList.size());
}
}結(jié)果:

到此這篇關(guān)于Spring JDBC配置與使用的文章就介紹到這了,更多相關(guān)Spring JDBC配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring boot使用sharding jdbc的配置方式
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)
- SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
相關(guān)文章
Java運(yùn)用設(shè)計(jì)模式中的建造者模式構(gòu)建項(xiàng)目的實(shí)例解析
這篇文章主要介紹了Java運(yùn)用設(shè)計(jì)模式中的建造者模式構(gòu)建項(xiàng)目的實(shí)例解析,建造者模式對(duì)外隱藏創(chuàng)建過程的產(chǎn)品,使用組合的方式,由指揮者來決定建造的流程,需要的朋友可以參考下2016-04-04
windows啟動(dòng)jar的三種方式小結(jié)
JAR(Java Archive)文件是Java程序的封裝格式,使得多個(gè).class文件及其相關(guān)資源可以被打包成一個(gè)文件,方便發(fā)布和分發(fā),這篇文章主要介紹了windows啟動(dòng)jar的三種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-09-09
Java實(shí)現(xiàn)SM3加密和驗(yàn)證的示例代碼
在商用密碼體系中,SM3主要用于數(shù)字簽名及驗(yàn)證、消息認(rèn)證碼生成及驗(yàn)證、隨機(jī)數(shù)生成等,其算法公開,本文給大家詳細(xì)介紹了使用Java實(shí)現(xiàn)SM3加密和驗(yàn)證,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12
Java Socket編程實(shí)現(xiàn)簡(jiǎn)單的問候服務(wù)
這篇文章主要為大家介紹了Java Socket編程實(shí)現(xiàn)簡(jiǎn)單的問候服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Java 基礎(chǔ)全面講解StringBuffer類的使用
當(dāng)對(duì)字符串進(jìn)行修改的時(shí)候,需要使用 StringBuffer 和 StringBuilder類,和String類不同的是,StringBuffer和 StringBuilder類的對(duì)象能夠被多次的修改,并且不產(chǎn)生新的未使用對(duì)象2022-01-01
JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法
本篇文章主要介紹了JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Java Socket實(shí)現(xiàn)UDP編程淺析
類 DatagramSocket 何 DatagramPacket(數(shù)據(jù)包/數(shù)據(jù)報(bào)) 實(shí)現(xiàn)了基于 UDP協(xié)議網(wǎng)絡(luò)程序;UDP數(shù)據(jù)報(bào)通過數(shù)據(jù)報(bào)套接字 DatagramSocket 發(fā)送和接收,系統(tǒng)不保證 UDP數(shù)據(jù)報(bào)一定能夠安全送達(dá)目的地,也不確定什么時(shí)候可以抵達(dá)2022-11-11
解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題
這篇文章主要介紹了解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

