欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot對(duì)接Oracle數(shù)據(jù)庫(kù)具體流程

 更新時(shí)間:2023年11月09日 11:24:06   作者:七七r  
這篇文章主要給大家介紹了關(guān)于Spring?Boot對(duì)接Oracle數(shù)據(jù)庫(kù)的具體流程,本文將介紹如何在Spring Boot中連接Oracle數(shù)據(jù)庫(kù)的基本配置,包括添加依賴(lài)、配置數(shù)據(jù)源、配置JPA等,需要的朋友可以參考下

Spring Boot對(duì)接Oracle數(shù)據(jù)庫(kù)

最近學(xué)習(xí)了Oracle數(shù)據(jù)庫(kù),那么如何使用Spring Boot和MyBatis Plus對(duì)接Oracle數(shù)據(jù)庫(kù)呢?

這就有了這篇隨記,具體流程如下

1、創(chuàng)建Maven工程

創(chuàng)建一個(gè)空的Maven工程,導(dǎo)入如下依賴(lài):

<?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:這里碰到一個(gè)坑,我本機(jī)適用的Oracle數(shù)據(jù)庫(kù)版本是11g XE,所以要使用的驅(qū)動(dòng)為ojdbc5/ojdbc6,不然連接老會(huì)失敗。

2、application.yml

在配置文件中填寫(xiě)數(shù)據(jù)庫(kù)連接的參數(shù)

spring:
  datasource:
      username: pp
      password: 123456
      url: jdbc:oracle:thin:@localhost:1521:XE
      driver-class-name: oracle.jdbc.OracleDriver

參數(shù)說(shuō)明:

  • username:數(shù)據(jù)庫(kù)連接用戶(hù)名
  • password:數(shù)據(jù)庫(kù)連接密碼
  • url:連接地址
  • driver-class-name:數(shù)據(jù)庫(kù)驅(qū)動(dòng)

3、創(chuàng)建實(shí)例數(shù)據(jù)表

為了演示本次對(duì)接Oracle數(shù)據(jù)庫(kù),我們需要一張測(cè)試數(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
);

什么?沒(méi)有測(cè)試數(shù)據(jù)?。?!Orz…

這里可以使用Oracle數(shù)據(jù)庫(kù)的PL/SQL編程來(lái)批量生成測(cè)試數(shù)據(jù),真香??!

3.2PL/SQL批量生產(chǎn)測(cè)試數(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; -- 使用當(dāng)前時(shí)間作為創(chuàng)建時(shí)間
  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!準(zhǔn)備工作完成,接下來(lái)就可以進(jìn)行對(duì)接了。

4、使用MP對(duì)Oracle進(jìn)行CRUD

4.1創(chuàng)建實(shí)體類(lèi)

User:

/**
 * TODO User實(shí)體類(lèi)
 * @version 1.0
 * @author ss_419
 * @date 2023/8/11 14:49
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
// 指定Oracle數(shù)據(jù)庫(kù)中的表名
@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ù)庫(kù)操作方法
}

4.3、創(chuàng)建Service

/**
 * @author ss_419
 */
@Service
public class UserService
        extends ServiceImpl<UserRepository, User> {
    // 這里可以編寫(xiě)一些業(yè)務(wù)邏輯方法
    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)建啟動(dòng)器

@SpringBootApplication
// Mapper包掃描
@MapperScan("org.example.mapper")
public class OracleDBApplication {
    public static void main(String[] args) {
        SpringApplication.run(OracleDBApplication.class,args);
    }
}

5、測(cè)試

萬(wàn)事俱備,只欠東風(fēng),對(duì)CRUD進(jìn)行測(cè)試。

@SpringBootTest
public class OraCTest {
    @Autowired
    private UserService service;

    /**
     * 獲取所有用戶(hù)信息
     */
    @Test
    public void testGetAllUsers() {
        List<User> users = service.getAllUsers();
        users.forEach(System.out::println);
    }

    /**
     * 根據(jù)id獲取用戶(hù)
     */
    @Test
    public void testGetUserById() {
        User userById = service.getUserById(1L);
        System.out.println("userById = " + userById);
    }

    /**
     * 保存用戶(hù)
     */
    @Test
    public void testSaveUser() {
        User user = new User();
        user.setId(1000L);
        user.setUsername("測(cè)試新增User");
        user.setPassword("00101010");
        user.setEmail("test@example.com");
        user.setCreated_at(new Date());
        service.saveUser(user);
    }

    /**
     * 更新用戶(hù)
     */
    @Test
    public void testUpdateUser() {
        // 先查詢(xún)
        User user = service.getUserById(1000L);
        System.out.println("userById = " + user);
        // 后更新
        user.setUsername("update_username");
        service.updateUser(user);
    }

    /**
     * 根據(jù)id刪除用戶(hù)
     */
    @Test
    public void testDeleteUser() {
        service.deleteUser(1000L);
    }
}

補(bǔ)充知識(shí):Oracle主鍵自增問(wèn)題

在MySQL中,id可以直接設(shè)置自增長(zhǎng),而在Oracle中需要設(shè)置序列

 ① 以navicat為例進(jìn)行演示:

 ② 編寫(xiě)配置類(lèi)

@Configuration
public class KeyGeneratorConfig {
 
    @Bean
    public IKeyGenerator keyGenerator(){
        return new OracleKeyGenerator();
    }
}

 ③ 實(shí)體類(lèi)配置

總結(jié) 

到此這篇關(guān)于Spring Boot對(duì)接Oracle數(shù)據(jù)庫(kù)具體流程的文章就介紹到這了,更多相關(guān)SpringBoot對(duì)接Oracle內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的HashMap源碼分析

    Java中的HashMap源碼分析

    這篇文章主要介紹了Java中的HashMap源碼分析,散列表是根據(jù)關(guān)鍵碼值(Key?value)而直接進(jìn)行訪問(wèn)的數(shù)據(jù)結(jié)構(gòu),也就是說(shuō),它通過(guò)把關(guān)鍵碼值映射到表中一個(gè)位置來(lái)訪問(wèn)記錄,以加快查找的速度,這個(gè)映射函數(shù)叫做散列函數(shù),存放記錄的數(shù)組叫做散列表,需要的朋友可以參考下
    2023-09-09
  • 解決mybatis plus字段為null或空字符串無(wú)法保存到數(shù)據(jù)庫(kù)的問(wèn)題

    解決mybatis plus字段為null或空字符串無(wú)法保存到數(shù)據(jù)庫(kù)的問(wèn)題

    這篇文章主要介紹了解決mybatis plus字段為null或空字符串無(wú)法保存到數(shù)據(jù)庫(kù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Java自動(dòng)拆箱空指針異常的解決

    Java自動(dòng)拆箱空指針異常的解決

    這篇文章主要介紹了Java自動(dòng)拆箱空指針異常的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java實(shí)現(xiàn)上傳文件到oss(阿里云)功能示例

    java實(shí)現(xiàn)上傳文件到oss(阿里云)功能示例

    這篇文章主要介紹了java實(shí)現(xiàn)上傳文件到oss(阿里云)功能,結(jié)合實(shí)例形式詳細(xì)分析了java上傳文件到阿里云的具體步驟、配置及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-11-11
  • 淺析Java?BigDecimal為什么可以不丟失精度

    淺析Java?BigDecimal為什么可以不丟失精度

    在金融領(lǐng)域,為了保證數(shù)據(jù)的精度,往往會(huì)使用BigDecimal,所以這篇文章主要來(lái)和大家探討下為什么BigDecimal可以保證精度不丟失,感興趣的可以了解下
    2024-03-03
  • mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java訪問(wèn)修飾符public、private、protected及默認(rèn)訪問(wèn)權(quán)限詳解

    Java訪問(wèn)修飾符public、private、protected及默認(rèn)訪問(wèn)權(quán)限詳解

    這篇文章主要介紹了Java訪問(wèn)修飾符public、private、protected及默認(rèn)訪問(wèn)權(quán)限的相關(guān)資料,每種修飾符都有其特定的使用場(chǎng)景,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-01-01
  • SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn)

    SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java制作驗(yàn)證碼的完整實(shí)例代碼

    Java制作驗(yàn)證碼的完整實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Java制作驗(yàn)證碼的完整實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java中Array、List、Map相互轉(zhuǎn)換的方法詳解

    Java中Array、List、Map相互轉(zhuǎn)換的方法詳解

    這篇文章主要介紹了Java中Array、List、Map相互轉(zhuǎn)換的方法詳解,在實(shí)際項(xiàng)目開(kāi)發(fā)中或者一些算法面試題目中經(jīng)常需要用到Java中這三種類(lèi)型的相互轉(zhuǎn)換,比如對(duì)于一個(gè)整型數(shù)組中尋找一個(gè)整數(shù)與所給的一個(gè)整數(shù)值相同,需要的朋友可以參考下
    2023-08-08

最新評(píng)論