• Mybatis 代码自动生成(generatorConfig.xml配置)


     博客推荐:

    Mybatis最入门---代码自动生成(generatorConfig.xml配置)

    MyBatis Generator generatorConfig.xml配置详解

     pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>
      <groupId>com.watermelon</groupId>
      <artifactId>Simple</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
            <scope>test</scope>
        </dependency>
            
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
      <!-- 新增加 -->
    <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>

     1.java代码生成的方式

    <?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>
        <context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
            <property name="beginningDelimiter" value="`"/>
            <property name="endingDelimiter" value="`"/>
            
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <property name="addRemarkComments" value="true"/>
            </commentGenerator>
            
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
                        connectionURL="jdbc:mysql://localhost:3306/simple"
                        userId="root"
                        password="123456">
            </jdbcConnection>
            
            <javaModelGenerator targetPackage="test.model" 
                                targetProject="srcmainjava">
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            
            <sqlMapGenerator targetPackage="test.xml" 
                            targetProject="srcmain
    esources">
            </sqlMapGenerator>
            
            <javaClientGenerator type="XMLMAPPER"
                                targetPackage="test.dao" 
                                targetProject="srcmainjava">
            </javaClientGenerator>
            
            <table tableName="%">
                <generatedKey column="id" sqlStatement="MySql"/>
            </table>
        </context>
    </generatorConfiguration>

    package
    com.watermelon.generator; import java.io.IOException; 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.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class Generator { public static void main(String[] args) throws Exception, XMLParserException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; InputStream is = Generator.class.getResourceAsStream("/generator/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config, callback, warnings); mybatisGenerator.generate(null); for(String warning:warnings) { System.out.println(warning); } } }

     2.Eclispe插件的方式(推荐使用)

      优点;在所有生成的代码中都会有@mbggenerated 标记(通过MBG生成) 但是不管是maven,还是java main方法的方式运行生成的代码,

      重新生成后都会覆盖中间自己修改的代码。只有Eclispe插件的方式才不会修改。

    插件下载地址https://github.com/mybatis/generator/releases

    <?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>
        <!-- eclipse生成时必须指定classPathEntry 物理数据库驱动电脑上面可以随意指定一个 -->
        <classPathEntry location="E:javajarmysqlmysql-connector-java-5.1.18-bin.jar"/>
        <context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
            <!-- 在关键字名称前后自动拼接` -->
            <property name="beginningDelimiter" value="`"/>
            <property name="endingDelimiter" value="`"/>
            
            <property name="javaFileEncoding" value="UTF-8"/>
            
            <!-- 加注释 ,suppressDate去掉时间-->
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <property name="addRemarkComments" value="true"/>
            </commentGenerator>
            
            <!-- 配置数据库连接 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
                        connectionURL="jdbc:mysql://localhost:3306/simple"
                        userId="root"
                        password="123456">
            </jdbcConnection>
            
            <!-- eclipse插件生成时必须targetProject前加项目名称 -->
            <!-- 生成Model -->
            <javaModelGenerator targetPackage="test.model" 
                                targetProject="Simplesrcmainjava">
                <!--去掉多余的空格 
                    public void setUserName(String userName) {
                        this.userName = userName == null ? null : userName.trim();
                        } -->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            
            <!-- 生成Mapper xml文件 -->
            <sqlMapGenerator targetPackage="test.xml" 
                            targetProject="Simplesrcmain
    esources">
            </sqlMapGenerator>
            
            <!-- 生成对应的dao文件 -->
            <javaClientGenerator type="XMLMAPPER"
                                targetPackage="test.dao" 
                                targetProject="Simplesrcmainjava">
            </javaClientGenerator>
            
            <!-- tableName 生成的表名“%”多个表 -->
            <table tableName="%">
                <generatedKey column="id" sqlStatement="MySql"/>
            </table>
        </context>
    </generatorConfiguration>

    在配置文件上面单机右键运行。

  • 相关阅读:
    arcgis pro加载其他数据
    ArcGIS Pro运行Python脚本
    获得ArcGIS Pro的版本
    ArcGIS Pro使用键盘控制地图平移
    ArcGIS Pro添加注记工具
    ArcGIS Pro二次开发添加网络图层
    ArcGIS Pro放大缩小按钮
    ArcGIS Pro做一个矩形选择按钮
    ArcGIS Pro获得一个图层的样式
    ArcGIS Pro二次开发闪烁对象
  • 原文地址:https://www.cnblogs.com/watermelonban/p/7594839.html
Copyright © 2020-2023  润新知