我是采用Maven的方式来运行的,关于其他更多运行方式可以参考官方文档.

首先需要在pom中添加插件:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
        <configurationFile>${basedir}/src/main/resources/mybatis/generatorConfig.xml</configurationFile>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>
</plugin>

可以通过configurationFile来指定配置文件的所在位置,如果不指定该值则默认为:${basedir}/src/main/resources/generatorConfig.xml

紧接着需要做的就是添加配置文件,按照以上的配置,需要在src/main/resources/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>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--生成的java文件的编码-->
        <property name="javaFileEncoding" value="utf-8"/>

        <!--生成java注释,true代表不生成-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc连接属性-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mywebsite?characterEncoding=utf-8"
                        userId="root"
                        password="123789">
        </jdbcConnection>

        <!--<javaTypeResolver>元素用于定义Java类型解析器的属性。Java Type Resolver用于根据数据库列信息计算Java类型。
        如果可能的话,默认的Java Type Resolver尝试通过替换Integral类型来使JDBC DECIMAL和NUMERIC类型更易于使用
        (Long,Integer,Short等)-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成的pojo类存放的位置-->
        <javaModelGenerator targetPackage="cn.zzy.Test.Entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--Java客户端生成器的属性,ANNOTATEDMAPPER代表以注解形式生成-->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.zzy.Test.Mapper"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--需要生成的表,table标签可以存在多个-->
        <table tableName="test_table"
               enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <!--该元素可以实现主键的自增:@Options(useGeneratedKeys=true,keyProperty="id")-->
            <generatedKey column="ID" sqlStatement="JDBC" identity="true" />
        </table>

    </context>
</generatorConfiguration>

最后在IDEA右侧找到插件双击运行即可:

查看目录结构,可以发现新增了几个文件:

因为是通过注解的方式生成的,所以是没有xml文件的,以下是TestTableMapper文件的内容:

package cn.zzy.Test.Mapper;

import cn.zzy.Test.Entity.TestTable;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface TestTableMapper {
    @Delete({
        "delete from test_table",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into test_table (name, age, ",
        "address, test_tablecol)",
        "values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=VARCHAR}, ",
        "#{address,jdbcType=VARCHAR}, #{test_tablecol,jdbcType=VARCHAR})"
    })
    @Options(useGeneratedKeys=true,keyProperty="id")
    int insert(TestTable record);

    @InsertProvider(type=TestTableSqlProvider.class, method="insertSelective")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int insertSelective(TestTable record);

    @Select({
        "select",
        "id, name, age, address, test_tablecol",
        "from test_table",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.VARCHAR),
        @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
        @Result(column="test_tablecol", property="test_tablecol", jdbcType=JdbcType.VARCHAR)
    })
    TestTable selectByPrimaryKey(Integer id);

    @UpdateProvider(type=TestTableSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(TestTable record);

    @Update({
        "update test_table",
        "set name = #{name,jdbcType=VARCHAR},",
          "age = #{age,jdbcType=VARCHAR},",
          "address = #{address,jdbcType=VARCHAR},",
          "test_tablecol = #{test_tablecol,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(TestTable record);
}

通过设置context标签的targetRuntime属性为MyBatis3Simple,可以关闭生成动态SQL:

删除之前生成的文件,再次运行,不再生成TestTableSqlProvider类了


只要你成为一个废物,就没人能利用你