天天看点

MyBatis Generator超级简单配置

本博客主要是想让大家快速搭建一个Mybatis Generator。用来生成我们需要的一些文件。

如果大家想深入理解。请参考:http://blog.csdn.net/isea533/article/details/42102297

我们不考虑配置mybatis ,Spring  , Spring MVC等环境。这里使用的maven项目和mysql数据库。

一、下载所需的包

在maven中只需写以下依赖既可。包括 mybatis generator包和mysql 的jdbc包。

<dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>1.3.2</version>
      </dependency>

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
      </dependency>
           

如果不会maven,也没有关系:

MBG包下载地址:http://repo1.maven.org/maven2/org/mybatis/generator/mybatis-generator-core/

jdbc下载地址:https://dev.mysql.com/downloads/connector/j/

下载完成导入到项目中既可。

二、下面就是配置我们的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>



    <context id="DB2Tables" targetRuntime="MyBatis3">

        <!-- 去掉自动生成的注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection
          driverClass="com.mysql.jdbc.Driver"  connectionURL="jdbc:mysql://localhost:3306/db?characterEncoding=utf8"
          userId="root" password="">
        </jdbcConnection>


        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 指定javabean生成的位置 package标明要声明在哪个包中。targetProject表明在那个工程下->
        <javaModelGenerator targetPackage="com.haopangang.beans" targetProject="./src/main/java/" >
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成对应的xml文件 指定SQL文件生成的位置-->
        <sqlMapGenerator
                targetPackage="mapper" targetProject="./src/main/resources/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mapperinterface ,也就是dao接口生成的文件的位置-->
        <javaClientGenerator type="XMLMAPPER"  targetPackage="com.haopangang.dao"  targetProject="./src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaClientGenerator>
        <!--
        <table tableName="table_name" domainObjectName="object_name"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
               -->
        <table tableName="emp" domainObjectName="Employee"></table>
        <table tableName="dep" domainObjectName="Department"></table>
    </context>
</generatorConfiguration>
           

其中红色的部分是要根据你自己的项目自己写。

下面是我的项目目录截图,大家参考下:

MyBatis Generator超级简单配置
MyBatis Generator超级简单配置

对于下面这两个。tableName表示使用数据库中的哪个表。

domainObjectName:生成的bean类叫什么名字。

大家根据自己的需求写。

<table tableName="emp" domainObjectName="Employee"></table>
<table tableName="dep" domainObjectName="Department"></table>
           

以下是我数据库中两个表,特别简单。参考下:

MyBatis Generator超级简单配置
MyBatis Generator超级简单配置

三 、运行生成

自己建一个测试类。写以下代码,运行即可。

public static void main( String[] args ) 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);
    }
           

最后生成以下文件:

MyBatis Generator超级简单配置
MyBatis Generator超级简单配置
MyBatis Generator超级简单配置

具体每个文件中的是什么意思,mybatis怎么调用。请查阅相关资料,或等下期博客。