Mybatis相关

  1. <generatorConfiguration>  
  2. <!-- 引入配置文件 -->  
  3. <properties resource="init.properties"/>  //generator_config.xml中引入property文件
  4. 转自:http://www.cnblogs.com/lichenwei/p/4145696.htmlhtml

     

     

    Mybatis属于半自动ORM,在使用这个框架中,工做量最大的就是书写Mapping的映射文件,因为手动书写很容易出错,咱们能够利用Mybatis-Generator来帮咱们自动生成文件。java

     

    一、相关文件mysql

    关于Mybatis-Generator的下载能够到这个地址:https://github.com/mybatis/generator/releasesgit

    因为我使用的是MySQL数据库,这里须要再准备一个链接mysql数据库的驱动jar包github

    如下是相关文件截图:sql

     

    hibernate逆向生成同样,这里也须要一个配置文件:数据库

    generatorConfig.xmlmybatis

    复制代码

    1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE generatorConfiguration
     3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
     5 <generatorConfiguration>
     6     <!--数据库驱动-->
     7     <classPathEntry    location="mysql-connector-java-5.0.8-bin.jar"/>
     8     <context id="DB2Tables"    targetRuntime="MyBatis3">
     9         <commentGenerator>
    10             <property name="suppressDate" value="true"/>
    11             <property name="suppressAllComments" value="true"/>
    12         </commentGenerator>
    13         <!--数据库连接地址帐号密码-->
    14         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
    15         </jdbcConnection>
    16         <javaTypeResolver>
    17             <property name="forceBigDecimals" value="false"/>
    18         </javaTypeResolver>
    19         <!--生成Model类存放位置-->
    20         <javaModelGenerator targetPackage="lcw.model" targetProject="src">
    21             <property name="enableSubPackages" value="true"/>
    22             <property name="trimStrings" value="true"/>
    23         </javaModelGenerator>
    24         <!--生成映射文件存放位置-->
    25         <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
    26             <property name="enableSubPackages" value="true"/>
    27         </sqlMapGenerator>
    28         <!--生成Dao类存放位置-->
    29         <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
    30             <property name="enableSubPackages" value="true"/>
    31         </javaClientGenerator>
    32         <!--生成对应表及类名-->
    33         <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    34     </context>
    35 </generatorConfiguration>

    复制代码

    须要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置能够自定义)不能带有中文。app

    上面配置文件中的:框架

    <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    tableName和domainObjectName为必选项,分别表明数据库表名和生成的实体类名,其他的能够自定义去选择(通常状况下均为false)。

     

    生成语句文件:

    java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
    在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码便可。
相关文章
相关标签/搜索