2021-10-25

本文介绍了如何通过MyBatis Generator自动化生成Java项目的增删改查代码,包括引入依赖、配置插件、创建generatorConfig.xml以及运行生成。通过配置文件,可以指定数据库连接信息、目标包路径、实体类、Mapper接口等内容,实现快速开发。
摘要由CSDN通过智能技术生成

自动化生成增删改查

第一步:
引入依赖:

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

第二步:
引入plugin

           <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.46</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

`
第三步:创建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>
<!--    <properties resource="mybatis_generator.properties"/>-->

    <context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional">
        <!-- 注意以下标签的顺序:property*,plugin*,commentGenerator?,jdbcConnection,
                javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,
                javaClientGenerator?,table+ -->

        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:-->
            <property name="suppressAllComments" value="true"/>
            <!-- 不希望生成的注释中包含时间戳 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否  自动为每一个生成的类创建一个构造方法-->
            <property name="constructorBased" value="false"/>
        </commentGenerator>
        <!-- 数据库连接 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/umat?characterEncoding=utf-8"
                userId="root"
                password="root">
        </jdbcConnection>
        <!-- 指定生成的类型为java类型,避免数据库中number等类型字段 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成model模型,对应的包,存放位置可以指定具体的路径,/ProjectName/src,也可以使用MAVEN来自动生成 -->
        <javaModelGenerator targetPackage="com.nj3i.umat.pojo" targetProject="src/main/java">
            <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="true"/>
            <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--对应的xml mapper文件  -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 对应的dao接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.nj3i.umat.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 表名对应生成的实体 -->
        <!--        <table tableName="atm_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"-->
        <!--               enableDeleteByExample="false" enableSelectByExample="false"-->
        <!--               selectByExampleQueryId="false">-->
        <!--            <generatedKey column="id" sqlStatement="MySql" identity="true" />-->
        <!--        </table>-->

        <!--        <table tableName="atm_card" domainObjectName="Card" enableCountByExample="false" enableUpdateByExample="false"-->
        <!--               enableDeleteByExample="false" enableSelectByExample="false"-->
        <!--               selectByExampleQueryId="false">-->
        <!--            <generatedKey column="id" sqlStatement="MySql" identity="true" />-->
        <!--        </table>-->

        <!--        <table tableName="atm_flow" domainObjectName="Flow" enableCountByExample="false" enableUpdateByExample="false"-->
        <!--               enableDeleteByExample="false" enableSelectByExample="false"-->
        <!--               selectByExampleQueryId="false">-->
        <!--            <generatedKey column="id" sqlStatement="MySql" identity="true" />-->
        <!--        </table>-->

        <!--        <table tableName="atm_transfer_order" domainObjectName="TransferOrder" enableCountByExample="false" enableUpdateByExample="false"-->
        <!--               enableDeleteByExample="false" enableSelectByExample="false"-->
        <!--               selectByExampleQueryId="false">-->
        <!--            <generatedKey column="id" sqlStatement="MySql" identity="true" />-->
        <!--        </table>-->

        <table tableName="bscan" domainObjectName="Bscan" enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>


    </context>
</generatorConfiguration>

需要修改:在这里插入图片描述

在这里插入图片描述

4.第四步:
运行即可。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值