mybatis 逆向生成工具(记录)

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>
    <!-- targetRuntime:指定生成代码的运行目标.  可选值:MyBatis:可以生成动态WHERE条件的SQL;           MyBatis3Simple:只能生成简单的CURD;   Ibatista2Java2;  Ibatista2Java5 -->
    <!--<context id="DB2Tables" targetRuntime="MyBatis3Simple">-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 指定如何连接到目标数据库 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://***.***.**.***:3306/*****"
                        userId="******"
                        password="******">
        </jdbcConnection>

        <!-- Java类型解析器 -->
        <javaTypeResolver>
            <!-- 是否强制转换BigDecimals -->
            <property name="forceBigDecimals" value="false"></property>
        </javaTypeResolver>

        <!-- 配置JavaBean的生成策略 -->
        <!-- targetPackage:目标包名;targetProject:目标工程;-->
        <javaModelGenerator targetPackage="com.lwz.ssm.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"></property>
            <property name="trimStrings" value="true"></property>
        </javaModelGenerator>

        <!-- sql映射生成策略 -->
        <sqlMapGenerator targetPackage="com.lwz.ssm.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"></property>
        </sqlMapGenerator>

        <!--mapper接口的所在位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lwz.ssm.dao" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"></property>
        </javaClientGenerator>

        <!-- 根据表创建JavaBean-->
        <!-- tableName:表名;domainObjectName:要生成的类名;-->
        <table tableName="exercise_info" domainObjectName="ExerciseInfo"/>
        <table tableName="exercise_info_question" domainObjectName="ExerciseInfoQuestion"/>
        <table tableName="exercise_knowledge" domainObjectName="ExerciseKnowledge"/>
        <table tableName="exercise_level" domainObjectName="ExerciseLevel"/>
        <table tableName="exercise_question_answer" domainObjectName="ExerciseQuestionAnswer"/>
        <table tableName="exercise_question_option" domainObjectName="ExerciseQuestionOption"/>
        <table tableName="exercise_tag" domainObjectName="ExerciseTag"/>
        <table tableName="exercise_type" domainObjectName="ExerciseType"/>
        <table tableName="knowledge_info" domainObjectName="KnowledgeInfo"/>
        <table tableName="question_score_log" domainObjectName="QuestionScoreLog"/>
        <table tableName="tag_info" domainObjectName="TagInfo"/>
    </context>
</generatorConfiguration>

pom.xml:

        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- MyBatis generator(逆向工程) -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.1</version>
        </dependency>

main:

    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        String path=Thread.currentThread().getContextClassLoader().getResource("GeneratorConfig.xml").getPath();
        File configFile = new File(path);
        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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 MyBatis Generator 工具可以快速生成 MyBatis 的 Mapper 接口、XML 映射文件和实体类等代码,实现逆向工程。 下面是使用 Spring Boot 集成 MyBatis Generator 的步骤: 1. 在 pom.xml 中引入 MyBatis Generator 插件: ```xml <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <dependencies> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> ``` 2. 在 resources 目录下创建 generatorConfig.xml 配置文件,定义 MyBatis Generator 的配置信息: ```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> <context id="MySQLTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user"></table> </context> </generatorConfiguration> ``` 3. 在 Maven 中执行以下命令,生成 MyBatis 的 Mapper 接口、XML 映射文件和实体类等代码: ``` mvn mybatis-generator:generate ``` 执行成功后,会在 src/main/java/com/example/demo/entity 和 src/main/java/com/example/demo/mapper 目录下生成对应的代码。 以上就是使用 Spring Boot 集成 MyBatis Generator 的步骤

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值