Spring Boot项目中使用MyBatis连接达梦数据库6

在开发中,使用Spring Boot框架结合MyBatis来操作数据库是一种常见的做法。本篇博客将介绍如何在Spring Boot项目中配置MyBatis来连接达梦数据库6,并提供一个简单的示例供参考。(达梦六不仅分表还分模式.) 我拿SYSTEM表的LPS模式下面Student表做案例。

1. 添加依赖

首先,我们需要在pom.xml文件中添加相关依赖:

java 复制代码
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!--  添加dm6 jdbc jar 包依赖-->
        <dependency>
            <groupId>com.github.tianjing</groupId>
            <artifactId>Dm6JdbcDriver</artifactId>
            <version>1.0.0</version>
        </dependency>

2. 配置数据源、配置MyBatis

接下来,我们需要在application.yml文件中配置数据源信息:

bash 复制代码
spring:
  datasource:
    url: jdbc:dm6://localhost:12345/SYSTEM
    username: SYSDBA
    password: 密码自己写啦
    driver-class-name: dm6.jdbc.driver.DmDriver
    hikari:
      connection-test-query: select 1

mybatis:
  mapper-locations: classpath:/mappers/*.xml  # 修改为你的 MyBatis XML 映射文件路径
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl


server:
  # 服务器的HTTP端口
  port: 8081
  servlet:
    # 应用的访问路径
    context-path: /

3. 编写Mapper接口和SQL映射文件

src/main/resources目录下创建mappers文件夹,用于存放MyBatis的SQL映射文件。然后,编写Mapper接口和SQL映射文件,示例如下:

复制代码
@Mapper
public interface StudentMapper {
    List<Student> selectList();
}
bash 复制代码
    <select id="selectList" resultType="com.lps.domain.Student">
        select
        <include refid="Base_Column_List" />
        from LPS.STUDENT
    </select>

4. 编写Controller(偷懒没写service)

最后,编写相应的Controller层代码,调用Mapper接口执行数据库操作,并提供API接口供外部调用。

java 复制代码
@RestController
public class StudentController {
    @Autowired(required = false)
    StudentMapper studentMapper;
//增删改同理....
    @RequestMapping("/list")
    public String list() {
        List<Student> students = studentMapper.selectList();
        return students.toString();
    }
}

5、网页访问

总结

通过本文的介绍,我们学习了如何在Spring Boot项目中使用MyBatis连接达梦数据库6。首先,我们添加了必要的依赖项,然后配置了数据源和MyBatis,并编写了Mapper接口和SQL映射文件。最后,我们编写了Controller层代码来调用Mapper接口执行数据库操作,并提供API接口供外部调用。

报错注意事项

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method dm6/jdbc/driver/DmdbPreparedStatement.isClosed()Z is abstract] with root cause

java.lang.AbstractMethodError: Method dm6/jdbc/driver/DmdbPreparedStatement.isClosed()Z is abstract

相关推荐
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence2 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
阿杆2 天前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
昵称为空C2 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术3 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql