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

目录

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

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

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

项目结构与库表数据

【Oracle】springboot连接Oracle 集成mybatis、druid_第1张图片

-- 创建表: 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;

【Oracle】springboot连接Oracle 集成mybatis、druid_第2张图片

pom.xml


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.11version>
        <relativePath/> 
    parent>
    <groupId>comgroupId>
    <artifactId>oraclDemoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>oracl-demoname>
    <description>oracl-demodescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>

        
        <dependency>
            <groupId>com.oracle.database.jdbcgroupId>
            <artifactId>ojdbc6artifactId>
            <version>11.2.0.4version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.14version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>2.0.25version>
        dependency>
        
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.8.15version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.6version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        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();
    }

}

接口测试

成功从后台拿到数据。
【Oracle】springboot连接Oracle 集成mybatis、druid_第3张图片

你可能感兴趣的:(springboot,oracle,mybatis,spring,boot)