Spring JdbcTemplate實(shí)現(xiàn)添加與查詢(xú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)依賴(lài)
<dependencies> <!-- 基于Maven依賴(lài)傳遞性,導(dǎo)入spring-context依賴(lài)即可導(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 的依賴(lài)傳遞性把其他兩個(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)查詢(xún)功能
①創(chuàng)建實(shí)體類(lèi)
②測(cè)試類(lèi),查詢(xún)一條數(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),查詢(xún)多條數(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),查詢(xún)單條數(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)添加與查詢(xú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)文章
WebFlux 服務(wù)編排使用優(yōu)勢(shì)詳解
這篇文章主要為大家介紹了WebFlux 服務(wù)編排使用優(yōu)勢(shì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05java?spring?validation?自動(dòng)、手動(dòng)校驗(yàn)
HibernateValidator簡(jiǎn)化了Java開(kāi)發(fā)中的參數(shù)校驗(yàn)過(guò)程,提供自動(dòng)和手動(dòng)兩種校驗(yàn)方式,通過(guò)引入相關(guān)依賴(lài)并使用@Validated注解,可以實(shí)現(xiàn)自動(dòng)校驗(yàn),手動(dòng)校驗(yàn)則需要使用ValidatorUtils類(lèi),此方法有效減少代碼重復(fù),提高開(kāi)發(fā)效率2024-09-09SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson)
有時(shí)候,我們接口返回的數(shù)據(jù)需要做一些處理,有一些敏感數(shù)據(jù),本文主要介紹了SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07關(guān)于如何正確地定義Java內(nèi)部類(lèi)方法詳解
在Java中,我們通常是把不同的類(lèi)創(chuàng)建在不同的包里面,對(duì)于同一個(gè)包里的類(lèi)來(lái)說(shuō),它們都是同一層次的,但其實(shí)還有另一種情況,有些類(lèi)可以被定義在另一個(gè)類(lèi)的內(nèi)部,本文將詳細(xì)帶你了解如何正確地定義Java內(nèi)部類(lèi),需要的朋友可以參考下2023-05-05java識(shí)別一篇文章中某單詞出現(xiàn)個(gè)數(shù)的方法
這篇文章主要介紹了java識(shí)別一篇文章中某單詞出現(xiàn)個(gè)數(shù)的方法,涉及java字符解析操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10JAVA實(shí)現(xiàn)對(duì)阿里云DNS的解析管理
本文主要介紹了JAVA實(shí)現(xiàn)對(duì)阿里云DNS的解析管理,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01java使用randomaccessfile在文件任意位置寫(xiě)入數(shù)據(jù)
Java在文件任意位置寫(xiě)入數(shù)據(jù)可以使用RandomAccessFile方法來(lái)完成,下面看一個(gè)簡(jiǎn)單的示例就明白了2014-01-01IDEA中Maven依賴(lài)包無(wú)法下載或?qū)氲慕鉀Q方案(系統(tǒng)缺失文件導(dǎo)致)
在配置Maven環(huán)境時(shí),可能會(huì)遇到各種報(bào)錯(cuò)問(wèn)題,首先確保Maven路徑配置正確,例如使用apache-maven-3.5.0版本,則需要在系統(tǒng)環(huán)境變量的Path中添加其bin目錄路徑,并上移優(yōu)先級(jí),接下來(lái),在Maven的conf目錄下修改settings.xml文件,將鏡像源改為阿里云2024-09-09springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01