mybatis------逆向工程

逆向工程

什么是逆向工程

Mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml,po...)

企业实际开发中,常用的逆向工程方式:

由数据库的表生成Java代码。

使用方法

所用jar包

jar包下载地址:

操作指南

操作指南在所下载的mybatis-generator-core-1.3.2\docs\index.html中

运行逆向程序

还可以使用插件运行,但是不建议。建议使用xml,不依赖开发工具。

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="testTables" targetRuntime="MyBatis3">

    <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;

    一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->

    <property name="autoDelimitKeywords" value="false"/>

    <!-- 生成的Java文件的编码 -->

    <property name="javaFileEncoding" value="UTF-8"/>

    <!-- 格式化java代码 -->

    <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

    <!-- 格式化XML代码 -->

    <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

    <commentGenerator>

        <!-- 是否去除自动生成的注释 -->

        <property name="suppressAllComments" value="true"/>

    </commentGenerator>

    <!-- 数据库链接信息 -->

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"

        connectionURL="jdbc:mysql://localhost:3306/mybatis"

        userId="root"

        password="123456">

    </jdbcConnection>

   <!--  <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"

        connectionURL="jdbc:oracle:thin:@10.163.91.6:1521:gitest01"

        userId="gisit"

        password="gisit">

</jdbcConnection> -->

<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和

            NUMERIC 类型解析为java.math.BigDecimal -->

    <javaTypeResolver >

      <property name="forceBigDecimals" value="false" />

</javaTypeResolver>

 

<!-- targetProject:生成PO类的位置 -->

    <javaModelGenerator targetPackage="mybatis.po" targetProject=".\src">

      <!-- enableSubPackages:是否让schema作为包的后缀 -->

      <property name="enableSubPackages" value="false" />

      <!-- 从数据库返回的值被清理前后的空格 -->

      <property name="trimStrings" value="true" />

    </javaModelGenerator>

    <!-- targetProject:mapper映射文件生成的位置 -->

    <sqlMapGenerator targetPackage="mybatis.mapper"  targetProject=".\src">

        <!-- enableSubPackages:是否让schema作为包的后缀 -->

      <property name="enableSubPackages" value="false" />

    </sqlMapGenerator>

    <!-- targetPackage:mapper接口生成的位置 -->

    <javaClientGenerator type="XMLMAPPER" targetPackage="mybatis.mapper"  targetProject=".\src">

      <!-- enableSubPackages:是否让schema作为包的后缀 -->

      <property name="enableSubPackages" value="false" />

    </javaClientGenerator>

    <!-- 指定数据库表 -->

    <!-- <table schema="GISIT" tableName="lcgrpcont" enableCountByExample="false" enableDeleteByExample="false"

        enableSelectByExample="false" enableUpdateByExample="false" ></table> -->

    <!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->

        <!-- tableName:要生成的表名

        domainObjectName:生成后的实例名

        enableCountByExample:Count语句中加入where条件查询,默认为true开启

        enableUpdateByExample:Update语句中加入where条件查询,默认为true开启

        enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启

        enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启

        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启

        -->

       <!--  <table tableName="lccont" domainObjectName="Lccont"

            enableCountByExample="false" enableUpdateByExample="false"

            enableDeleteByExample="false" enableSelectByExample="false"

            selectByExampleQueryId="false" /> -->

        <table tableName="items" />

        <table tableName="orderdetail"/>

        <table tableName="orders"/>

        <table tableName="user"/>

  </context>

</generatorConfiguration>

可以在操作指南中查找mybatis-generator-core-1.3.2\docs\index.html -->

主方法(逆向工程执行代码)

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) {

        try {

            GeneratorSqlMap generatorSqlMap=new GeneratorSqlMap();

            generatorSqlMap.generator();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

可以在操作指南中查找mybatis-generator-core-1.3.2\docs\index.html -->

猜你喜欢

转载自blog.csdn.net/qq_40802448/article/details/88583907
今日推荐