Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解
簡(jiǎn)介
Spring框架對(duì)JDBC進(jìn)行封裝,使用JdbcTemplate方便實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)操作
JdbcTemplate 目的是使JDBC更加易于使用,JdbcTemplate是Spring的一部分。JdbcTemplate 處理了資源的建立和釋放,它幫助我們避免一些常見(jiàn)的錯(cuò)誤,比如忘了總要關(guān)閉連接。他運(yùn)行核心的JDBC工作流,如Statement的建立和執(zhí)行,而我們只需要提供SQL語(yǔ)句和提取結(jié)果即可。
準(zhǔn)備工作
①創(chuàng)建一個(gè)Maven工程
②添加相關(guān)依賴
<dependencies>
<!-- 基于Maven依賴傳遞性,導(dǎo)入spring-context依賴即可導(dǎo)入當(dāng)前所需所有jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!-- Spring 持久化層支持jar包 -->
<!-- Spring 在執(zhí)行持久化層操作、與持久化層技術(shù)進(jìn)行整合過(guò)程中,需要使用orm、jdbc、tx三個(gè)
jar包 -->
<!-- 導(dǎo)入 orm 包就可以通過(guò) Maven 的依賴傳遞性把其他兩個(gè)也導(dǎo)入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.23</version>
</dependency>
<!-- Spring 測(cè)試相關(guān) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.23</version>
</dependency>
<!-- junit測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
</dependencies>③創(chuàng)建Spring配置文件Spring-jdbc
<!--引入jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>④創(chuàng)建數(shù)據(jù)庫(kù)表


⑤創(chuàng)建測(cè)試類(lèi)
//指定當(dāng)前測(cè)試類(lèi)在Spring的測(cè)試環(huán)境中執(zhí)行,此時(shí)就可以通過(guò)注入的方式直接獲取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
//設(shè)置Spring測(cè)試環(huán)境的配置文件
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testInsert() {
String sql = "insert into t_user values(null,?,?,?,?,?)";
jdbcTemplate.update(sql,"root","123",23,"女","123@qq.com");
}
}然后進(jìn)行添加測(cè)試即可
JdbcTemplate實(shí)現(xiàn)查詢功能
①創(chuàng)建實(shí)體類(lèi)
②測(cè)試類(lèi),查詢一條數(shù)據(jù)
@Test
public void testGetUserById() {
String sql = "select * from t_user where id = ?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);
System.out.println(user);
}測(cè)試類(lèi),查詢多條數(shù)據(jù)
@Test
public void testGetAllUser() {
String sql = "select * from t_user";
List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
list.forEach(System.out::println);
}測(cè)試類(lèi),查詢單條數(shù)據(jù)
@Test
public void testGetCount() {
String sql = "select count(*) from t_user";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(count);
}
到此這篇關(guān)于Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解的文章就介紹到這了,更多相關(guān)Spring JdbcTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot用JdbcTemplates操作Mysql實(shí)例代碼詳解
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫(kù)操作詳解
- SpringBoot整合JdbcTemplate的示例代碼
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- Spring Boot 整合持久層之JdbcTemplate
- Spring操作JdbcTemplate數(shù)據(jù)庫(kù)的方法學(xué)習(xí)
- Spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理完全注解方式
相關(guān)文章
Java面向?qū)ο箢?lèi)和對(duì)象實(shí)例詳解
面向?qū)ο竽耸荍ava語(yǔ)言的核心,是程序設(shè)計(jì)的思想,這篇文章主要介紹了Java面向?qū)ο箢?lèi)和對(duì)象的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
詳解Java繼承中屬性、方法和對(duì)象的關(guān)系
這篇文章主要幫助大家詳細(xì)介紹了Java繼承中屬性、方法和對(duì)象的關(guān)系,感興趣的朋友可以參考一下2016-03-03
springboot配置mybatis和事務(wù)管理方式
這篇文章主要介紹了springboot配置mybatis和事務(wù)管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringCloud Alibaba微服務(wù)實(shí)戰(zhàn)之遠(yuǎn)程Feign請(qǐng)求頭丟失問(wèn)題解決方案
這篇文章主要介紹了SpringCloud Alibaba微服務(wù)實(shí)戰(zhàn)之遠(yuǎn)程Feign請(qǐng)求頭丟失問(wèn)題,對(duì)SpringCloud Alibaba Feign請(qǐng)求頭問(wèn)題感興趣的朋友跟隨小編一起看看吧2024-02-02
Java實(shí)現(xiàn)post請(qǐng)求詳細(xì)代碼(帶有參數(shù))
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)帶有參數(shù)post請(qǐng)求的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08
Java如何優(yōu)雅地關(guān)閉資源try-with-resource及其異常抑制
這篇文章主要介紹了Java如何優(yōu)雅地關(guān)閉資源try-with-resource及其異常抑制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
IDEA+Maven搭建JavaWeb項(xiàng)目的方法步驟
本文主要介紹了IDEA+Maven搭建JavaWeb項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

