eclipse使用mybatis generator自动生成代码

本文介绍如何使用MyBatis Generator插件自动生成基于MyBatis框架的Java代码,包括实体类、Mapper接口及XML映射文件等。通过详细步骤说明,帮助开发者快速搭建开发环境并实现代码自动生成。

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

准备工作

1. 下载mybatis generator插件:https://dl.bintray.com/mybatis/mybatis-generator/zipped/

2. 解压zip包,并将其中的features和plugin放到eclipse的dropins下

3. 重启eclipse,右键New--》other 出现如下选项则证明安装成功

使用插件生成代码

1. 新建一个名为test的Dynamic Web Project项目,引入Oracle数据库驱动jar包:ojdbc14.jar

2.在项目名称上右键—》new—》MyBatis Generator Configuration File,生成如下配置文件:

3.对配置文件进行配置

<?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="D:\generator\mysql-connector-java-5.1.34.jar" /> -->    
    <classPathEntry location="D:\Workspace\Eclipse\test\WebContent\WEB-INF\lib\ojdbc14.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressAllComments" value="true" />  
        </commentGenerator>  
        <!-- 数据库链接URL、用户名、密码 -->  
         <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/my_db?characterEncoding=utf8" userId="root" password="123456"> -->   
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="scott" password="tiger"> 
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="com.test.model" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator>  
        <!-- 生成的映射文件包名和位置 -->  
        <sqlMapGenerator targetPackage="com.test.mapping" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->  
        <table tableName="admin_division" domainObjectName="AdminDivision" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->  
    </context>  
</generatorConfiguration>

4. 生成代码

在generatorConfig.xml上右键—》Run As—》Run Mybatis Generator,生成如下相应代码








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值