栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合Mybatis,自动生成代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合Mybatis,自动生成代码

1、POM添加依赖

 
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
       

       
            org.mybatis.generator
            mybatis-generator-core
            1.3.6
       


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文件。




    
    
    
    
        

        
        
        
        
            
        
        
        
            
            
            
        
        
        
            
        
        
        
            
        
        

5、创建Generator类

package com.example.serviceuser.utils;


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 warnings = new ArrayList();
        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 {
            List 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、之后就会自动生成代码

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988180.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号