Eclipse添加MyBatis Generator

本文介绍如何使用MyBatisGenerator自动生成映射文件、接口及Bean类,包括下载、配置generatorConfig.xml、生成代码等步骤,适用于快速搭建基于MyBatis的数据库操作框架。

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

MyBatis Generator
代码生成器
可以根据指定的表快速生成对应的映射文件,接口,以及Bean类
支持基本的增删改查,以及QBC风格的条件查询
但是一些复杂的表连接还是需要我们自己来去编写

使用步骤:

1、下载:

  • https://github.com/mybatis/generator/releases

2、把相关jar导入到工程当中

3、创建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>
    <!--
    targetRuntime:设置自动生成的版本
    MyBatis3:
    MyBatis3Simple:简单增删改查
    -->
    <!--建议读者将<context id="DB2Tables" targetRuntime="MyBatis3">改为
    <context id="mybatis" targetRuntime="MyBatis3Simple">这样读者更好理解与应用 -->
    <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/mybatis?characterEncoding=utf-8"
                        userId="root"
                        password="1234">
        </jdbcConnection>
        <!--
        配置domain生成策略
        targetProject:把自动生成的domian放在哪个工程里面
        targetPackage:哪个包下
        -->
        <javaModelGenerator targetPackage="com.itlike.domain" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--
            配置mapper的生成策略
            targetPackage:把自动生成的mapper放在哪个工程里面
            targetProject:哪个包下
        -->
        <sqlMapGenerator targetPackage="com.itlike.mapper"  targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--
            mapper接口生成策略
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.itlike.mapper"  targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <table tableName="customer" domainObjectName="Customer" ></table>
        <table tableName="teacher" domainObjectName="Teacher" ></table>
        <table tableName="student" domainObjectName="Student" ></table>
    </context>
</generatorConfiguration>

4、编写生成代码

 List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("./src/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值