使用springboot暴露oracle数据接口的问题

新建一个spring initializr项目

2.把pom.xml文件中的oracle依赖换成自己的oracle版本依赖:

原来的:

现在的:

 <dependency>
            <groupid>com.oracle</groupid>
            <artifactid>ojdbc6</artifactid>
            <version>11.2.0.1.0</version>
        </dependency>

3.编辑application.yml

spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.oracledriver
    url: jdbc:oracle:thin:@192.168.21.200:1521:orcl
    username: alisa
    password: alisa

4.创建实例类——userinfos.java:

package cn.alisa.myspboracle.entity;
 
import java.util.date;
 
public class userinfos {
    private int userid;
    private string username;
    private date birthyear;
    private float userhight;
 
    public int getuserid() {
        return userid;
    }
 
    public void setuserid(int userid) {
        this.userid = userid;
    }
 
    public string getusername() {
        return username;
    }
 
    public void setusername(string username) {
        this.username = username;
    }
 
    public date getbirthyear() {
        return birthyear;
    }
 
    public void setbirthyear(date birthyear) {
        this.birthyear = birthyear;
    }
 
    public float getuserhight() {
        return userhight;
    }
 
    public void setuserhight(float userhight) {
        this.userhight = userhight;
    }
}

创建接口类——userinfosdao.java

package cn.alisa.myspboracle.dao;
 
import cn.alisa.myspboracle.entity.userinfos;
import org.apache.ibatis.annotations.select;
 
import java.util.list;
 
public interface userinfosdao {
    @select("select userid,username,birthyear,userhight from userinfos")
    public list<userinfos>findall();
 
    @select("select userid,username,birthyear,userhight from userinfos where username like '%${name}%'")
    public list<userinfos>findusersamename(string name);
}

创建服务类——userinfosservice.java

package cn.alisa.myspboracle.service;
 
import cn.alisa.myspboracle.dao.userinfosdao;
import cn.alisa.myspboracle.entity.userinfos;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
 
import java.util.list;
 
@service
public class userinfosservice {
    @autowired
    private userinfosdao udao;
 
    public list<userinfos>searchall(){
        return udao.findall();
    }
 
    public list<userinfos>samename(string name){
        return udao.findusersamename(name);
    }
}

创建控制层类——initctrl.java:

package cn.alisa.myspboracle.ctrl;
 
import cn.alisa.myspboracle.entity.userinfos;
import cn.alisa.myspboracle.service.userinfosservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
import java.util.list;
 
@restcontroller
public class initctrl {
    @autowired
    private userinfosservice userinfosservice;
 
    @requestmapping("/all")
    public list<userinfos>init(){
        return userinfosservice.searchall();
    }
 
    @requestmapping("/same")
    public list<userinfos>same(string name){
        return userinfosservice.samename(name);
    }
}

编辑myspboracleapplication.java类:

package cn.alisa.myspboracle;
 
import org.mybatis.spring.annotation.mapperscan;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
 
@springbootapplication
@mapperscan("cn.alisa.myspboracle.dao")
public class myspboracleapplication {
 
    public static void main(string[] args) {
 
        springapplication.run(myspboracleapplication.class, args);
    }
 
}

运行此类(myspboracleapplication.java),在浏览器中输入”http://localhost:8080/all”或者”http://localhost:8080/same?name=l”即可看到想要的结果。

到此这篇关于使用springboot暴露oracle数据接口的文章就介绍到这了,更多相关springboot oracle数据接口内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐