mybatis逆向工程

Mybatis官方提供了逆向工程,可以针对单表自动生成mybatis代码(mapper.java\mapper.xml\po类) 也就是说,我们可以通过mybatis逆向工程生成dao,pojo,mapper.xml这些东东,节省我们写这些代码的时间,可以把更多的时间花在业务上

  • 添加依赖

<!--设置编码-->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>
</dependencies>
  • 配置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>
    <properties resource="db.properties"/><context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码 ,加上“useSSL=false”是因为我SSL连接数据库出现了错误 -->
        <jdbcConnection driverClass="${mysql.driver}"
                        connectionURL="${mysql.url}"
                        userId="${mysql.username}" password="${mysql.password}">
        </jdbcConnection><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver><!-- targetProject:生成pojo类的位置 -->
        <javaModelGenerator targetPackage="pojo" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator><!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator><!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="mappers" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator><!-- 指定数据库表 -->
        <table schema="" tableName="animal"></table>
        <table schema="" tableName="cards"></table>
        <table schema="" tableName="cat"></table>
        <table schema="" tableName="cities"></table>
        <table schema="" tableName="dog"></table>
        <table schema="" tableName="man"></table>
        <table schema="" tableName="phone"></table>
        <table schema="" tableName="students"></table>
        <table schema="" tableName="user"></table>
    </context>
</generatorConfiguration>
  • 执行类,生成Mapper接口,Mapeer.xml映射文件和POJO类

public class MyBatisGeneratorApp {
    public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("D:\\javaProjects\\mybatis_generator\\src\\main\\resources\\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);
    }
    public static void main(String[] args) throws Exception {
        try {
            MyBatisGeneratorApp generatorSqlmap = new MyBatisGeneratorApp();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

 

posted @ 2020-10-28 22:03  hjy1995  阅读(109)  评论(0编辑  收藏  举报