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

相关推荐
Data_Journal1 小时前
使用 AutoScraper 进行网页抓取:分步教程
大数据·开发语言·数据库·python·scrapy
白远山2 小时前
健身场馆无人自动化解决方案:从架构到落地实践
java·开发语言·数据库·数据挖掘·需求分析
虎虎(_ _)。゜zzZ2 小时前
SQLAlchemy入门教程
数据库·后端·python·sql·ai·sqlalchemy
user_admin_god2 小时前
第 01 篇:OpenAI 兼容 API 初探 —— 用 curl 跑通第一次对话
java·人工智能·spring boot·语言模型·devops
Data_Journal3 小时前
如何将网页抓取用于机器学习
大数据·开发语言·数据库·python·scrapy
Lyra_Infra3 小时前
从 MySQL 到达梦:一次信创隔离环境里的数据库迁移踩坑实录
数据库·后端·mysql
数据工匠老o3 小时前
sysbench/TPC-C/自定义脚本:数据库压测工具对比与实战流程
数据库·测试
小辰爱喝汤4 小时前
基于智能定价的 校园二手交易平台-计算机毕业设计
spring boot·毕业设计·课程设计
数安3000天4 小时前
数据分类分级产品有哪四类技术路线?
大数据·数据库
泡茶喝茶写代码4 小时前
量化数据开发实战系列(第 25 篇):基金基础接口实战:基金列表、ETF-LOF 分类、基金概况、净值数据
java·数据库·人工智能·python·mysql