IDEA SpringBoot Mybatis逆向工程

IDEA SpringBoot Mybatis逆向工程

  1. 添加依赖

            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.2version>
            dependency>
    
    		
            <dependency>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-coreartifactId>
                <version>1.3.6version>
            dependency>
    
  2. 添加插件

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
                
                <plugin>
                    <groupId>org.mybatis.generatorgroupId>
                    <artifactId>mybatis-generator-maven-pluginartifactId>
                    <version>1.3.6version>
                    <dependencies>
                    
                        <dependency>
                            <groupId>mysqlgroupId>
                            <artifactId>mysql-connector-javaartifactId>
                            
                            <version>8.0.13version>
                        dependency>
                    dependencies>
                plugin>
            plugins>
        build>
    
  3. 在src/main/resources下添加generatorConfig.xml文件, 内容如下:

    
    
    
    <generatorConfiguration>
        
        
        
        
        
        <context id="mybatisGenerator" targetRuntime="MyBatis3">
            <commentGenerator>
                
                <property name="suppressAllComments" value="true" />
            commentGenerator>
            
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/shiro_csdn_demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
                            userId="root"
                            password="871403165">
            jdbcConnection>
    
            
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            javaTypeResolver>
    
            
            <javaModelGenerator targetPackage="com.sqt.generator.bean"
                                targetProject="src/main/java">
                
                <property name="enableSubPackages" value="false" />
                
                <property name="trimStrings" value="true" />
            javaModelGenerator>
            
            <sqlMapGenerator targetPackage="mapper"
                             targetProject="src/main/resources">
                
                <property name="enableSubPackages" value="false" />
            sqlMapGenerator>
            
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.sqt.generator.mapper"
                                 targetProject="src/main/java">
                
                <property name="enableSubPackages" value="false" />
            javaClientGenerator>
            
            <table tableName="sys_permission">table>
            <table tableName="user_info">table>
            <table tableName="sys_role">table>
            <table tableName="sys_role_permission">table>
            <table tableName="sys_user_role">table>
    
            
        context>
    generatorConfiguration>
    

    注意字节在xml中写&,会因为未转义而报错要将&写成& amp;

     connectionURL="jdbc:mysql://localhost:3306/shiro_csdn_demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
    
  4. 使用插件生成代码

IDEA SpringBoot Mybatis逆向工程_第1张图片

IDEA SpringBoot Mybatis逆向工程_第2张图片

IDEA SpringBoot Mybatis逆向工程_第3张图片
5. 如果使用的是java类而不是plugin来生成代码需要添加依赖, 但是可以将build中的plugin删除掉

<dependency>
      <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
       <version>8.0.16version>
dependency>

在java包中添加java代码, 执行该代码可以生成, 达到plugin同样的效果

public class Generator {
    public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        /**指向逆向工程配置文件*/
        File configFile = new File("src/main/resources/generatorConfig.xml");
        ConfigurationParser parser = new ConfigurationParser(warnings);
        Configuration config = parser.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 {
            Generator generatorSqlmap = new Generator();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(后端,Spring)