新建一個Spring Initializr項目
2.把pom.xml文件中的oracle依賴換成自己的oracle版本依賴:
原來的:
現(xiàn)在的:
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.創(chuàng)建實例類——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;
}
}
創(chuàng)建接口類——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 ListUserinfos>findAll();
@Select("select userid,username,birthyear,userhight from userinfos where username like '%${name}%'")
public ListUserinfos>findUserSameName(String name);
}
創(chuàng)建服務類——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 ListUserinfos>searchAll(){
return udao.findAll();
}
public ListUserinfos>sameName(String name){
return udao.findUserSameName(name);
}
}
創(chuàng)建控制層類——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 ListUserinfos>init(){
return userinfosService.searchAll();
}
@RequestMapping("/same")
public ListUserinfos>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數(shù)據(jù)接口的文章就介紹到這了,更多相關springboot oracle數(shù)據(jù)接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 教你利用springboot集成swagger并生成接口文檔
- Vue+Springboot實現(xiàn)接口簽名的示例代碼
- Springboot添加支付接口
- SpringBoot集成Spring security JWT實現(xiàn)接口權限認證
- SpringBoot使用Feign調用其他服務接口
- springboot運行時新增/更新外部接口的實現(xiàn)方法
- 基于注解實現(xiàn) SpringBoot 接口防刷的方法
- SpringBoot 防止接口惡意多次請求的操作
- 使用SpringBoot跨系統(tǒng)調用接口的方案
- SpringBoot同一接口多個實現(xiàn)類配置的實例詳解
- SpringBoot可視化接口開發(fā)工具magic-api的簡單使用教程