二:mybatis逆向生成接口和映射

1:添加项目依赖


        
            mysql
            mysql-connector-java
            8.0.18
        
        
            org.mybatis
            mybatis
            3.5.1
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
    

2:mybatis-generator 配置文件,指定生成类和映射文件的路径及名称

image.png



    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
        

        
        
            
            
            
            
            
            
            
            
            
            
        

        

        
        
            
        

        

        
        
            
            
            
        

        
        
            
            
        
#mysql驱动的路径
jdbc.driverLocation=/Users/apple/Tomcat/apache-tomcat-9.0.14/webapps/ROOT/WEB-INF/lib/mysql-connector-java-5.1.6.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
#根据自己的地址修改
jdbc.connectionURL=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useServerPrepStmts=true&cachePrepStmts=true
jdbc.userId=root
jdbc.password=********

3:CommentGenerator,用来读取sql数据库的属性等

package mybatis.config;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import org.mybatis.generator.api.dom.java.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

// 注释生成 会获取到mysql数据库字段注释
public class CommentGenerator extends DefaultCommentGenerator {
    private Properties properties;
    private Properties systemPro;
    private boolean suppressDate;
    private boolean suppressAllComments;
    private String currentDateStr;

    public CommentGenerator() {
        super();
        properties = new Properties();
        systemPro = System.getProperties();
        suppressDate = false;
        suppressAllComments = false;
        currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }
}

4:创建启动类MyBatisGeneratorRun

package mybatis.config;

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 java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

// 自动生成插件启动类
public class MyBatisGeneratorRun {

    public static void main(String[] args) throws Exception{
        MyBatisGeneratorRun app = new MyBatisGeneratorRun();
        System.out.println(app.getClass().getResource("/").getPath());
        app.generator();
        System.out.println(System.getProperty("user.dir"));
    }
    public void generator() throws Exception{
        List warnings = new ArrayList();
        boolean overwrite = true;
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("myBatis/generator.config.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(resourceAsStream);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for(String warning:warnings){
            System.out.println(warning);
        }
    }
}

5:运行MyBatisGeneratorRun类,生成实体类与映射文件

image.png

你可能感兴趣的:(二:mybatis逆向生成接口和映射)