Mybatis(1)反向生成DAO、Mapper、Model

本文介绍如何使用MyBatis Generator自动生成DAO、Mapper及Model等代码文件,并详细展示了generator.xml配置文件的具体设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工程目录结构

现在mybatis generator 最高版本是1.3.5。我使用的是1.3.2版本。
以下书生成代码目录结构:
生成文件所需要的文件
上图是生成DAO、Mapper、Model需要的所有文件

  • generator : 生成出来的代码存放这个目录中
  • generator.xml : 用于生成表的一些核心配置文件、
  • mybatis-generator-core-1.3.2.jar 生成代码需要用的jar文件
  • mysql-connector-java-5.1.34.jar : mysql数据库驱动

下载地址:

https://github.com/mybatis/generator

官方参考:

http://www.mybatis.org/generator/

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>
    <classPathEntry location="mysql-connector-java-5.1.34.jar"/>
    <context id="MysqlTables"   targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否生成注释代时间戳-->
            <property name="suppressDate" value="true"/>
            <!-- 是否取消注释 -->
            <property name="suppressAllComments" value="true"/>
            <!-- 生成的Java文件的编码 -->
            <property name="javaFileEncoding" value="UTF-8"/>
            <!-- 格式化java代码 -->
            <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
            <!-- 格式化XML代码 -->
            <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

        </commentGenerator>

        <!-- jdbc连接 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3308/restaurant" userId="root" password="password"></jdbcConnection>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="cn.com.test.restaurant.model" targetProject="generator">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="true"/>
            <!-- 是否针对string类型的字段在set的时候进行trim调用 --> 
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 --> 
        <sqlMapGenerator targetPackage="cn.com.test.restaurant.mapping" targetProject="generator">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.test.restaurant.dao" targetProject="generator">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> 
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 配置表信息 --> 
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类   -->
        <table tableName="s_log" domainObjectName="SLog" 
            enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!-- 忽略列,不生成bean 字段 
            <ignoreColumn column="FRED" />  -->  
            <!-- 指定列的java数据类型  
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />  --> 
        </table>  
        <table tableName="s_menu" domainObjectName="SMenu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="s_menu_role" domainObjectName="SMenuRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="s_role" domainObjectName="SRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="s_user" domainObjectName="SUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="s_user_role" domainObjectName="SUserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="u_dining_table" domainObjectName="UDiningTable" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="u_integral" domainObjectName="UIntegral" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="u_order" domainObjectName="UOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="u_restaurant" domainObjectName="URestaurant" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

执行命令

D:\tools\MyBatis Generator>java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
MyBatis Generator finished successfully.

如果成功会提示:MyBatis Generator finished successfully
在generator 目录中会看到生成的文件:
dao:
生成的dao代码
mapper:
生成的mapper代码
model
生成的model代码

注意

  1. 需要配置jdk
  2. 启动数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值