SpringBoot+Maven配置达梦数据库连接

注意:要定位到项目文件夹的目录下操作下面指令,不然会报没有pom文件夹的错误:

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\      ). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]

1.在项目的端口中运行以下指令

mvn org.apache.maven.plugins:maven-install-plugin:3.1.0:install-file -Dfile="C:\D\DMdb\drivers\jdbc\DmJdbcDriver18.jar" -DgroupId="com.dm" -DartifactId="DmJdbcDriver" -Dversion="1.8.0" -Dpackaging="jar" -DgeneratePom=true

-DgroupId=com.dm 和-Dversion=1.8.0 会提示

[ERROR] Unknown lifecycle phase ".8.0". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-v ersion>]:<goal>. Available lifecycle phases are: pre-clean, clean, post-clean, validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compi le, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy. -> [Help 1]

报错把 ".8.0" 当成了生命周期阶段,说明 -Dversion=1.8.0 在命令行被拆成了 1.8.0 两个 token(和之前 .dm 的问题是同一类原因):shell/换行/续行符/隐藏字符 导致点号开头的片段被单独传给 Maven。故加一个双引号即可

执行完后的结果:

2. 然后再到pom文件中加入依赖

        <!-- 达梦数据库驱动包-->
        <dependency>
            <groupId>com.dm</groupId>
            <artifactId>DmJdbcDriver</artifactId>
            <version>1.8.0</version>
        </dependency>

3.修改数据库连接的配置文件

spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    # 推荐用 ?schema=... 参数(注意大写或小写均可,但以你驱动文档为准)
    url: jdbc:dm://localhost:5236?schema=springboot_vue&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
    username: SYSDBA
    password: *************

### 配置Spring Boot项目中使用MyBatis连接达梦数据库Spring Boot项目中集成MyBatis并连接达梦数据库(DM)需要对数据源、驱动类名、URL格式以及相关的持久化框架配置进行详细设置。以下是完整的配置方法和示例。 #### 数据库连接配置 在`application.properties`文件中,可以按照以下方式配置达梦数据库连接信息: ```properties # 达梦数据库连接配置 spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver spring.datasource.url=jdbc:dm://127.0.0.1:5236/demo? spring.datasource.username=SYSDBA spring.datasource.password=SYSDBA ``` 其中: - `driver-class-name`指定为达梦数据库的JDBC驱动类`dm.jdbc.driver.DmDriver`。 - `url`参数中,`demo`是数据库名称,根据实际情况替换;端口号默认为5236。 - 用户名和密码根据实际环境填写,例如`SYSDBA/SYSDBA`是达梦数据库的默认超级用户[^3]。 如果使用YAML格式的配置文件,则对应的`application.yml`内容如下: ```yaml spring: datasource: driver-class-name: dm.jdbc.driver.DmDriver url: jdbc:dm://127.0.0.1:5236/demo? username: SYSDBA password: SYSDBA ``` #### MyBatis配置 为了确保MyBatis能够正常工作,还需要在配置文件中指定MyBatis的相关属性,包括Mapper文件的位置和日志实现等: ```properties # MyBatis配置 mybatis.mapper-locations=classpath:dao/*.xml mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl ``` 上述配置指定了MyBatis的Mapper XML文件路径为`resources/dao/`目录,并启用了标准输出的日志实现以便调试[^3]。 对于YAML格式的配置文件,对应的MyBatis配置如下: ```yaml mybatis: mapper-locations: classpath:dao/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` #### Maven依赖配置 在`pom.xml`文件中添加达梦数据库的JDBC驱动依赖和MyBatis的启动器依赖: ```xml <!-- 达梦数据库依赖 --> <dependency> <groupId>com.dm</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>22.1.5.0</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> ``` 注意:达梦数据库的JDBC驱动版本应与实际安装的数据库版本保持一致,避免兼容性问题。 #### 示例代码 ##### Mapper接口 创建一个简单的Mapper接口用于操作数据库中的表,例如查询学生信息: ```java import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface StudentMapper { List<Student> findAllStudents(); } ``` ##### Mapper XML文件 在`resources/dao/StudentMapper.xml`中定义SQL语句: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.StudentMapper"> <select id="findAllStudents" resultType="com.example.model.Student"> SELECT * FROM STUDENT </select> </mapper> ``` ##### 实体类 创建一个与数据库表对应的实体类`Student`: ```java public class Student { private Integer id; private String name; private Integer age; // Getter and Setter methods } ``` ##### 测试类 编写一个测试类验证是否能够成功查询数据: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.List; @Component public class DatabaseTest implements CommandLineRunner { @Autowired private StudentMapper studentMapper; @Override public void run(String... args) { List<Student> students = studentMapper.findAllStudents(); for (Student student : students) { System.out.println(student.getName()); } } } ``` 通过以上配置和代码示例,可以在Spring Boot项目中成功使用MyBatis连接达梦数据库,并执行基本的CRUD操作。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值