mybatis逆向工程生成dao、mapper和model组件

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.

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值