- 首先下载generate工具 主要就是jar包和xml文件(自己写太难了)提取码:srvk
- 然后编写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工具大大提高了开发效率希望对大家有益