spring boot+mybatis整合达梦数据库

一、项目目录结构

spring boot+mybatis整合达梦数据库

二、application.yml文件

###服务启动端口号
server:
  port: 2010
spring:
  application:
    name:  yjlc-Dm
#使用mybatis数据连接
#  datasource:
#     url: jdbc:dm://192.168.200.229:5236/testDB?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
#     username: SYSDBA
#     password: SYSDBA
#     driver-class-name: dm.jdbc.driver.DmDriver

#使用德鲁伊数据连接池
  datasource:
      name: dm8
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
        driverClassName: dm.jdbc.driver.DmDriver
        url: jdbc:dm://192.168.200.229:5236/?testDB&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
        username: SYSDBA
        password: SYSDBA
mybatis:
  mapper-locations: classpath*:mapping/**/*.xml

 三/启动类

package com.yjlc;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yjlc.mapper")
public class AppDMConformity {
    public static void main(String[] args) {
        SpringApplication.run(AppDMConformity.class);
    }
}

四、Service

package com.yjlc.service;

import com.yjlc.entity.UserEntity;
import com.yjlc.mapper.ConformityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class ConformityService {

    @Autowired
    ConformityMapper conformityMapper;
    @RequestMapping("/test")
    public void Test(){
        System.out.println("进入测试类");
        UserEntity userEntity = conformityMapper.selectById();
        System.out.println("获取到实体"+userEntity.toString());


    }
}

五、Entity

package com.yjlc.entity;


public class UserEntity {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

六、Mapper

package com.yjlc.mapper;

import com.yjlc.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ConformityMapper {

    UserEntity selectById();
}

七、Mapper.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.yjlc.mapper.ConformityMapper">

   <select id="selectById" resultType="com.yjlc.entity.UserEntity">
       select * from "testDB"."TABLE_1" where "id"=‘2‘;
   </select>

</mapper>

八、报错注意项

(一)mybatis访问达梦数据库时报SSL错误:

Caused by: dm.jdbc.driver.DMException: 初始化SSL环境失败

at dm.jdbc.driver.DBError.throwException(DBError.java:630) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at dm.jdbc.c.a.b(DBAccess.java:1078) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at dm.jdbc.c.a.s(DBAccess.java:158) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at dm.jdbc.driver.DmdbConnection.openConnection(DmdbConnection.java:660) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at dm.jdbc.driver.DmDriver.do_connect(DmDriver.java:183) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at dm.jdbc.driver.DmDriver.connect(DmDriver.java:458) ~[Dm8JdbcDriver18-8.1.1.49.jar:- 8.1.1.49 - Production]

at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:156) ~[druid-1.1.20.jar:1.1.20]

at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:218) ~[druid-1.1.20.jar:1.1.20]

at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:150) ~[druid-1.1.20.jar:1.1.20]

at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1645) ~[druid-1.1.20.jar:1.1.20]

at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1709) ~[druid-1.1.20.jar:1.1.20]

at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:932) ~[druid-1.1.20.jar:1.1.20]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]

at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1922) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1864) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]

... 72 common frames omitted

(二)解决方案:

在达梦数据库中运行以下代码关闭SSL验证并重启

sp_set_para_value(2,‘ENABLE_ENCRYPT‘,0);

  以上功能亲测有效,如果对以上步骤有所疑问的可以留言讨论。转载请说明出处,本人博客地址:https://www.cnblogs.com/chenyuanbo/

 

spring boot+mybatis整合达梦数据库

上一篇:Mysql 数据库


下一篇:德鲁伊数据库密码加密