关注小程序 找一找教程网-随时随地学编程

XML教程

自动生成实体类和xml

  1 首先eclipse需要安装一个插件

  

2 两个配置文件

 1 generator.properties

  

2 generator.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="generator.properties" />
    <classPathEntry location="${classpath}" /> 
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="module-organization-configure"</span><span style="color: #ff0000;"> targetRuntime</span><span style="color: #0000ff;">="MyBatis3Simple"</span><span style="color: #ff0000;"> defaultModelType</span><span style="color: #0000ff;">="flat"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">去除注释 (true好像不起作用) </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">commentGenerator</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="suppressAllComments"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="suppressDate"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">commentGenerator</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">数据库连接 </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jdbcConnection </span><span style="color: #ff0000;">driverClass</span><span style="color: #0000ff;">="${driver}"</span><span style="color: #ff0000;"> connectionURL</span><span style="color: #0000ff;">="${url}"</span><span style="color: #ff0000;">
        userId</span><span style="color: #0000ff;">="${user}"</span><span style="color: #ff0000;"> password</span><span style="color: #0000ff;">="${password}"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jdbcConnection</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> 这个元素的配置用来指定JDBC类型和Java类型如何转换。 </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer  true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal  </span><span style="color: #008000;">--&gt;</span> 
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">javaTypeResolver</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="forceBigDecimals"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">javaTypeResolver</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> 该元素用来控制生成的实体类 </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">javaModelGenerator </span><span style="color: #ff0000;">targetPackage</span><span style="color: #0000ff;">="${package.entity}"</span><span style="color: #ff0000;"> targetProject</span><span style="color: #0000ff;">="${project}"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="enableSubPackages"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="trimStrings"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="false"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="rootClass"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="org.service.core.entity.BaseEntity"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">javaModelGenerator</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">生成SQLMAP文件 </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">sqlMapGenerator </span><span style="color: #ff0000;">targetPackage</span><span style="color: #0000ff;">="${package.mapper}"</span><span style="color: #ff0000;"> targetProject</span><span style="color: #0000ff;">="${project}"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="enableSubPackages"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">sqlMapGenerator</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现context id="DB2Tables" 修改targetRuntime="MyBatis3" </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">javaClientGenerator </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="XMLMAPPER"</span><span style="color: #ff0000;">
        targetPackage</span><span style="color: #0000ff;">="${package.mapper}"</span><span style="color: #ff0000;"> targetProject</span><span style="color: #0000ff;">="${project}"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="enableSubPackages"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="rootInterface"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="org.service.core.dao.IBaseMapper"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="methodNameCalculator"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="extended"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">javaClientGenerator</span><span style="color: #0000ff;">&gt;</span>

     <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 </span><span style="color: #008000;">--&gt;</span>
     <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">table </span><span style="color: #ff0000;">tableName</span><span style="color: #0000ff;">="his_allergic_history"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">table</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">context</span><span style="color: #0000ff;">&gt;</span>

</generatorConfiguration>  View Code

 

3 自动生成

右键-->Run As -->Run Mybatis Generator

4 实例

 表  

 实体类

  

xml里按需求改改