<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
############################################################ # 数据源相关配置,这里用的是阿里的druid 数据源 ############################################################ spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_ccjg?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.initial-size=1 spring.datasource.druid.min-idle=1 spring.datasource.druid.max-active=20 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.stat-view-servlet.allow=true
<?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"> <generatorConfiguration> <!-- 数据库驱动,项目中如果有则不需要指定 --> <!--<classPathEntry location="mysql-connector-java-5.1.45.jar" />--> <!--数据库链接URL,用户名、密码 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8"/> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db_ccjg" userId="root" password="root"> </jdbcConnection> <!-- 是否启用java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 实体类路径 --> <javaModelGenerator targetPackage="com.example.serviceuser.pojo" targetProject="F:\公司资料\代码\manage-htmlx\cxjg-spring-cloud\service-user\src\main\java"> <!--<javaModelGenerator targetPackage="com.wonders.cop.po" targetProject="src/main/java">--> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- xml路径 --> <sqlMapGenerator targetPackage="mapper" targetProject="F:\公司资料\代码\manage-htmlx\cxjg-spring-cloud\service-user\src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- mapper接口路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.serviceuser.dao" targetProject="F:\公司资料\代码\manage-htmlx\cxjg-spring-cloud\service-user\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user_info"></table> </context> </generatorConfiguration>
package com.example.serviceuser.utils; /** * @ClassName &{NAME} * @Description TODO * @Author zhanghao MX8837 * @Date 2022/6/23 16:14 * @Version 1.0 **/ import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import org.springframework.util.ResourceUtils; public class GeneratorDisplay { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = ResourceUtils.getFile("classpath:generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { /* try { GeneratorDisplay generatorSqlmap = new GeneratorDisplay(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } */ try { List<String> warnings = new ArrayList<>(); boolean overwrite = true; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("generatorConfig.xml"); ConfigurationParser configurationParser = new ConfigurationParser(warnings); Configuration config = configurationParser.parseConfiguration(inputStream); DefaultShellCallback defaultShellCallback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, defaultShellCallback, warnings); myBatisGenerator.generate(null); } catch (Exception e) { e.printStackTrace(); } } }