1. 程式人生 > 實用技巧 >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/