MyBatis-自动生成代码

本文详细介绍了如何使用Mybatis-Generator自动生成Java实体类、SQL映射文件和接口类,包括下载jar包、配置generatorConfig.xml及执行命令的过程。

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

目录

1.Mybatis-Generator

2.生成代码

1.下载Mybatis-Generator的zip压缩包

2.jar包拷贝到自定义文件里

3.新建generatorConfig.xml配置文件

4.运行命令生成代码


1.Mybatis-Generator

可以利用Mybatis-Generator来帮助我们自动生成文件:

(1)自动生成实体类

可以帮助我们针对数据库中的每张表自动生成实体类

 (2)自动生成SQL映射文件

可以帮助我们针对每张表自动生成SQL配置文件,配置文件里已经定义好对于该表的增删改查的SQL以及映射

(3)自动生成接口类

可以帮助我们针对每个SQL配置文件自动生成接口类并关联

2.生成代码

1.下载Mybatis-Generator的zip压缩包

网址:https://github.com/mybatis/generator/releases

获取里面的jar包

同时获取mysql驱动jar包

2.jar包拷贝到自定义文件里

这里的src存放生成的代码

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>
    <classPathEntry location="E:\MybatisGenerator\mysql-connector-java-8.0.28.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
        <property name="suppressDate" value="true"/>
    </commentGenerator>
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3307/pss"
                    userId="root"
                    password="123456">
    </jdbcConnection>
    <javaModelGenerator targetPackage="entity" targetProject="E:\Gener\gener\src">
        <property name="enableSubPackages" value="true" />
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <sqlMapGenerator targetPackage="mapper"  targetProject="E:\Gener\gener\src">
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <javaClientGenerator type="XMLMAPPER" targetPackage="dao"  targetProject="E:\Gener\gener\src">
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
        <table tableName="purchase" domainObjectName="purchase"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
        </table>
        <table tableName="sale" domainObjectName="sale"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
        </table>
      
        <!-- 其他表省略 -->
    </context>
</generatorConfiguration>

jdbcConnection:修改自己的mysql连接

javaClientGenerator:这个换成自己需要生成的表

更改代码保存位置

4.运行命令生成代码

cmd:java -jar mybatis-generator-core-1.4.2.jar -configfile generatorConfig.xml -overwrite

去src看生成结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值