Spring Boot對接Oracle數(shù)據(jù)庫
最近學習了Oracle數(shù)據(jù)庫,那么如何使用Spring Boot和MyBatis Plus對接Oracle數(shù)據(jù)庫呢?
這就有了這篇隨記,具體流程如下
1、創(chuàng)建Maven工程
創(chuàng)建一個空的Maven工程,導入如下依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>oracle-init</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<!-- Oracel11g與ojdbc5/ojdbc6版本匹配 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.oracle.database.jdbc</groupId>-->
<!-- <artifactId>ojdbc8</artifactId>-->
<!-- <version>19.8.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
tips:這里碰到一個坑,我本機適用的Oracle數(shù)據(jù)庫版本是11g XE,所以要使用的驅動為ojdbc5/ojdbc6,不然連接老會失敗。
2、application.yml
在配置文件中填寫數(shù)據(jù)庫連接的參數(shù)
spring:
datasource:
username: pp
password: 123456
url: jdbc:oracle:thin:@localhost:1521:XE
driver-class-name: oracle.jdbc.OracleDriver
參數(shù)說明:
- username:數(shù)據(jù)庫連接用戶名
- password:數(shù)據(jù)庫連接密碼
- url:連接地址
- driver-class-name:數(shù)據(jù)庫驅動
3、創(chuàng)建實例數(shù)據(jù)表
為了演示本次對接Oracle數(shù)據(jù)庫,我們需要一張測試數(shù)據(jù)表
3.1創(chuàng)建Users數(shù)據(jù)表,SQL如下:
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
什么?沒有測試數(shù)據(jù)?。?!Orz…
這里可以使用Oracle數(shù)據(jù)庫的PL/SQL編程來批量生成測試數(shù)據(jù),真香?。?/p>
3.2PL/SQL批量生產測試數(shù)據(jù)
sql如下:
DECLARE
TYPE user_type IS RECORD (
id NUMBER,
username VARCHAR2(50),
email VARCHAR2(100),
password VARCHAR2(100),
created_at DATE
);
TYPE user_list IS TABLE OF user_type;
l_users user_list := user_list();
BEGIN
FOR i IN 1..100 LOOP
l_users.extend;
l_users(i).id := i;
l_users(i).username := 'user' || i;
l_users(i).email := 'user' || i || '@example.com';
l_users(i).password := 'password' || i;
l_users(i).created_at := SYSDATE; -- 使用當前時間作為創(chuàng)建時間
END LOOP;
FORALL i IN 1..l_users.COUNT
INSERT INTO users (id, username, email, password, created_at)
VALUES (l_users(i).id, l_users(i).username, l_users(i).email, l_users(i).password, l_users(i).created_at);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Data inserted successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
ROLLBACK;
END;
執(zhí)行之后查看數(shù)據(jù)表中的數(shù)據(jù):
OK!準備工作完成,接下來就可以進行對接了。
4、使用MP對Oracle進行CRUD
4.1創(chuàng)建實體類
User:文章來源:http://www.zghlxwxcb.cn/news/detail-700450.html
/**
* TODO User實體類
* @version 1.0
* @author ss_419
* @date 2023/8/11 14:49
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
// 指定Oracle數(shù)據(jù)庫中的表名
@TableName("users")
public class User {
private Long id;
private String username;
private String password;
private String email;
private Date created_at;
}
4.2、創(chuàng)建Mapper
/**
* @author ss_419
*/
@Mapper
public interface UserRepository extends BaseMapper<User> {
// 這里可以自定義一些數(shù)據(jù)庫操作方法
}
4.3、創(chuàng)建Service
/**
* @author ss_419
*/
@Service
public class UserService
extends ServiceImpl<UserRepository, User> {
// 這里可以編寫一些業(yè)務邏輯方法
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAllUsers() {
List<User> users = userRepository.selectList(null);
return users;
}
public User getUserById(Long id) {
return userRepository.selectById(id);
}
public void saveUser(User user) {
userRepository.insert(user);
}
public void updateUser(User user) {
userRepository.updateById(user);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
4.4、創(chuàng)建啟動器
@SpringBootApplication
// Mapper包掃描
@MapperScan("org.example.mapper")
public class OracleDBApplication {
public static void main(String[] args) {
SpringApplication.run(OracleDBApplication.class,args);
}
}
5、測試
萬事俱備,只欠東風,對CRUD進行測試。文章來源地址http://www.zghlxwxcb.cn/news/detail-700450.html
@SpringBootTest
public class OraCTest {
@Autowired
private UserService service;
/**
* 獲取所有用戶信息
*/
@Test
public void testGetAllUsers() {
List<User> users = service.getAllUsers();
users.forEach(System.out::println);
}
/**
* 根據(jù)id獲取用戶
*/
@Test
public void testGetUserById() {
User userById = service.getUserById(1L);
System.out.println("userById = " + userById);
}
/**
* 保存用戶
*/
@Test
public void testSaveUser() {
User user = new User();
user.setId(1000L);
user.setUsername("測試新增User");
user.setPassword("00101010");
user.setEmail("test@example.com");
user.setCreated_at(new Date());
service.saveUser(user);
}
/**
* 更新用戶
*/
@Test
public void testUpdateUser() {
// 先查詢
User user = service.getUserById(1000L);
System.out.println("userById = " + user);
// 后更新
user.setUsername("update_username");
service.updateUser(user);
}
/**
* 根據(jù)id刪除用戶
*/
@Test
public void testDeleteUser() {
service.deleteUser(1000L);
}
}
到了這里,關于Spring Boot對接Oracle數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!