springboot2.1.0、Mybatis+Oracle简单使用

这篇博客介绍了如何在SpringBoot2.1.0项目中结合Mybatis操作Oracle数据库的步骤,包括创建Oracle表、添加依赖、定义实体类、创建Mapper接口和XML文件、配置数据库信息、启用Mapper扫描以及编写测试Controller。通过完成这些步骤,可以在运行项目后通过接口进行数据库访问测试。
摘要由CSDN通过智能技术生成

一)预先在Oracle中创建一张表,表名为employee

二)在pom.xml文件中添加mybatis和oracle相关jar依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.oysept.springboot</groupId>
    <artifactId>oysept-springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>oysept-springboot-mybatis</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
		
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		
        <!-- mybatis start -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisPlus 是 Mybatis 的增强工具,可以简化 Mybatis 的开发。MybatisX 是一款 Mybatis 开发插件,可以提高 Mybatis 的开发效率。下面是 MybatisPlus+SpringBoot+MybatisX 的联合步骤: 1. 在 pom.xml 文件中添加 MybatisPlus 和 MybatisX 的依赖。 ```xml <!-- MybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!-- MybatisX --> <dependency> <groupId>io.github.mybatisx</groupId> <artifactId>mybatisx-boot-starter</artifactId> <version>2.1.0</version> </dependency> ``` 2. 配置 MybatisPlus 和 MybatisX。 ```java @Configuration public class MybatisConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } @Bean public MybatisXConfigurer mybatisXConfigurer() { return new MybatisXConfigurer(); } } ``` 3. 编写实体类和 Mapper 接口,使用 MybatisPlus 提供的注解。 ```java @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String password; } public interface UserMapper extends BaseMapper<User> { } ``` 4. 在 application.yml 中配置数据库连接信息和 MybatisPlus 的相关信息。 ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml type-aliases-package: com.example.demo.entity global-config: db-config: id-type: auto field-strategy: not_null logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true cache-enabled: false mybatisx: enabled: true ``` 5. 在 Controller 中使用 Mapper 接口进行数据库操作。 ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user/{id}") public User getUser(@PathVariable("id") Long id) { return userMapper.selectById(id); } @PostMapping("/user") public boolean addUser(@RequestBody User user) { return userMapper.insert(user) > 0; } @PutMapping("/user") public boolean updateUser(@RequestBody User user) { return userMapper.updateById(user) > 0; } @DeleteMapping("/user/{id}") public boolean deleteUser(@PathVariable("id") Long id) { return userMapper.deleteById(id) > 0; } } ``` 这样,就完成了 MybatisPlus+SpringBoot+MybatisX 的整合。在 Controller 中,可以直接注入 Mapper 接口进行数据库操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值