Springboot+Druid+Mybatis+Oracle整合

导入依赖

需要导入的依赖有

1、Springboot-web

2、ojdbc8

3、spring-boot-starter-test

4、druid-spring-boot-starter

5、mybatis-spring-boot-starter

6、orai18n

 

     
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.oracle.database.jdbc
            ojdbc8
            runtime
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        

        
        
            com.oracle.database.nls
            orai18n
            21.1.0.0
        

创建配置文件application.yml

    url: jdbc:oracle:
    username: 
    password: 

以上三个值根据自己实际的数据库来配置

server:
  port: 8891
spring:
  mvc:
     static-path-pattern: classpath:/**
# 配置 Oracle
 datasource:
    driver-class-name: oracle.jdbc.OracleDriver   #固定

    url: jdbc:oracle:thin:@localhost:1521:orcl  #按你本地的来
    username: oracle # 按你本地的来
    password: oracle # 按你本地的来
    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
          #禁止那些ip
          deny: 192.168.1.102
          #是否可以重置
          reset-enable: true
          #启用
          enabled: true

# mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml    # mapper映射文件位置
  type-aliases-package: com.gouggou.shardingtable.entity    # 实体类所在的位置
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   #用于控制台打印sql语句
    map-underscore-to-camel-case: true


看一下结构目录

Springboot+Druid+Mybatis+Oracle整合_第1张图片

bean类

Msg(返回消息类,该类可无)

package cn.orz.bean;



import java.util.HashMap;
import java.util.Map;

public class Msg {
    //状态码 10001 成功 10002失败
    private int code;
    private String msg;
    private Map extend=new HashMap();

    /**
     * 返回成功
     * @return
     */
    public static  Msg Success (){
        Msg result = new Msg();
        result.setCode(10001);
        result.setMsg("成功");
        return result;
    }

    /***
     * 返回失败
     * @return
     */
    public static  Msg fail (){
        Msg result = new Msg();
        result.setCode(10002);
        result.setMsg("失败");
        return result;
    }

    public Msg Add(String key,Object value){
        this.getExtend().put(key,value);
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map getExtend() {
        return extend;
    }

    public void setExtend(Map extend) {
        this.extend = extend;
    }

    @Override
    public String toString() {
        return "Msg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", extend=" + extend +
                '}';
    }

}

config类

WebConfig

package cn.orz.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



@Configuration
public class WebConfig implements WebMvcConfigurer {
    /*
     * 添加静态资源文件,外部可以直接访问地址
     *
     * @param registry
     */
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);

    }
}

User类

package cn.orz.bean;

public class User {

    String ID ;
    String USERNAME;
    String PASSWORD;
    String REALNAME ;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getUSERNAME() {
        return USERNAME;
    }

    public void setUSERNAME(String USERNAME) {
        this.USERNAME = USERNAME;
    }

    public String getPASSWORD() {
        return PASSWORD;
    }

    public void setPASSWORD(String PASSWORD) {
        this.PASSWORD = PASSWORD;
    }

    public String getREALNAME() {
        return REALNAME;
    }

    public void setREALNAME(String REALNAME) {
        this.REALNAME = REALNAME;
    }

    @Override
    public String toString() {
        return "User{" +
                "ID='" + ID + '\'' +
                ", USERNAME='" + USERNAME + '\'' +
                ", PASSWORD='" + PASSWORD + '\'' +
                ", REALNAME='" + REALNAME + '\'' +
                '}';
    }
}

Controller层

UserController

package cn.orz.controller;

import cn.orz.bean.Msg;
import cn.orz.bean.User;
import cn.orz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping("/GetUser/{id}")
    public Msg GetUser(@PathVariable("id") String id) {

       User user= userService.GetUser(id);

        return Msg.Success().Add("User",user);

    }
}

Service层

UserService

package cn.orz.service;

import cn.orz.bean.User;
import cn.orz.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    UserMapper userDao;

    public User GetUser(String id) {

        return userDao.GetUser(id);
    }
}

Dao层

UserMapper
package cn.orz.dao;

import cn.orz.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;


@Mapper
@Component
public interface UserMapper {


   public User GetUser(String id);

}
Mapper
UserMapper.xml





    



最后在Springboot的启动类加@MapperScan注解


@MapperScan("cn.orz.dao")

Springboot+Druid+Mybatis+Oracle整合_第2张图片

数据库的表结构为:

create table USER1
(
  ID       VARCHAR2(50) not null,
  USERNAME VARCHAR2(200) not null,
  PASSWORD VARCHAR2(11),
  REALNAME VARCHAR2(100)
)

表数据为

Springboot+Druid+Mybatis+Oracle整合_第3张图片

效果:

1、druid后台登录页面

地址为

http://localhost:8891/druid

Springboot+Druid+Mybatis+Oracle整合_第4张图片

登录成功后的页面

账号和密码在yml配置文件上

Springboot+Druid+Mybatis+Oracle整合_第5张图片

Springboot+Druid+Mybatis+Oracle整合_第6张图片

看一下Web请求结果

http://localhost:8891/User/GetUser/1

Springboot+Druid+Mybatis+Oracle整合_第7张图片

Springboot+Druid+Mybatis+Oracle整合_第8张图片

至此结束

你可能感兴趣的:(Springboot,Oracle,Druid,mybatis,spring,boot,oracle)