最近学习了Oracle数据库,那么如何使用Spring Boot和MyBatis Plus对接Oracle数据库呢?
这就有了这篇随记,具体流程如下
创建一个空的Maven工程,导入如下依赖:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.7.RELEASEversion>
<relativePath/>
parent>
<groupId>org.examplegroupId>
<artifactId>oracle-initartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>com.oracle.database.jdbcgroupId>
<artifactId>ojdbc6artifactId>
<version>11.2.0.4version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.16.10version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.3.2version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
dependencies>
project>
tips:这里碰到一个坑,我本机适用的Oracle数据库版本是11g XE,所以要使用的驱动为ojdbc5/ojdbc6,不然连接老会失败。
在配置文件中填写数据库连接的参数
spring:
datasource:
username: pp
password: 123456
url: jdbc:oracle:thin:@localhost:1521:XE
driver-class-name: oracle.jdbc.OracleDriver
参数说明:
为了演示本次对接Oracle数据库,我们需要一张测试数据表
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
什么?没有测试数据!!!Orz…
这里可以使用Oracle数据库的PL/SQL编程来批量生成测试数据,真香!!
sql如下:
DECLARE
TYPE user_type IS RECORD (
id NUMBER,
username VARCHAR2(50),
email VARCHAR2(100),
password VARCHAR2(100),
created_at DATE
);
TYPE user_list IS TABLE OF user_type;
l_users user_list := user_list();
BEGIN
FOR i IN 1..100 LOOP
l_users.extend;
l_users(i).id := i;
l_users(i).username := 'user' || i;
l_users(i).email := 'user' || i || '@example.com';
l_users(i).password := 'password' || i;
l_users(i).created_at := SYSDATE; -- 使用当前时间作为创建时间
END LOOP;
FORALL i IN 1..l_users.COUNT
INSERT INTO users (id, username, email, password, created_at)
VALUES (l_users(i).id, l_users(i).username, l_users(i).email, l_users(i).password, l_users(i).created_at);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Data inserted successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
ROLLBACK;
END;
执行之后查看数据表中的数据:
OK!准备工作完成,接下来就可以进行对接了。
User:
/**
* TODO User实体类
* @version 1.0
* @author ss_419
* @date 2023/8/11 14:49
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
// 指定Oracle数据库中的表名
@TableName("users")
public class User {
private Long id;
private String username;
private String password;
private String email;
private Date created_at;
}
/**
* @author ss_419
*/
@Mapper
public interface UserRepository extends BaseMapper<User> {
// 这里可以自定义一些数据库操作方法
}
/**
* @author ss_419
*/
@Service
public class UserService
extends ServiceImpl<UserRepository, User> {
// 这里可以编写一些业务逻辑方法
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAllUsers() {
List<User> users = userRepository.selectList(null);
return users;
}
public User getUserById(Long id) {
return userRepository.selectById(id);
}
public void saveUser(User user) {
userRepository.insert(user);
}
public void updateUser(User user) {
userRepository.updateById(user);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
@SpringBootApplication
// Mapper包扫描
@MapperScan("org.example.mapper")
public class OracleDBApplication {
public static void main(String[] args) {
SpringApplication.run(OracleDBApplication.class,args);
}
}
万事俱备,只欠东风,对CRUD进行测试。
@SpringBootTest
public class OraCTest {
@Autowired
private UserService service;
/**
* 获取所有用户信息
*/
@Test
public void testGetAllUsers() {
List<User> users = service.getAllUsers();
users.forEach(System.out::println);
}
/**
* 根据id获取用户
*/
@Test
public void testGetUserById() {
User userById = service.getUserById(1L);
System.out.println("userById = " + userById);
}
/**
* 保存用户
*/
@Test
public void testSaveUser() {
User user = new User();
user.setId(1000L);
user.setUsername("测试新增User");
user.setPassword("00101010");
user.setEmail("[email protected]");
user.setCreated_at(new Date());
service.saveUser(user);
}
/**
* 更新用户
*/
@Test
public void testUpdateUser() {
// 先查询
User user = service.getUserById(1000L);
System.out.println("userById = " + user);
// 后更新
user.setUsername("update_username");
service.updateUser(user);
}
/**
* 根据id删除用户
*/
@Test
public void testDeleteUser() {
service.deleteUser(1000L);
}
}