mybatis逆向工程生成dao、mapper和model组件
-步骤1.需要准备两个jar文件:
①mybatis逆向工程包:mybatis-generator-core-1.3.5.jar
②数据库连接驱动包(这里是mysql驱动包):mysql-connector-java-5.1.26-bin.jar
- 步骤2.创建文件夹mybatis-generator(任意命名),将步骤1中相应的jar复制到该文件夹下,并创建如下文件:
①项目配置文件:generatorConfig.xml
②生成项目的接口dao,映射文件mapper和持久层组件pojo文件的跟目录目录文件:myProject(任意命名)
-步骤3.配置generatorConfig.xml,如下:
<?xml version="1.0" encoding="gb2312"?>
<!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="mysql-connector-java-5.1.26-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db_lab"
userId="root" password="wampp">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类(持久类组件)存放位置-->
<javaModelGenerator targetPackage="com.zsw.pojo" targetProject="D:\aaa\myProject\src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件(mapper.xml)存放位置-->
<sqlMapGenerator targetPackage="com.zsw.mapping" targetProject="D:\aaa\myProject\src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zsw.dao" targetProject="D:\aaa\myProject\src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="tb_user" domainObjectName="User" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="tb_banji" domainObjectName="Clazz" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="tb_student" domainObjectName="Student" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
-**步骤4.生成语句:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
这里为了方便使用,先把它放在 生成语句文件.txt 中。**
-步骤5.在aaa目录按住Shift键,右键鼠标选择”在此处打开命令窗口”,复制粘贴生成语句代码即可。
这里的目录结构如下
最后:经过第5步操作后,如出现如下字样则表示逆向操作成功!
“MyBatis Generator finished successfully, there were warnings.”
成功后的项目结构如下:
解析:
1.classPathEntry标签:引入连接数据库的驱动
①location:定义数据库驱动的位置
2.jdbcConnection标签:创建数据库连接
① driverClass:数据库驱动类
② connectionURL:连接路径
③ userId:用户名
④ password:连接密码
userId=”root” password=”wampp”>
1.sqlMapGenerator 标签:生成映射文件mapper.xml
2.javaModelGenerator标签:生成Model类
3.javaClientGenerator标签:生成Dao接口
①targetPackage:生成文件的位置
②targetProject:项目所在位置
4.table标签:定义需要进行逆向操作的数据表。
①tableName:表名
②domainObjectName:逆向操作后生成model的类名。
错误纠正:在该过程中,可能在进行第5步操作时,会有如下报错:
“前言不允许有内容!”
是因为在用记事本打开xml文件转化为utf-8会有一个BOM头,所以Java在读取时就会报以上错误。
解决方法:
①用eclipse打开generatorConfig.xml
修改文件编码为ANSI编码–》:如将utf-8换成gb2312.