赞
踩
项目中,通过mybatis-generator插件自动生成代码,直接构建出项目全面的基本结构。例如常用的POJO,DAO,Service,Service实现类,Controller层以及mapper.xml文件;进而达到快速开发的目的; 下面介绍两种方式实现,基本相同,写法不同而已;
引入pom插件
- <plugin>
- <!--Mybatis-generator插件,用于自动生成Mapper和POJO -->
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <!--配置文件的位置 -->
- <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
- <verbose>true</verbose>
- <overwrite>true</overwrite>
- </configuration>
- <executions>
- </executions>
- </plugin>
generatorConfig.xml 配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <!-- 自动生成mybatis相关类文件 -->
- <generatorConfiguration>
- <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
- <!--<classPathEntry location="?\mysql-connector-java-5.1.47.jar"/>-->
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <commentGenerator>
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true" />
- <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码 -->
- </commentGenerator>
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="数据库地址"
- userId="xxx" password="xxx">
- </jdbcConnection>
- <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
- NUMERIC 类型解析为java.math.BigDecimal -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- targetProject:自动生成代码的位置 -->
- <javaModelGenerator targetPackage="com.xxx.entity"
- targetProject="src/main/java">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="true" />
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
-
- <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
-
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="com.xxx.mapper" targetProject="src/main/java">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
-
-
- <!-- 表-->
- <table schema="xxx" tableName="xxx"
- domainObjectName="xxx">
- </table>
-
- </context>
-
- </generatorConfiguration>
运行这个maven插件就可以了
引入pom
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>3.0.7.1</version>
- </dependency>
可执行的java文件
- package com.xxx;
-
- import com.baomidou.mybatisplus.annotation.DbType;
- import com.baomidou.mybatisplus.generator.AutoGenerator;
- import com.baomidou.mybatisplus.generator.InjectionConfig;
- import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
- import com.baomidou.mybatisplus.generator.config.FileOutConfig;
- import com.baomidou.mybatisplus.generator.config.GlobalConfig;
- import com.baomidou.mybatisplus.generator.config.PackageConfig;
- import com.baomidou.mybatisplus.generator.config.StrategyConfig;
- import com.baomidou.mybatisplus.generator.config.TemplateConfig;
- import com.baomidou.mybatisplus.generator.config.po.TableInfo;
- import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
- import java.util.ArrayList;
- import java.util.List;
- import org.junit.Test;
-
- public class MyBatisPlusGenerator {
-
-
- @Test
- public void generator() {
- // 具体项目的绝对路径
- String projectPath = "E:/WorkingSpace/xxx/xxx/";
- // 生成mapper .xml 文件 地址的路径
- String mapperPath = projectPath + "src/main/resources/mappers/";
- // 项目代码路径
- String srcPath = projectPath + "src/main/java";
-
- GlobalConfig config = new GlobalConfig();
- // 数据库地址
- String dbUrl = "jdbc:mysql://xxx";
- DataSourceConfig dataSourceConfig = new DataSourceConfig();
- dataSourceConfig.setDbType(DbType.MYSQL)
- .setUrl(dbUrl)
- .setUsername("xxx")
- .setPassword("xxx")
- .setDriverName("com.mysql.cj.jdbc.Driver");
-
- StrategyConfig strategyConfig = new StrategyConfig();
- strategyConfig.setCapitalMode(false)
- .setEntityLombokModel(false)
- .setNaming(NamingStrategy.underline_to_camel)
- .setInclude(new String[]{"表名称"})
- //.setLogicDeleteFieldName("is_delete")
- .setTablePrefix("表前缀")
- .setEntityLombokModel(true);
-
- config.setActiveRecord(true)
- .setAuthor("xxx")
- .setOutputDir(srcPath)
- .setFileOverride(true)
- .setBaseResultMap(true)
- .setEnableCache(false)
- .setServiceName("%sService")
- .setServiceImplName("%sServiceImpl")
- .setMapperName("%sDao")
- .setXmlName("%sMapper")
- .setOpen(false);
-
- InjectionConfig cfg = new InjectionConfig() {
-
- @Override
- public void initMap() {
- }
- };
-
- List<FileOutConfig> foc = new ArrayList<>();
- foc.add(new FileOutConfig("/templates/mapper.xml.vm") {
- @Override
- public String outputFile(TableInfo tableInfo) {
- return mapperPath + tableInfo.getXmlName() + ".xml";
- }
- });
-
- cfg.setFileOutConfigList(foc);
-
- TemplateConfig tc = new TemplateConfig();
- // 根据需要生成文件 设置null即不生成对应文件
- // tc.setController(null);
- tc.setXml(null);
- // tc.setEntity(null);
-
- AutoGenerator ag = new AutoGenerator();
-
- ag.setGlobalConfig(config)
- .setDataSource(dataSourceConfig)
- .setStrategy(strategyConfig)
- .setPackageInfo(new PackageConfig()
- // 父层路径
- .setParent("com.xxx")
- .setEntity("domain")
- .setMapper("dao")
- )
- .setCfg(cfg)
- .setTemplate(tc)
- .execute();
- }
-
- }
直接运行就可以了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。