mybatis代码生成

本文介绍如何使用Eclipse插件mybatisGenerator自动生成MyBatis相关代码,包括SQL映射文件及Java模型类等,有效减少代码重复工作。

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

自动生成 mybatis代码,特别是sqlmap文件,可以有效的减少dao等代码的重复劳动。
eclipse中添加插件: mybatis Generator - http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/


添加生成代码的配置文件



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >

<ibatorConfiguration>
<classPathEntry
location="/home/xxx/xxx.war/WEB-INF/lib/mysql-connector-java-5.1.6.jar" />

<ibatorContext id="DB2Tables" targetRuntime="Ibatis2Java5">

<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<!-- 是否生成注释代时间戳
<property name="suppressDate" value="true" />
-->
</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/test" userId="test"
password="123456">
</jdbcConnection>

<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<javaModelGenerator targetPackage="test.model"
targetProject="Test-web">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<sqlMapGenerator targetPackage="test.dao"
targetProject="Test-web">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<!-- 生成DAO的类文件以及配置文件 -->
<daoGenerator targetPackage="test.dao" targetProject="Test-web"
type="GENERIC-CI" />

<!-- 避免生成一大堆Example方法,代码看去清晰一些 -->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">

<!-- 去除一列,这样 PASSWORD这列就不会生成
<ignoreColumn column="PASSWORD"/>
-->
<!-- columnOverride 将数据库字段对应成javabean里面的字段 -->
<columnOverride column="USERID" property="userid" />
<columnOverride column="USERNAME" property="username" />
<columnOverride column="PASSWORD" property="password" />
</table>

</ibatorContext>
</ibatorConfiguration>



在文件上点击鼠标右键,可以看到插件生成的选项,选择即可生成代码

daoGenerator 标签的类型,见如下链接.
http://ibatis.apache.org/docs/tools/abator/configreference/daoGenerator.html

IBATIS The generated DAO objects will conform to the iBATIS DAO framework.
GENERIC-CI The generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by constructor dependency injection.
GENERIC-SI The generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by setter dependency injection.
SPRING The generated DAO objects will conform to the Spring DAO framework.
### MyBatis 代码生成工具及其实现方法 MyBatis 是一种流行的 ORM 框架,其提供了强大的代码生成功能来简化开发流程。以下是关于 MyBatis代码生成工具以及其实现方式的具体说明。 #### 工具介绍 MyBatis 提供了一款名为 **MyBatis Generator (MBG)** 的官方工具[^2],它能够自动生成与数据库表结构对应的 Java 实体类、Mapper 接口及相关 XML 文件。该工具支持多种配置选项,允许开发者灵活调整生成的代码样式以满足项目需求。 #### 配置文件详解 为了使用 MBG 进行代码生成,通常需要创建一个 XML 格式的配置文件。此文件定义了生成过程中的各种参数和规则。以下是一些核心属性的解释: - `context` 定义了一个上下文,用于指定生成代码的一些全局配置和规则。 - `id` 属性指定了上下文的唯一标识符,例如可以设置为 `mysqlTables` 表示针对 MySQL 数据库的操作[^1]。 - `targetRuntime` 属性决定了生成代码的目标运行时环境,常见的值有 `MyBatis3` 和其他特定版本的支持。 - `defaultModelType` 属性设置了生成代码时默认使用的模型类型,比如 `flat` 表示生成扁平化结构的实体类。 #### 使用步骤概述 虽然不允许使用具体步骤词汇描述,但可以通过以下几个方面理解实现逻辑: - 创建并编辑上述提到的 XML 配置文件。 - 利用 Maven 或 Gradle 插件集成到构建过程中,或者单独执行命令启动生成操作。 - 调整模板引擎(如果适用),定制输出代码风格以便更好地融入现有项目架构。 #### 示例代码展示 下面是一个简单的例子演示如何利用 Spring Boot 3.x 结合 MyBatis 来完成自动化代码生成的过程。 ```xml <!-- generatorConfig.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="mysqlTables" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 数据库连接信息省略 --> </context> </generatorConfiguration> ``` 对于基于 Maven 构建的应用程序来说,则可以在 pom.xml 中引入如下依赖项: ```xml <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.2</version> </dependency> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.2</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> ``` 最后,在终端输入 mvn mybatis-generator:generate 即可触发实际的代码生产环节。 #### 总结 借助于 MyBatis 自带的强大功能模块——MyBatis Generator 及其扩展能力,我们可以快速搭建起一套高效的数据访问层解决方案。这不仅减少了手动编写重复性代码的工作量,还提高了整体项目的稳定性和一致性水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值