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
    评论
MyBatis Generator 是一个能够根据数据库表信息自动创建 MyBatis 相应 DAO 和 Mapping 文件的工具,也就是逆向工程逆向工程需要通过 XML 配置文件来指定数据库连接信息和生成规则。以下是一个简单的 MyBatis 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> <!-- 配置数据库连接信息 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"> </jdbcConnection> <!-- 配置生成规则 --> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user"></table> </context> </generatorConfiguration> ``` 以上配置文件中: - `<jdbcConnection>` 标签指定了数据库连接信息,包括驱动类、连接地址、用户名和密码。 - `<javaModelGenerator>` 标签用于生成实体类(JavaBean),指定了 JavaBean 生成的包名和存放路径。 - `<sqlMapGenerator>` 标签用于生成 Mapper XML 文件,指定了 Mapper XML 生成的包名和存放路径。 - `<javaClientGenerator>` 标签用于生成 Mapper 接口,指定了 Mapper 接口生成的包名和存放路径。 - `<table>` 标签用于指定要生成的表名。 在使用 MyBatis Generator 生成代码时,只需执行以下命令即可: ```sh java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite ``` 其中 `mybatis-generator-core-1.3.7.jar` 是 MyBatis Generator 工具的 JAR 包,`generatorConfig.xml` 是上述配置文件的路径,`-overwrite` 参数表示覆盖已有文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值