写在前面:只实现简单增删改查,不使用页面
使用的表为Oracle中Scott用户下的emp表。
一、创建项目
1.选择Spring Initializr(初始化)
2.输入配置信息
可以只填入Group和Artifact,其他默认
3.添加所需模块
方法1. 可以直接在这里进行勾选以下三个模块
Spring Web
MyBatis Framework
OracleDriver
方法2. 创建好项目使用pom.xml添加所需模块
这里为了方便演示使用的方法1
二、调整项目结构
需要添加
com.aaa包下添加
controller
dao
entity
service
resources文件夹下
mapper
上图是添加好包的效果。
三、编写application.properties配置文件
# 服务端口信息
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
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aaa</groupId>
<artifactId>springboot_oracle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_oracle</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>11.2.0.3</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n -->
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件写好后可以进行代码编辑
四、编写CRUD代码
entity实体类
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 + '\'' +
'}';
}
}
dao层接口:
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();
}
Service层
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();
}
}
Controller层
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);
}
}
mapper 文件
TestMapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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会生成可以传入的参数
只需将需要添加的数据放入即可
下面为增删改查的截图
添加接口
根据empno查询刚添加的数据
修改数据
删除数据
以上就是用SpringBoot+Mybatis+Oracle实现第一个项目。小伙伴们一起来试试吧!
项目传送门:下载项目资源