新建一个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 ListUserinfos>findAll();
@Select("select userid,username,birthyear,userhight from userinfos where username like '%${name}%'")
public ListUserinfos>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 ListUserinfos>searchAll(){
return udao.findAll();
}
public ListUserinfos>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 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数据接口的文章就介绍到这了,更多相关springboot oracle数据接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:- 教你利用springboot集成swagger并生成接口文档
- Vue+Springboot实现接口签名的示例代码
- Springboot添加支付接口
- SpringBoot集成Spring security JWT实现接口权限认证
- SpringBoot使用Feign调用其他服务接口
- springboot运行时新增/更新外部接口的实现方法
- 基于注解实现 SpringBoot 接口防刷的方法
- SpringBoot 防止接口恶意多次请求的操作
- 使用SpringBoot跨系统调用接口的方案
- SpringBoot同一接口多个实现类配置的实例详解
- SpringBoot可视化接口开发工具magic-api的简单使用教程