MyBatis逆向工程——Java代码自动生成

关于代码自动生成,网上呢有很多工具,也有不同的方法,MyBatis是我接触到的第一款代码自动生成器,比较简单,代码量也比较少,也比较好理解,好了废话不多说,下面就给大家说一下实现方法及代码解释:

我们新建一个普通的Java项目,这里我使用的工具是idea,是一款很强大的代码编写工具。

首先是要实现代码自动生成所需要的jar包

其次编写一个Java类

import java.io.File;
import java.io.IOException;
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 GeneratorSqlmap {

public void generator() throws Exception{
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    //指定 逆向工程配置文件
    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);
} 
public static void main(String[] args) throws Exception {
    try {
        GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
        generatorSqlmap.generator();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

还有一个配置文件

<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

    <!-- targetProject:生成POJO类的位置 -->
    <javaModelGenerator targetPackage="cn.lht.pojo"
                        targetProject=".\src">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
        <!-- 从数据库返回的值被清理前后的空格 -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="cn.lht.mapper"
                     targetProject=".\src">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- targetPackage:mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="cn.lht.mapper"
                         targetProject=".\src">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </javaClientGenerator>

    <!-- 指定数据库表 -->
    <table schema="" tableName="tb_content"></table>
    <table schema="" tableName="tb_content_category"></table>
    <table schema="" tableName="tb_item"></table>
    <table schema="" tableName="tb_item_cat"></table>
    <table schema="" tableName="tb_item_desc"></table>
    <table schema="" tableName="tb_item_param"></table>
    <table schema="" tableName="tb_item_param_item"></table>
    <table schema="" tableName="tb_order"></table>
    <table schema="" tableName="tb_order_item"></table>
    <table schema="" tableName="tb_order_shipping"></table>
    <table schema="" tableName="tb_user"></table>

</context>

注意这个配置文件跟src目录是同级的

具体的代码注释我都在代码里边做了解释

然后运行就可以了

注意:如果你已经运行过一次,但是生成的代码你需要有所改动需要再次生成的话,你必须得把之前已经运行所生成的所有代码以及包结构都删除掉,否则就会出错。

如有意见或者更好的自动生成代码的方法,欢迎评论。

猜你喜欢

转载自www.cnblogs.com/lht0703/p/9990091.html
今日推荐