【Oracle】springboot连接Oracle 集成mybatis、druid

news2024/9/22 23:30:21

目录

  • 项目结构与库表数据
  • pom.xml
  • application.yml
  • 实体类
  • Mapper
  • controller
  • 接口测试

基于spring-boot 2.7.11,连接Oracle 11g
仅做一个简单的示例

特别说明(不一定正确,还请指正):我Oracle也不熟,但据我观察发现,springboot项目配置Oracle驱动之后,就不需要Oracle的客户端instantclient,以及后面将该项目打成jar包部署的同时,linux环境中也不需要进行instantclient相关的配置。

项目结构与库表数据

在这里插入图片描述

-- 创建表: student_info 属主: scott (默认当前用户)
create table scott.student_info (
  sno         number(10) constraint pk_si_sno primary key,
  sname       varchar2(10),
  sex         varchar2(2),
  create_date date
);
-- 添加注释
comment on table scott.student_info is '学生信息表';
comment on column scott.student_info.sno is '学号';
comment on column scott.student_info.sname is '姓名';
comment on column scott.student_info.sex is '性别';
comment on column scott.student_info.create_date is '创建日期';

-- 插入
insert into scott.student_info (sno, sname, sex, create_date)
values (1, '张三', '男', sysdate);
insert into scott.student_info (sno, sname, sex, create_date)
values (2, '李四', '女', sysdate);
insert into scott.student_info (sno, sname, sex, create_date)
values (3, '王五', '男', sysdate);
commit;
select * from scott.student_info;

在这里插入图片描述

pom.xml

<?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 https://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.7.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>oraclDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>oracl-demo</name>
    <description>oracl-demo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!--oracle 连接-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

        <!-- 阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.14</version>
        </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.25</version>
        </dependency>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.15</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 8081

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    username: scott
    password: xxxxxx
    url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #初始化大小
      initialSize: 5
      #最小值
      minIdle: 5
      #最大值
      maxActive: 20
      #最大等待时间,配置获取连接等待超时,时间单位都是毫秒ms
      maxWait: 60000
      #配置间隔多久才进行一次检测,检测需要关闭的空闲连接
      timeBetweenEvictionRunsMillis: 60000
      #配置一个连接在池中最小生存的时间
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,
      #'wall'用于防火墙,SpringBoot中没有log4j,我改成了log4j2
      filters: stat,wall,log4j2
      #最大PSCache连接
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
      # 配置StatFilter
      web-stat-filter:
        #默认为false,设置为true启动
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      #配置StatViewServlet
      stat-view-servlet:
        url-pattern: "/druid/*"
        #允许那些ip
        allow: 127.0.0.1
        login-username: admin
        login-password: admin
        #是否可以重置
        reset-enable: true
        #启用
        enabled: true

实体类

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class Student {
    private int sno;
    private String sname;
    private String sex;
    private Date create_date;
}

Mapper

@Mapper
public interface StudentMapper {

    @Select("Select * from scott.student_info")
    List<Student> selectStudentList();

    @Select("Select * from scott.student_info where sname='张三' ")
    Student selectStudent();
}

controller

@Slf4j
@RestController
public class StudentController {

    @Resource
    public StudentMapper studentMapper;

    @GetMapping("/getstuList")
    public List getstuList(){
        log.info("执行了 getstuList");
        return studentMapper.selectStudentList();
    }

    @GetMapping("/getstu")
    public Student getstu(){
        log.info("执行了 selectStudent");
        return studentMapper.selectStudent();
    }

}

接口测试

成功从后台拿到数据。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/697416.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Java高级语法】(二十三)系统辅助工具类:解析System类,一个系统操作与资源管理工具类~

Java高级语法详解之系统辅助工具类 1️⃣ 概念2️⃣ 优势和缺点3️⃣ 使用3.1 System类常用方法3.2 使用技巧 4️⃣ 应用场景&#x1f33e; 总结 1️⃣ 概念 Java的System类是Java标准库中一个重要且常用的类。它被设计用于提供与系统相关的操作和信息访问功能。System类的设计…

【算法系列之贪心算法III】leetcode135. 分发糖果

134. 加油站 力扣题目链接 在一条环路上有 n 个加油站&#xff0c;其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车&#xff0c;从第 i 个加油站开往第 i1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发&#xff0c;开始时油箱为空。 给定…

创建临时文件mkstemp()和tmpfile()

有些程序需要创建一些临时文件&#xff0c;仅供其在运行期间使用&#xff0c;程序终止后即行删除。例如&#xff0c;很多编译器程序会在编译过程中创建临时文件。GNU C语言函数库为此而提供了一系列库函数。&#xff08;之所以有“一系列”的库函数&#xff0c;部分原因是由于这…

ASD光谱仪.asd格式光谱曲线文件转换为.txt格式的方法

本文介绍基于ViewSpec Pro软件&#xff0c;将ASD地物光谱仪获取到的.asd格式文件&#xff0c;批量转换为通用的.txt文本格式文件的方法。 ASD光谱仪是英国Malvern Panalytical公司研发的系列野外便携式全范围光谱辐射仪和光谱仪&#xff0c;可以获取地物的实时光谱信息。我们用…

Arch Linux 中的 AUR 是什么?您应该使用它吗?

Arch Linux AUR 存储库包含社区驱动的软件,如果您采取一些简单的预防措施,就可以安全使用。即使您不懂 shell 脚本,也可以使用一些指标来判断包是否安全。 AUR 是 Arch Linux 皇冠上的宝石之一,提供了数千个附加软件包。但是这个用户驱动的存储库使用起来安全吗,还是应该避…

你给企业创建百科了吗?5分钟带你看懂创建企业百度百科的实用技巧和注意事项

企业百度百科是一种企业在互联网上展示自身形象和产品的重要途径。通过在百度百科上创建企业页面&#xff0c;可以让更多的人了解企业的历史、文化、产品和服务等信息&#xff0c;提高企业知名度和品牌形象。分媒互动将介绍企业百度百科的创建方法和需要注意的事项。 一、企业百…

搭建IP代理池 - ProxyPool

前言 在爬虫开发中&#xff0c;我们经常会碰到ip封禁&#xff0c;访问限制的问题&#xff0c;今天主要分享个我在开发中用到过比较好用的ip代理池&#xff0c;希望有需要的小伙伴提供到帮助~ 简介 ProxyPool是一个简易高效的代理池&#xff0c;他可以在windows上搭配redis使…

【强化学习】常用算法之一 “SARSA”

作者主页&#xff1a;爱笑的男孩。的博客_CSDN博客-深度学习,活动,python领域博主爱笑的男孩。擅长深度学习,活动,python,等方面的知识,爱笑的男孩。关注算法,python,计算机视觉,图像处理,深度学习,pytorch,神经网络,opencv领域.https://blog.csdn.net/Code_and516?typeblog个…

飞控的安全性设计

针对安全性设计&#xff0c;就必须先考虑故障情况。一般来讲&#xff0c;飞控故障有以下几个方面&#xff1a; 1、通讯故障 飞行器与地面端&#xff08;遥控器或地面站等设备&#xff09;需要进行实时通信&#xff0c;如果通信发生故障&#xff0c;后果很严重&#xff0c;因此…

赛效:WPS文字(Word)中的页面背景如何删除

1&#xff1a;打开一个有背景颜色的文档。 2&#xff1a;在“页面布局”选项卡里点击“背景”&#xff0c;在下拉菜单里点击“删除页面背景”。 3&#xff1a;接下来我们看到&#xff0c;文档背景已经恢复了默认的颜色。 如果你想了解更多办公软件以及办公技巧&#xff0c;可以…

青大数据结构【2019】【五算法设计】

关键字: 简单选择排序、二叉树后序遍历 1) void Countsort(int A[],int B[],int n) {int i,j,count;for(i=0;i<n;i++){count=0;for(j=0;j<n;j++)if(A[j]<A[i])count++;B[count]=A[i];}} 2) 每个元素都要与n个元素(含自身)进行比较,故比较次数为n方 3) …

Redis之数据类型String、List、Hash、Set、Sorted Set(详细)

一、String数据类型 1、SET/GET/APPEND/STRLEN &#xff08;1&#xff09; APPEND &#xff08;2&#xff09; SET/STRLEN 2、 INCR/ DECR/INCRBY/DECRBY &#xff08;1&#xff09;INCR/ DECR &#xff08;2&#xff09; INCRBY/DECRBY INCRBY key increment&#xff1…

JavaWed第二章:HTML和CSS的知识制作静态网页

目录 前言 一.HTML和CSS的介绍 &#x1f496;HTML的基本框架 二.HTML常用标签大全 三.资源路径讲解 &#x1f496;路径 &#x1f496;图片 img标签讲解 &#x1f496;超链接标签讲解 四.CSS &#x1f496;CSS的引入方式 五.HTML页面布局 &#x1f496;盒子模型介绍 …

小米手机文件误删还有救,这10个工具请收好!

说到智能手机&#xff0c;小米以其令人印象深刻的功能和实惠的价格成为一个受欢迎的品牌。然而&#xff0c;与任何其他智能手机一样&#xff0c;小米设备上可能会由于各种原因而发生数据丢失。幸运的是&#xff0c;有多种恢复软件可以帮助您从小米设备中检索丢失或删除的数据。…

vue2 配置less

在vue2中配置less&#xff0c;需要安装less和less-loader npm install less less-loader5 --save-dev 直接安装less-loader会报错&#xff0c;提示如下&#xff1a; 安装less 3.0.0版本 npm install less3.3.0 然后在安装less-loader就ok啦。 在vue中使用&#xff0c;设置…

三维空间离散点如何拟合平面?

文章目录 0.引言1.算法原理2.算法实现 0.引言 在点云建模过程中&#xff0c;有时需要对扫描建模的点云进行标定&#xff0c;在实际使用中往往以地面做为参照平面&#xff0c;需要将扫描的三维空间点云进行拟合平面&#xff0c;以便纠正扫描结果。本文对三维空间离散点拟合平面算…

学习Vue3——watch(侦听器)

基本用法 watch有三个参数 第一个参数是监听源 第二个参数回调函数cb&#xff08;newVal,oldVal&#xff09; 第三个参数一个options配置项 监听单个属性 <template><div>姓:<input v-model"lastName" type"text" /></div><…

PostgreSQL 自增主键冲突问题分析及解决办法

创建一个test表 create table test (id integer default nextval(test_id_seq::regclass) not nullconstraint test_pkprimary key,c1 integer );插入数据 insert into test (c1) values (1); insert into test (c1) values (2); insert into test (c1) values (3);发现自增I…

自定义指令实现按钮加密

1. 需求描述 给按钮加一个密码保护&#xff0c;输入的密码正确之后才能进行下一步操作。 2. 实现思路 1. 直接在点击事件里面处理密码校验&#xff0c; <!-- template --> <el-button type"warning" click"handlePub">发布</el-button&…

Unity Android打包成Apk之后 紫屏 无内容

打包成Apk之后 打开游戏 过完logo是紫色的屏幕什么都没有 解决方法&#xff1a; 打开项目的目录&#xff1a; 删除除了 .vscode assets package 之外的所有文件夹 然后重新打开就可以了