菜鸟一枚,记录成长(2)

本文介绍如何使用MyBatis Generator工具自动生成数据库操作代码,包括配置文件详解及执行步骤,大大提高开发效率。
  1. 首先下载generate工具 主要就是jar包和xml文件(自己写太难了)http://pan.baidu.com/s/1miOwA4C提取码:srvk
  2. 然后编写generator配置文件 需要注意的地方已经注释上去了,只要修改下配置文件就ok了。

<?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="C:\Users\Gost\Desktop\SpringMvc+Spring+MyBatis+Maven\generator\generator\mysql-connector-java-5.1.26-bin.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/sy" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="cn.lsu.model" targetProject="C:\Users\Gost\Desktop\SpringMvc+Spring+MyBatis+Maven\generator\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="cn.lsu.mapping" targetProject="C:\Users\Gost\Desktop\SpringMvc+Spring+MyBatis+Maven\generator\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.lsu.dao" targetProject="C:\Users\Gost\Desktop\SpringMvc+Spring+MyBatis+Maven\generator\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="tuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="tmenu" domainObjectName="Menu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="tonline" domainObjectName="Online" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="tresource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="trole" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="tbug" domainObjectName="Bug" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="trole_tresource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="tuser_trole" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

3.然后建立空的src文件夹

4.shift+右键在此位置运行(xp没有此功能,只有在cmd中依次找到文件)执行语句

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite(在执行语句时注意你上面配置的一切路径不要有中文,还有就是可能遇到总是报“前言中不允许有内容”的问题,解决方案:

用记事本打开xml文件转化为UTF-8会有一个BOM头,所以java在读取时就会报以上错误

解决方法:用notepad++打开,在格式选择“以UTF-8无BOM格式编码”,然后保存, 就可以了

完成代码的自动生成

 

本人以为generate工具大大提高了开发效率希望对大家有益

 

 

 

转载于:https://my.oschina.net/yuzhoukai/blog/704439

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值