写在前面:只实现简单增删改查,不使用页面
使用的表为Oracle中Scott用户下的emp表。
方法1. 可以直接在这里进行勾选以下三个模块
Spring Web
MyBatis Framework
OracleDriver
方法2. 创建好项目使用pom.xml添加所需模块
这里为了方便演示使用的方法1
需要添加
com.aaa包下添加
controller
dao
entity
service
resources文件夹下
mapper
# 服务端口信息
server.port=8080
server.address=127.0.0.1
server.session-timeout=60
# 数据库连接信息
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.username=scott
spring.datasource.password=root
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
#mybatis配置xml文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
配置文件写好后可以进行代码编辑
Emp:
package com.aaa.entity;
import java.util.Date;
public class Emp {
private String empno;
private String ename;
private String job;
private String mgr;
private Date hiredate;
private String sal;
private String comm;
private String deptno;
// ->Get And Set
@Override
public String toString() {
return "Emp{" +
"empno='" + empno + '\'' +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr='" + mgr + '\'' +
", hiredate=" + hiredate +
", sal='" + sal + '\'' +
", comm='" + comm + '\'' +
", deptno='" + deptno + '\'' +
'}';
}
}
TestDao:
package com.aaa.dao;
import com.aaa.entity.Emp;
import java.util.List;
public interface TestDao {
/**
* 添加方法
* @param emp
* @return 添加后的主键id
*/
int insert(Emp emp);
/**
* 修改方法
* @param emp
* @return
*/
int update(Emp emp);
/**
* 删除方法
* @param emp
* @return
*/
int delete(Emp emp);
/**
* 查询一条数据
* @param emp
* @return
*/
Emp findOne(Emp emp);
/**
* 总条数
* @return
*/
int count();
/**
* 查所有
* @return
*/
List<Emp> findAll();
}
TestService:
package com.aaa.service;
import com.aaa.dao.TestDao;
import com.aaa.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
@Autowired
private TestDao testDao;
public int insert(Emp emp){
return testDao.insert(emp);
}
public int update(Emp emp){
return testDao.update(emp);
}
public int delete(Emp emp){
return testDao.delete(emp);
}
public Emp findOne(Emp emp){
return testDao.findOne(emp);
}
public int count(){
return testDao.count();
}
public List<Emp> findAll(){
return testDao.findAll();
}
}
TestController:
package com.aaa.controller;
import com.aaa.entity.Emp;
import com.aaa.service.TestService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@EnableAutoConfiguration
public class TestController {
@Resource
private TestService svc;
@RequestMapping("/test")
public String test(){
return "success";
}
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public String insert(Emp emp){
int i = svc.insert(emp);
return "新增数据的id为:"+i;
}
@RequestMapping(value = "/findOne",method = RequestMethod.GET)
public Emp findOne(Emp emp){
Emp result = svc.findOne(emp);
return result;
}
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public String update(Emp emp){
int i = svc.update(emp);
return "修改的数据为id为"+i;
}
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
public String delete(Emp emp){
int i = svc.delete(emp);
return "删除成功";
}
@RequestMapping(value = "/count",method = RequestMethod.GET)
public int count(){
return svc.count();
}
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
public List<Emp> findAll(){
return svc.findAll();
}
}
SpringbootOracleApplication:
package com.aaa;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.aaa.dao")
public class SpringbootOracleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootOracleApplication.class, args);
}
}
TestMapper:
<mapper namespace="com.aaa.dao.TestDao">
<insert id="insert" parameterType="com.aaa.entity.Emp" useGeneratedKeys="true" keyProperty="empno" keyColumn="empno">
insert into emp (empno, ename) values (#{empno},#{ename})
insert>
<update id="update" parameterType="com.aaa.entity.Emp">
update emp set ename=#{ename} where empno=#{empno}
update>
<delete id="delete" parameterType="com.aaa.entity.Emp">
delete from emp where EMPNO=#{empno}
delete>
<select id="findOne" resultType="com.aaa.entity.Emp" parameterType="com.aaa.entity.Emp">
select * from emp where empno=#{empno}
select>
<select id="count" resultType="java.lang.Integer">
select count(*) from emp
select>
<select id="findAll" resultType="com.aaa.entity.Emp">
select * from emp
select>
mapper>
以上就是实现SpringBoot+Mybatis+OracleCRUD的所有代码。
推荐使用这款idea插件进行接口验证
RESTfulToolkit
点击/insert
下方RequestParams会生成可以传入的参数
只需将需要添加的数据放入即可
下面为增删改查的截图
以上就是用SpringBoot+Mybatis+Oracle实现第一个项目。小伙伴们一起来试试吧!
项目传送门:下载项目资源