SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
Oracle相信大家都不陌生吧,一個(gè)大型的數(shù)據(jù)庫(kù),至于數(shù)據(jù)庫(kù),我相信各位都比較熟悉了,一個(gè)軟件系統(tǒng),不論是我們常做的App、小程序、還是傳統(tǒng)的web站點(diǎn),我們都有用戶的信息,相關(guān)業(yè)務(wù)的數(shù)據(jù),通常都會(huì)存儲(chǔ)在相關(guān)數(shù)據(jù)庫(kù)中,比如:MySQL,Oracle,SQL server 等等。
在你看到這篇文章的時(shí)候,我相信你對(duì)Spring Boot已經(jīng)有足夠的了解了,我在這篇文檔中將會(huì)采用Oracle數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)存儲(chǔ),PS:Oracle數(shù)據(jù)庫(kù)和MySQL有語(yǔ)法差別,雖然總體是一樣的,但是還是存在不一樣的語(yǔ)法。
接下來(lái)我們正式開始:
JdbcTemplate
JdbcTemplate是Spring框架中的一個(gè)核心類,用于簡(jiǎn)化Java應(yīng)用程序與關(guān)系型數(shù)據(jù)庫(kù)的交互操作。它提供了一種簡(jiǎn)單而靈活的方式來(lái)執(zhí)行SQL查詢、更新和存儲(chǔ)過(guò)程調(diào)用等數(shù)據(jù)庫(kù)操作。
JdbcTemplate封裝了一些常見的數(shù)據(jù)庫(kù)操作,如查詢單行或多行數(shù)據(jù)、插入、更新和刪除數(shù)據(jù)等。它通過(guò)使用JDBC(Java Database Connectivity)來(lái)與數(shù)據(jù)庫(kù)進(jìn)行通信,并提供了一些方便的方法來(lái)處理結(jié)果集、處理異常以及執(zhí)行事務(wù)操作。
使用JdbcTemplate可以減少編寫重復(fù)的JDBC代碼的工作量,提高開發(fā)效率。它還提供了一些高級(jí)功能,如命名參數(shù)、批處理操作和查詢結(jié)果的映射等,使得數(shù)據(jù)庫(kù)操作更加方便和易于維護(hù)。
數(shù)據(jù)源配置
在此,我說(shuō)明一下,由于今年我寫這篇文章的時(shí)候,發(fā)現(xiàn)Oracle11早已停止更新,Oracle 21的又是官網(wǎng)比較新的,故而我采用了Oracle 19c,如果你自己用的Oracle還是比較舊的版本, 請(qǐng)改為你所對(duì)應(yīng)的版本,不同的版本,會(huì)有相應(yīng)的差別:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--導(dǎo)入jdbc依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.oracle.ojdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
接下來(lái),在我們Spring Boot創(chuàng)建的src/main/resources/application.properties
添加數(shù)據(jù)源信息。
# Mysql的配置 #spring.datasource.url=jdbc:mysql://localhost:3306/test #spring.datasource.username=root #spring.datasource.password=123456 #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Oracle的配置 spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
PS: 在Spring Boot 2.x中默認(rèn)采用MySQL 8的驅(qū)動(dòng),故而上邊加了cj
使用JdbcTemplate操作數(shù)據(jù)庫(kù)
Spring 的JdbcTemplate是自動(dòng)配置的,我們可以使用@Autowried
或者構(gòu)造函數(shù),或者set方法來(lái)注入到我們想要的bean中使用。
我們?cè)贠racle 中創(chuàng)建的數(shù)據(jù)庫(kù)實(shí)際上就是我們創(chuàng)建的相關(guān)用戶,我們利用我們創(chuàng)建的用戶的賬號(hào)和密碼進(jìn)行登錄,然后再用戶下創(chuàng)建表的。
以下是我創(chuàng)建了root用戶后,然后利用root用戶的賬號(hào)密碼進(jìn)行登錄創(chuàng)建的表的SQL語(yǔ)句
CREATE TABLE useradd ( name varchar2 (100) NOT NULL, age integer NOT NULL )
接下來(lái),我們根據(jù)數(shù)據(jù)庫(kù)中創(chuàng)建的表然后創(chuàng)建實(shí)體對(duì)象:
public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age) { this.name = name; this.age = age; } public User() { } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(name, user.name) && Objects.equals(age, user.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
創(chuàng)建相關(guān)接口
public interface UserService { /** * 新增一個(gè)用戶 * * @param name * @param age */ int create(String name, Integer age); /** * 根據(jù)name查詢用戶 * * @param name * @return */ List<User> getByName(String name); /** * 根據(jù)name刪除用戶 * * @param name */ int deleteByName(String name); /** * 獲取用戶總量 */ int getAllUsers(); /** * 刪除所有用戶 */ int deleteAllUsers(); }
之后,我們通過(guò)jdbcTemplate實(shí)現(xiàn)接口中的數(shù)據(jù)訪問(wèn)操作:
@Service public class UserServiceImpl implements UserService { private JdbcTemplate jdbcTemplate; UserServiceImpl(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public int create(String name, Integer age) { return jdbcTemplate.update("insert into USERADD(NAME, AGE) values(?, ?)", name, age); } @Override public List<User> getByName(String name) { List<User> users = jdbcTemplate.query("select NAME, AGE from USERADD where NAME = ?", (resultSet, i) -> { User user = new User(); user.setName(resultSet.getString("NAME")); user.setAge(resultSet.getInt("AGE")); return user; }, name); return users; } @Override public int deleteByName(String name) { return jdbcTemplate.update("delete from USERADD where NAME = ?", name); } @Override public int getAllUsers() { return jdbcTemplate.queryForObject("select count(1) from USERADD", Integer.class); } @Override public int deleteAllUsers() { return jdbcTemplate.update("delete from USERADD"); } }
然后我們采用Spring Boot的單元測(cè)試創(chuàng)建一個(gè)測(cè)試用例,通過(guò)創(chuàng)建,刪除,以及查詢來(lái)看我們是否正確的對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作。
@RunWith(SpringRunner.class) @SpringBootTest public class Chapter31ApplicationTests { @Autowired private UserService userSerivce; @Before public void setUp() { // 準(zhǔn)備,清空user表 userSerivce.deleteAllUsers(); } @Test public void test() throws Exception { // 插入5個(gè)用戶 userSerivce.create("miaow", 10); userSerivce.create("jjkeo", 11); userSerivce.create("cfase", 30); userSerivce.create("okeda", 21); userSerivce.create("joke", 17); // 查詢名為Oscar的用戶,判斷年齡是否匹配 List<User> userList = userSerivce.getByName("joke"); Assert.assertEquals(17, userList.get(0).getAge().intValue()); // 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶 Assert.assertEquals(5, userSerivce.getAllUsers()); // 刪除兩個(gè)用戶 userSerivce.deleteByName("jjkeo"); userSerivce.deleteByName("cfase"); // 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶 Assert.assertEquals(3, userSerivce.getAllUsers()); } }
我們發(fā)現(xiàn)成功了,通過(guò)上面這個(gè)簡(jiǎn)單的例子,我們可以看到在Spring Boot下訪問(wèn)數(shù)據(jù)庫(kù)的配置依然秉承了框架的初衷:簡(jiǎn)單。
我們只需要在pom.xml中加入數(shù)據(jù)庫(kù)依賴,再到application.properties
中配置連接信息,不需要像Spring應(yīng)用中創(chuàng)建JdbcTemplate的Bean,就可以直接在自己的對(duì)象中注入使用。
到此這篇關(guān)于SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解的文章就介紹到這了,更多相關(guān)SpringBoot JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
- SpringBoot訪問(wèn)MongoDB數(shù)據(jù)庫(kù)的兩種方式
- SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)
- SpringBoot使用JdbcTemplate訪問(wèn)操作數(shù)據(jù)庫(kù)基本用法
- SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問(wèn)功能
- SpringBoot如何訪問(wèn)不同的數(shù)據(jù)庫(kù)的方法實(shí)現(xiàn)
相關(guān)文章
SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能
本文主要介紹了Spring Boot 集成ElasticSearch實(shí)現(xiàn)搜索功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03詳解spring cloud config實(shí)現(xiàn)datasource的熱部署
這篇文章主要介紹了詳解spring cloud config實(shí)現(xiàn)datasource的熱部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01解決Springboot-application.properties中文亂碼問(wèn)題
這篇文章主要介紹了解決Springboot-application.properties中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis3中方法返回生成的主鍵:XML,@SelectKey,@Options詳解
這篇文章主要介紹了Mybatis3中方法返回生成的主鍵:XML,@SelectKey,@Options,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01RocketMQ中消費(fèi)者的消費(fèi)進(jìn)度管理
這篇文章主要介紹了RocketMQ中消費(fèi)者的消費(fèi)進(jìn)度管理,業(yè)務(wù)實(shí)現(xiàn)消費(fèi)回調(diào)的時(shí)候,當(dāng)且僅當(dāng)此回調(diào)函數(shù)返回ConsumeConcurrentlyStatus.CONSUME_SUCCESS ,RocketMQ才會(huì)認(rèn)為這批消息(默認(rèn)是1條)是消費(fèi)完成的,需要的朋友可以參考下2023-10-10Spring中@PathVariable注解的簡(jiǎn)單使用
這篇文章主要介紹了Spring中@PathVariable注解的簡(jiǎn)單使用,@PathVariable 是 Spring Framework 中的注解之一,用于處理 RESTful Web 服務(wù)中的 URL 路徑參數(shù),它的作用是將 URL 中的路徑變量綁定到方法的參數(shù)上,需要的朋友可以參考下2024-01-01