mybatis-plus 集成达梦数据库【其他数据库通用】
一、POM文件,引入所需要的依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.29</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.0.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> </dependencies>
二、(配置)达梦的驱动包,配置数据源
在达梦数据库的安装目录下有驱动包,我们先把jar包丢进来,放到lib这个文件夹下:
然后配置pom文件:
<dependency> <groupId>com.dm</groupId> <artifactId>Dm7JdbcDriver</artifactId> <version>1.7</version> <scope>system</scope> <systemPath>${project.basedir}/src/lib/Dm7JdbcDriver18.jar</systemPath> </dependency>
三、配置法或者直接在实体类中调用
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.baomidou.springboot.entity
typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
# 数据库相关配置
db-config:
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: id_worker
#字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: dm
#刷新mapper 调试神器
refresh: true
# 原生配置
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
或直接在类中设置
private final static String SCHEMANAME = "SYSDBA"; private final static String DATAURL = "jdbc:dm://localhost:5236/WSDC?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8"; private final static String DRIVERNAME = "dm.jdbc.driver.DmDriver"; private final static String USERNAME = "WSDC"; private final static String PASSWORD = "123456789"; private final static String AUTHOR = "YHT"; private final static String[] TABLE_NAME = { "ZW_ALERTREVIEW" }; public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java")// 生成路径 .setFileOverride(true)//文件覆盖 .setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I .setDateType(DateType.ONLY_DATE) .setAuthor(AUTHOR) .setOpen(false) .setSwagger2(true) //#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; .setIdType(IdType.AUTO) // 主键策略 .setBaseResultMap(true)// 生成基本的resultMap .setBaseColumnList(true);// 生成基本的SQL片段 // 自定义文件命名,注意 %s 会自动填充表实体属性! // gc.setMapperName("%sDao"); // gc.setXmlName("%sMapper"); // gc.setServiceName("MP%sService"); // gc.setServiceImplName("%sServiceDiy"); // gc.setControllerName("%sAction"); mpg.setGlobalConfig(gc); //达梦数据库的配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.DM); dsc.setSchemaName(SCHEMANAME); dsc.setUrl(DATAURL); dsc.setDriverName(DRIVERNAME); dsc.setUsername(USERNAME); dsc.setPassword(PASSWORD); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")) .setMapper("mapper")// dao .setService("service")// servcie .setController("controller")// controller .setEntity("bean") .setXml("mapper")// mapper.xml .setParent("com"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setInclude(TABLE_NAME); // 需要生成的表 //strategy.setSuperEntityColumns("id");//可去除实体类中ID strategy.setControllerMappingHyphenStyle(true);// 驼峰转连字符 strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setEntityLombokModel(true); strategy.setSuperControllerClass("com.util.controller.BaseController"); //strategy.setSuperServiceClass("com.amazon.base.service.BaseService"); //strategy.setSuperServiceImplClass("com.amazon.base.service.impl.BaseServiceImpl"); //strategy.setSuperMapperClass("com.amazon.base.mapper.BaseMapper"); //strategy.setSuperEntityClass("com.amazon.base.model.BaseModel"); // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // Boolean类型字段是否移除is前缀处理 // strategy.setEntityBooleanColumnRemoveIsPrefix(true) //strategy.entityTableFieldAnnotationEnable(true);// 是否生成实体时,生成字段注解 //strategy.setLogicDeleteFieldName("createDate"); mpg.setStrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }
都是开发,其他自己补充吧