Mybatis-flex整合达梦数据库的实现示例
目录
- 1.前言:
- 2.MyBATis-Flex概述:
- 3.环境准备:
- 3.1达梦数据库安装
- 3.2mysql迁移达梦、
- 4.整合步骤:
- 4.1添加依赖
- 4.2配置代码生成器
- 4.3application.yml配置
- 4.4配置数据库方言
- 5可能遇到的问题和解决方案。
- 5.1不支持LocalDateTime类的数据处理。
- 5.2长文本显示问题
1.前言:
国产化趋势下,达梦数据库作为作为关系型数据库的代表,在国产数据库中占据一席之地。好了,不拍马屁了相信很多人和我一样是中软杯赛项要求或者做信创项目才会用国产数据库的。
2.Mybatis-flex概述:
Mybatis-flex是一个为Mybatis增强而生的框架,它在保持Mybatis原有优势的基础上,通过提供更灵活的QueryWrapper、支持多表查询、联接查询、子查询等高级功能,以及对多数据源的管理和优化,显著提升了开发效率和性能。
3.环境准备:
3.1达梦数据库安装
参考:docker安装 | 达梦技术文档 (dameng.com)
3.2mysql迁移达梦、
参考:从 MySQL 移植到 DM | 达梦技术文档 (dameng.com)
4.整合步骤:
4.1添加依赖
达梦数据库驱动版本:8.1.2.141
<dependency> <groupId>com.dameng</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>8.1.2.141</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-processor</artifactId> <version>1.8.9</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-codegen</artifactId> <version>1.8.9</version> </dependency>
4.2配置代码生成器
public class Codegen { static class MyDialect extends AbstractJdbcDialect { //添加双引号 @Override protected String forBuildColumnsSql(String schema, String tableName) { return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + ".\"" : "") + tableName + "\" WHERE 1 = 2"; } } public static void main(String[] args) { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName("dm.jdbc.driver.DmDriver"); //url dataSource.setJdbcUrl("jdbc:dm://192.168.123.80:30236"); //用户名 dataSource.setUsername("SYSDBA"); //密码 dataSource.setPassword("SYSDBA001"); //创建配置内容,两种风格都可以。 GlobalConfig globalConfig = createGlobalConfigUseStyle1(); globalConfig.setGenerateSchema("RC"); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig,new MyDialect()); //数据库模式的名称 dataSource.setSchema("RC"); //生成代码 generator.generate(); } public static GlobalConfig createGlobalConfigUseStyle1() { //创建配置内容 GlobalConfig globalConfig = new GlobalConfig(); //设置根包 globalConfig.getPackageConfig() .setBasePackage("com.rc.pojo.po"); //设置生成 entity 并启用 Lombok globalConfig.enableEntity() .setWithLombok(true) .setWithBasePackage("po") .setJdkVersion(17); globalConfig.enableController(); globalConfig.enableTableDef(); globalConfig.enableMapper(); //代码生成的地址 globalConfig.setSourceDir("F:\\Java\\rural-classroom\\rc-pojo\\src\\main\\java"); return globalConfig; } }
4.3application.yml配置
url、username、password记得改成自己的
spring: datasource: driver-class-name: dm.jdbc.driver.DmDriver url: jdbc:dm://10.2.17.218:30236?clobAsString=1 username: SYSDBA password: SYSDBA001
4.4配置数据库方言
例如order、comment、user这些词在达梦中属于关键字需要用双引号转www.devze.com义
public class MyDmDialect extends CommonsDialectImpl { public static final Set<String> keywords = CollectionUtil.newHashSet( "Access", "ELSE", "MODIFY", "START", "ADD", "EXCLUSIVE", "NOAUDIT", "SELECTpython", "ALL", "EXISTS", "NOCOMPRESS", "SESSION", "ALTER", "FILE", "NOT", "SET", "AND", "FLOAT", "NOTFOUND", "SHARE", "ANY", "FOR", "NOWAIT", "SIZE", "ARRAYLEN", "FROM", "NULL", "SMALLINT", "AS", "GRANT", "NUMBER", "SQLBUF", "ASC", "GROUP", "OF", "SUCCESSFUL", "AUDIT", "HAVING", "OFFLINE", "SYNONYM", "BETWEEN", "IDENTIFIED", "ON", "SYSDATE", "BY", "IMMEDIATE", "ONLINE", "TABLE", "CHAR", "IN", "OPTION", "THEN", "CHECK", http://www.devze.com"INCREMENT", "OR", "TO", "CLUSTER", "INDEX", "ORDER", "TRIGGER", "COLUMN", "INITIAL", "PCTFREE", "UID", "COMMENT", "INSERT", "PRIOR", "UNION", "COMPRESS", "INTEGER", "PRIVILEGES", "UNIQUE", "CONNECT", "INTERSECT", "PUBLIC", "UPDATE", "CREATE", "INTO", "RAW", "USER", "CURRENT", "IS", "RENAME", "VALIDATE", "DATE", "LEVEL", "RESOURCE", "VALUES", "DECIMAL", "LIKE", "REVOKE", "VARCHAR", "DEFAULT", "LOCK", "ROW", "VARCHAR2", "DELETE", "LONG", "ROWID", "VIEW", "DESC", "MAXEXTENTS", "ROWLABEL", "WHENEVER", "DISTINCT", "MINUS", "ROWNUM", "WHERE", "DROP", "MODE", "ROWS", "WITH", "ADMIN", "CURSOR", "FOUND", "MOUNT", "AFTER", "CYCLE", "FUNCTION", "NEXT", "ALLOCATE", "DATABASE", "GO", "NEW", "ANALYZE", "DATAFILE", "GOTO", "NOARCHIVELOG", "ARCHIVE", "DBA", "GROUPS", "NOCACHE", "ARCHIVELOG", "DEC", "INCLUDING", "NOCYCLE", "AUTHORIZATION", "DECLARE", "INDICATOR", "NOMAXVALUE", "AVG", "DISABLE", "INITRANS", "NOMINVALUE", "BACKUP", "DISMOUNT", "INSTANCE", "NONE", "BEGIN", "DOUBLE", "INT", "NOORDER", "BECOME", "DUMP", "KEY", "NOREpythonSETLOGS", "BEFORE", "EACH", "LANGUAGE", "NORMAL", "block", "ENABLE", "LAYER", "NOSORT", "BODY", "END", "LINK", "NUMERIC", "CACHE", "ESCAPE", "LISTS", "OFF", "CANCEL", "EVENTS", "LOGFILE", "OLD", "CASCADE", "EXCEPT", "MANAGE", "ONLY", "CHANGE", "EXCEPTIONS", "MANUAL", "OPEN", "CHARACTER", "EXEC", "MAX", "OPTIMAL", "CHECKPOINT", "EXPLAIN", "MAXDATAFILES", "OWN", "CLOSE", "EXECUTE", "MAXINSTANCES", "PACKAGE", "COBOL", "EXTENT", "MAXLOGFILES", "PARALLEL", "COMMIT", "EXTERNALLY", "MAXLOGHISTORY", "PCTINCREASE", "COMPILE", "FETCH", "MAXLOGMEMBERS", "PCTUSED", "CONSTRAINT", "FLUSH", "MAXTRANS", "PLAN", "CONSTRAINTS", "FREELIST", "MAXVALUE", "PLI", "CONTENTS", "FREELISTS", "MIN", "PRECISION", "CONTINUE", "FORCE", "MINEXTENTS", "PRIMARY", "CONTROLFILE", "FOREIGN", "MINVALUE", "PRIVATE", "COUNT", "FORTRAN", "MODULE", "PROCEDURE", "PROFILE", "SAVEPOINT", "SQLSTATE", "TRACING", "QUOTA", "SCHEMA", "STATEMENT_ID", "TRANSACTION", "READ", "SCN", "STATISTICS", "TRIGGERS", "REAL", "SECTION", "STOP", "TRUNCATE", "RECOVER", "SEGMENT", "STORAGE", "UNDER", "REFERENCES", "SEQUENCE", "SUM", "UNLIMITED", "REFERENCING", "SHARED", "SWITCH", "UNTIL", "RESETLOGS", "SNAPSHOT", "SYSTEM", "USE", "RESTRICTED", "SOME", "TABLES", "USING", "REUSE", "SORT", "TABLESPACE", "WHEN", "ROLE", "SQL", "TEMPORARY", "WRITE", "ROLES", "SQLCODE", "THREAD", "WORK", "ROLLBACK", "SQLERROR", "TIME", "ABORT", "BETWEEN", "CRASH", "DIGITS", "ACCEPT", "BINARY_INTEGER", "CREATE", "DISPOSE", "ACCESS", "BODY", "CURRENT", "DISTINCT", "ADD", "BOOLEAN", "CURRVAL", "DO", "ALL", "BY", "CURSOR", "DROP", "ALTER", "CASE", "DATABASE", "ELSE", "AND", "CHAR", "DATA_BASE", "ELSIF", "ANY", "CHAR_BASE", "DATE", "END", "ARRAY", "CHECK", "DBA", "ENTRY", "ARRAYLEN", "CLOSE", "DEBUGOFF", "EXCEPTION", "AS", "CLUSTER", "DEBUGON", "EXCEPTION_INIT", "ASC", "CLUSTERS", "DECLARE", "EXISTS", "ASSERT", "COLAUTH", "DECIMAL", "EXIT", "ASSIGN", "COLUMNS", "DEFAULT", "FALSE", "AT", "COMMIT", "DEFINITION", "FETCH", "AUTHORIZATION", "COMPRESS", "DELAY", "FLOAT", "AVG", "CONNECT", "DELETE", "FOR", "BASE_TABLE", "CONSTANT", "DELTA", "FORM", "BEGIN", "COUNT", "DESC", "FROM", "FUNCTION", "NEW", "RELEASE", "SUM", "GENERIC", "NEXTVAL", "REMR", "TABAUTH", "GOTO", "NOCOMPRESS", "RENAME", "TABLE", "GRANT", "NOT", "RESOURCE", "TABLES", "GROUP", "NULL", "RETURN", "TASK", "HAVING", "NUMBER", "REVERSE", "TERMINATE", "IDENTIFIED", "NUMBER_BASE", "REVOKE", "THEN", "IF", "OF", "ROLLBACK", "TO", "IN", "ON", "ROWID", "TRUE", "INDEX", "OPEN", "ROWLABEL", "TYPE", "INDEXES", "OPTION", "ROWNUM", "UNION", "INDICATOR", "OR", "ROWTYPE", "UNIQUE", "INSERT", "ORDER", "RUN", "UPDATE", "INTEGER", "OTHERS", "SAVEPOINT", "USE", "INTERSECT", "OUT", "SCHEMA", "VALUES", "INTO", "PACKAGE", "SELECT", "VARCHAR", "IS", "PARTITION", "SEPARATE", "VARCHAR2", "LEVEL", "PCTFREE", "SET", "VARIANCE", "LIKE", "POSITIVE", "SIZE", "VIEW", "LIMITED", "PRAGMA", "SMALLINT", "VIEWS", "LOOP", "PRIOR", "SPACE", "WHEN", "MAX", "PRIVATE", "SQL", "WHERE", "MIN", "PROCEDURE", "SQLCODE", "WHILE", "MINUS", "PUBLIC", "SQLERRM", "WITH", "MLSLABEL", "RAISE", "START", "WORK", "MOD", "RANGE", "STATEMENT", "XOR", "MODE", "REAL", "STDDEV", "NATURAL", "RECORD", "SUBTYPE", "GEN", "KP", "L", "NA", "NC", "ND", "NL", "NM", "NR", "NS", "NT", "NZ", "TTC", "UPI", "O", "S", "XA" ); public MyDmDialect() { this(LimitOffsetProcessor.MYSQL); } public MyDmDialect(LimitOffsetProcessor limitOffsetProcessor) { //出现以上关键字时,添加 "" this(new Keywordwrap(false, true, keywords, "\"", "\""), limitOffsetProcessor); } public MyDmDialect(KeywordWrap keywordWrap, LimitOffsetProcessor limitOffsetProcessor) { super(keywordWrap, limitOffsetProcessor); } }
设置到flex中
@Configuration public class FlexConfig implements ConfigurationCustomizer { public FlexConfig(){ DialectFactory.registerDialect(DbType.DM,new MyDmDialect()); FlexGlobalCopythonnfig.getDefaultConfig().setDbType(DbType.DM); } }
到这里就可以像以前一样crud了
5可能遇到的问题和解决方案。
5.1不支持LocalDateTime类的数据处理。
旧版本达梦处理不支持将时间戳转为LocalDateTime,这里用的8.1.2.141是可以的,但是如果因为特殊原因使用低版本可以自己写一个TypeHandler把数据转一下
5.2长文本显示问题
长文本类型如text类型达梦驱动会把它用Clob类型封装而不是String类型,这时候需要在url上配置clobAsString=1
到此这篇关于Mybatis-flex整合达梦数据库的实现示例的文章就介绍到这了,更多相关Mybatis-flex整合达梦数据库内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论