SpringBoot+mybatis-plus+Oracle入门级操作
mybatis-plus
使用mybatisplus 便捷之处是在service serviceimpl mapper 定义了类的封装,便于操作数据库。本篇仅提供了简单的CRUD操作。
1、依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
2、application.properties文件配置
server:
port: 8899
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@121.196.53.196:1521:helowin
username: test
password: test
mybatis-plus:
configuration:
# 默认启用驼峰命名方式 此处数据库不需要驼峰命名
map-underscore-to-camel-case: false
jdbc:代表以baijdbc的方式连接;
oracle:表示连接的是oracle数据库;
thin:表示连接时采用thin模式(oracle中有两种模式);
@表示地址;
orcl代表数据库的sid。
thin
thin是一种瘦客户端的连接方式,即采用这种连接方式不需要安装oracle客户端,只要求classpath中包含jdbc驱动的jar包就行。thin就是纯粹用Java写的ORACLE数据库访问接口。
oci是一种胖客户端的连接方式,即采用这种连接方式需要安装oracle客户端。oci是Oracle
Call
Interface的首字母缩写,是ORACLE公司提供了访问接口,就是使用Java来调用本机的Oracle客户端,然后再访问数据库,优点是速度快,但是需要安装和配置数据库。
3、Mapper层
import com.alnex.oracle.pojo.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author Alnex
* @CreateTime 2020-08-25 16:46
* @Descirption
*/
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
@Mapper 配置后 启动类就不需进行扫描了
3、pojo对象
/**
* @Author Alnex
* @CreateTime 2020-08-25 16:37
* @Descirption JsonProperty 作用 json传值时 实体类的属性字段首字母为大写 需要设置映射配置
*/
@Data
@ToString
public class Student implements Serializable {
private static final long serialVersionUID = -7319131784076249735L;
@JsonProperty(value = "STUID")
private Integer STUID;
@JsonProperty(value = "STUNAME")
private String STUNAME;
@JsonProperty(value = "GENDER")
private String GENDER;
@JsonProperty(value = "AGE")
private Integer AGE;
@JsonProperty(value = "JOINDATE")
private Date JOINDATE;
@JsonProperty(value = "CLASSID")
private String CLASSID;
@JsonProperty(value = "ADDRESS")
private String ADDRESS;
}
@JsonProperty 注解作用 在postman post请求 json传值时 实体类的属性字段首字母为大写 不进行映射配置 传入数据获取不到。
4、controller 层
@RestController
@RequestMapping("student")
public class StudentController {
private Logger LOGGER = LoggerFactory.getLogger(StudentController.class);
@Autowired
StudentService studentService;
@GetMapping("/id/{id}")
public Object findById(@PathVariable("id") Integer id) {
LOGGER.info("接收的需要查询的数据id 为 {}", id);
return studentService.findById(id);
}
@GetMapping("/all")
public Object all() {
return studentService.list();
}
@PostMapping("/add")
public Object addDate(@RequestBody Student student){
LOGGER.info("接收的需要新增的数据student 为 {}", student);
return studentService.save(student);
}
@DeleteMapping("/del/{id}")
public Object delDate(@PathVariable("id") Integer id){
LOGGER.info("接收的需要删除数据的id 为 {}", id);
return studentService.deleteById(id);
}
}
5、service接口
public interface StudentService extends IService<Student> {
/**
* 根据id查询
* @param id
* @return
*/
Object findById(Integer id);
/**
* 根据id删除
* @param id
* @return
*/
Object deleteById(Integer id);
}
6、service 实现层
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Override
public Object findById(Integer id) {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("STUID",id);
return super.getOne(queryWrapper);
}
@Override
public Object deleteById(Integer id) {
Student student = (Student) this.findById(id);
if (student == null){
return "为查询到数据";
}
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("STUID", id);
return super.remove(queryWrapper);
}
}