下载地址:https://www.dameng.com/view_61.html
本文以x86 win64 DM8为例
安装完毕后打开DM数据库配置助手创建数据库,设置字符集utf8,去除字符大小写敏感
创建表空间及用户,最好是一个库对应一个用户一个表空间,创建用户时需要指定对应表空间
需要对用户分配DBA操作权限
针对现有项目或框架库需要同步迁移到达梦数据库,本文以mysql5.7为例,打开DM数据迁移工具,注意保持对象名大小写,选择表时全部取出再全选,迁移的表名和字段名就与原数据库保持一致
<dependency> <groupId>com.dameng</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>8.1.1.193</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.0</version> </dependency>
使用druid管理连接池,去除wall的配置否则会报错
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: dm.jdbc.driver.DmDriver url: jdbc:dm://localhost:5236/ROOT?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8 username: ROOT password: abcd@1234 filters: stat,slf4j
数据会在达梦的数据库驱动中强制大写,这对某些接口的数据返回给前端数据大小写出现问题,影响范围较大
我们可以通过混合使用jdbcTemplate进行查询的通用操作,调用query方法,传入自定义的ResultSetExtractor,得到jdbc原生的ResultSet对象,取出ResultSetMetaData转换成DmdbResultSetMetaData,其中的columns对象为私有对象且无方法访问,通过反射取出即可,通过columns获取到数据库实际的列名
public List<LinkedHashMap<String, Object>> findListByParam(String sqlText, Map<String, Object> map) { List<LinkedHashMap<String, Object>> result = new ArrayList<>(); List<Object> paramList = new ArrayList<>(); //解析sqlText中的占位符#{xxxx} String regex = "\\#\\{(?<RegxName>[\\w.]*)\\}"; String sqlTextCopy = sqlText; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(sqlTextCopy); while (matcher.find()) { String paramNameSymbol = matcher.group(0); sqlText = sqlText.replace(paramNameSymbol, " ? "); } logger.debug("【sqlText】:" + sqlText); //参数赋值 matcher = pattern.matcher(sqlTextCopy); while (matcher.find()) { String paramNameSymbol = matcher.group(0); String paramName = paramNameSymbol.replace("#", "").replace("{", "").replace("}", ""); Object paramValue = map.get(paramName); logger.debug("【paramName】:" + paramName); logger.debug("【paramValue】:" + paramValue); paramList.add(paramValue); } jdbcTemplate.query(sqlText, paramList.toArray(), new ResultSetExtractor<Object>() { @Override public Object extractData(ResultSet rs) throws SQLException, DataAccessException { try { ResultSetMetaData rsMetaData = rs.getMetaData(); Column[] dm_columns = null; if (dataBaseInfoUtil.getUsingDataBaseType() == GlobalEnum.DataBaseType.DM) { ResultSetMetaDataProxyImpl resultSetMetaDataProxy = (ResultSetMetaDataProxyImpl) rsMetaData; DmdbResultSetMetaData dmdbResultSetMetaData = (DmdbResultSetMetaData) resultSetMetaDataProxy.getRawObject(); Class dataClass = DmdbResultSetMetaData.class; Field field = dataClass.getDeclaredField("columns"); field.setAccessible(true); dm_columns = (Column[]) field.get(dmdbResultSetMetaData); } while (rs.next()) { LinkedHashMap<String, Object> resultitem = new LinkedHashMap<>(); for (int i = 1; i <= rsMetaData.getColumnCount(); i++) { String columnName = ""; if (dataBaseInfoUtil.getUsingDataBaseType() == GlobalEnum.DataBaseType.DM) { columnName = dm_columns[i - 1].name; } else { columnName = rsMetaData.getColumnName(i); ; } Object columnValue = rs.getObject(columnName); resultitem.put(columnName, columnValue); } result.add(resultitem); } } catch (Exception e) { e.printStackTrace(); } finally { return null; } } }); return result; }
在使用事务时,因为查询操作通过jdbcTemplate,更新操作通过myabtis,在某些隔离级别下会查询不到未提交的数据,所以需要统一数据源都为druid管理的datasource,这里的dynamicDataSource为我自定义的数据源处理对象,继承自spring的AbstractRoutingDataSource,为了处理多数据源情况
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { //SpringBootExecutableJarVFS.addImplClass(SpringBootVFS.class); final PackagesSqlSessionFactoryBean sessionFactory = new PackagesSqlSessionFactoryBean(); sessionFactory.setDataSource(dynamicDataSource()); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:mybatis/**/*Mapper.xml")); //关闭驼峰转换,防止带下划线的字段无法映射 sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(false); return sessionFactory.getObject(); } @Bean public JdbcTemplate jdbcTemplate(){ JdbcTemplate jdbcTemplate = null; try{ jdbcTemplate = new JdbcTemplate(dynamicDataSource()); }catch (Exception e){ e.printStackTrace(); } return jdbcTemplate; }
统一将查询操作结果转换成LinkHashMap键值对,再通过BeanMap映射成对应的实体类
Object value = map.get(resultkey); if(value instanceof ClobProxyImpl){ try { value = ((ClobProxyImpl) value).getSubString(1,(int)((ClobProxyImpl) value).length()); } catch (Exception e) { e.printStackTrace(); } }
Object value = map.get(resultkey); if(value instanceof DmdbBlob){ try { DmdbBlob dmdbBlob = (DmdbBlob)value; value = FileUtil.convertStreamToByte(dmdbBlob.getBinaryStream()); } catch (Exception e) { e.printStackTrace(); } }