MyBatis逆向工程

每次都要去官网上找挺麻烦的,所以准备给自己写一个方便复制粘贴的地方。
逆向生成网址:http://www.mybatis.org/generator/index.html
jar包地址

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

配置文件干了以下的几件事
如何连接到数据库,要生成哪些对象,应该使用哪些表来生成对象

<?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>
    <!--<classPathEntry location="D:\maven\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar" />-->
    <!--连接数据库-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mydata?useSSL=false"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <!--默认false,是否将Integral 型转为大数或者二进制型-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--pojo位置-->
        <javaModelGenerator targetPackage="com.pojo" targetProject=".\src\main\java">
            <!--没有包时是否生成-->
            <property name="enableSubPackages" value="true" />
            <!--去掉尾部空格-->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--mapper.xml位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--mapper接口位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!--哪些表-->
        <table  tableName="user" domainObjectName="User" />


    </context>
</generatorConfiguration>

生成方式有命令行、eclipse、java、Maven插件

以下是通过java方式运行

	 List<String> warnings = new ArrayList<String>();
   boolean overwrite = true;
   //该文件与src同级
   File configFile = new File("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);
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页