java全栈Java学习笔记

二、mybatis-generator生成model,domai

2017-03-12  本文已影响123人  蓝山牧童

本系列目录 http://www.jianshu.com/p/22f3b28d974c

  mybatis手动创建实体,dao以及mapper配置文件的确浪费时间,而且容易出错。自动化工具替我们省了很多时间,让我们专注于业务层的实现。

一、下载工具包

二、配置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>  
    <!-- 数据库驱动包位置 -->  
    <classPathEntry location="e:\generator\mysql-connector-java-5.1.21.jar" />   
    <context id="DB2Tables" targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressAllComments" value="true" />  
        </commentGenerator>  
        <!-- 数据库链接URL、用户名、密码 -->  
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tale?characterEncoding=utf8" userId="root" password="123456">   
       
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="com.albert.domain.table" targetProject="e:\generator\src">  
            <property name="enableSubPackages" value="true" />  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator>  
        <!-- 生成的映射文件包名和位置 -->  
        <sqlMapGenerator targetPackage="com.albert.dao" targetProject="e:\generator\src">  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.albert.dao" targetProject="e:\generator\src">  
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->  
        <table tableName="t_contents" domainObjectName="Contents" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> 
        <table tableName="t_attach" domainObjectName="Attach" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> 
    </context>  
</generatorConfiguration>  

三、创建src目录

这个目录下面就是生成我们model,dao以及mapper文件的地方。上面三部分配置好后目录结构下图所示

Paste_Image.png

四、最激动 的一步

在这一步将会生成model,dao以及mapper.xml文件。在命令行进入上图的目录,然后运行下面的命令

java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite

运行成功后,命令窗口出现 MyBatis Generator finished successfully.大功告成,看看src目录下面吧

上一篇下一篇

猜你喜欢

热点阅读