• springboot整合Mybatis,自动生成代码


    1、POM添加依赖

     <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>


    2、application.yml配置数据连接

    ############################################################
    # 数据源相关配置,这里用的是阿里的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

    3、在启动主类添加扫描器

    4、增加generatorConfig.xml配置文件
    在resources文件下创建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">
    
    <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>
    

    5、创建Generator类

    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();
            }
    
        }
    }

    6、之后就会自动生成代码

     

  • 相关阅读:
    【年中总结】创业3年,越来越穷,还是坚持架构平台
    猿创征文|【深度学习前沿应用】文本生成
    Apollo planning之hybrid A*
    浅谈智慧空开在某市科技馆项目的应用-安科瑞 蒋静
    LeetCode刷题--- 最长定差子序列
    Spring Boot DTO 验证示例
    jsp文件中模板字符串变量${}失效
    docker安装minio作为图床服务
    Visual Studio 2019 C# 断点调试代码内存窗口显示无法计算表达式的解决问题
    「学习笔记」字符串基础:Hash,KMP与Trie
  • 原文地址:https://blog.csdn.net/qq_23000805/article/details/125431149