Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解
簡介
Spring框架對JDBC進(jìn)行封裝,使用JdbcTemplate方便實(shí)現(xiàn)對數(shù)據(jù)庫操作
JdbcTemplate 目的是使JDBC更加易于使用,JdbcTemplate是Spring的一部分。JdbcTemplate 處理了資源的建立和釋放,它幫助我們避免一些常見的錯誤,比如忘了總要關(guān)閉連接。他運(yùn)行核心的JDBC工作流,如Statement的建立和執(zhí)行,而我們只需要提供SQL語句和提取結(jié)果即可。
準(zhǔn)備工作
①創(chuàng)建一個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)行整合過程中,需要使用orm、jdbc、tx三個 jar包 --> <!-- 導(dǎo)入 orm 包就可以通過 Maven 的依賴傳遞性把其他兩個也導(dǎo)入 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.23</version> </dependency> <!-- Spring 測試相關(guān) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.23</version> </dependency> <!-- junit測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- MySQL驅(qū)動 --> <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ù)庫表
⑤創(chuàng)建測試類
//指定當(dāng)前測試類在Spring的測試環(huán)境中執(zhí)行,此時就可以通過注入的方式直接獲取IOC容器中的bean @RunWith(SpringJUnit4ClassRunner.class) //設(shè)置Spring測試環(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)行添加測試即可
JdbcTemplate實(shí)現(xiàn)查詢功能
①創(chuàng)建實(shí)體類
②測試類,查詢一條數(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); }
測試類,查詢多條數(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); }
測試類,查詢單條數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot用JdbcTemplates操作Mysql實(shí)例代碼詳解
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解
- SpringBoot整合JdbcTemplate的示例代碼
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- Spring Boot 整合持久層之JdbcTemplate
- Spring操作JdbcTemplate數(shù)據(jù)庫的方法學(xué)習(xí)
- Spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
相關(guān)文章
java?spring?validation?自動、手動校驗(yàn)
HibernateValidator簡化了Java開發(fā)中的參數(shù)校驗(yàn)過程,提供自動和手動兩種校驗(yàn)方式,通過引入相關(guān)依賴并使用@Validated注解,可以實(shí)現(xiàn)自動校驗(yàn),手動校驗(yàn)則需要使用ValidatorUtils類,此方法有效減少代碼重復(fù),提高開發(fā)效率2024-09-09SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson)
有時候,我們接口返回的數(shù)據(jù)需要做一些處理,有一些敏感數(shù)據(jù),本文主要介紹了SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson),具有一定的參考價值,感興趣的可以了解一下2024-07-07關(guān)于如何正確地定義Java內(nèi)部類方法詳解
在Java中,我們通常是把不同的類創(chuàng)建在不同的包里面,對于同一個包里的類來說,它們都是同一層次的,但其實(shí)還有另一種情況,有些類可以被定義在另一個類的內(nèi)部,本文將詳細(xì)帶你了解如何正確地定義Java內(nèi)部類,需要的朋友可以參考下2023-05-05java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法
這篇文章主要介紹了java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法,涉及java字符解析操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10java使用randomaccessfile在文件任意位置寫入數(shù)據(jù)
Java在文件任意位置寫入數(shù)據(jù)可以使用RandomAccessFile方法來完成,下面看一個簡單的示例就明白了2014-01-01IDEA中Maven依賴包無法下載或?qū)氲慕鉀Q方案(系統(tǒng)缺失文件導(dǎo)致)
在配置Maven環(huán)境時,可能會遇到各種報(bào)錯問題,首先確保Maven路徑配置正確,例如使用apache-maven-3.5.0版本,則需要在系統(tǒng)環(huán)境變量的Path中添加其bin目錄路徑,并上移優(yōu)先級,接下來,在Maven的conf目錄下修改settings.xml文件,將鏡像源改為阿里云2024-09-09springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說明
這篇文章主要介紹了springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01