MyBatis逆向工程生成dao、model和mapper

年前,公司准备做招标系统,该系统中包含了项目管理模块、支付模块、公众号模块等等,这个准备工作的时间有点长,一直拖到现在才开始,LZ都等得有点不耐烦了。好了,吐槽完毕,回归正事:在该项目中,我主要负责的是支付模块的开发工作,LZ也从来没有接触相关的支付模块开发,只能一点一滴的做下去,不懂就问。 项目开始搭建也有两三天的时间了,从建库到springboot搭建,再到统一异常处理等等,今天就来说下使用MyBatis逆向工程生成dao、model和mapper。

一、建表

自行建表,不懂直接度娘,本项目中采用Oracle数据库。

二、maven中添加mybatis的相关依赖(以下代码只添加mybatis的依赖,其余springboot相关依赖自行添加)

注意:在resources目录下创建lib包,并将ojdbc6.jar(下载连接:https://www.jb51.net/softs/566175.html)拷贝进去,xml配置文件中添加oracle驱动包需要指定该包路径

<properties>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
        
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>
</properties>

<build>
        <plugins>
            <!--添加mybatis generator maven插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--generatorConfig.xml位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>

                <!--此处必须添加oracle驱动包-->
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.2.0.3</version>
                        <scope>system</scope>
                                         <systemPath>${basedir}/src/main/resources/lib/ojdbc6.jar</systemPath>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

三、在resource目录下创建配置文件generatorConfig.xml

Oracle配置(需要用到ojdbc6.jar包):

<?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>
    <!-- 配置绝对路径指向ojdbc6.jar包 -->
    <classPathEntry
            location="E:\lib\ojdbc6.jar"/>
    <context id="sqlserverTables" targetRuntime="MyBatis3">
        <!-- 生成的pojo,将implements Serializable-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@localhost:1521/orcl"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--对应的mapper.xml文件  -->
        <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 对应的Mapper接口类文件 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.gpdi.ebidding.pay.dao" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 列出要生成代码的所有表 -->
        <table tableName="PAY_BANK_ACCOUNT" domainObjectName="PayBankAccount"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table tableName="PAY_BANK_TRANSACTIONS" domainObjectName="PayBankTransactions"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table tableName="PAY_PAYMENT_LOG" domainObjectName="PayPaymentLog"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table tableName="PAYMENT_ORDER_LOG_INFO" domainObjectName="PaymentOrderLogInfo"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table tableName="PAY_PAYMENT_ORDER_INFO" domainObjectName="PayPaymentOrderInfo"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table tableName="PAY_THIRD_PARTY_PAYMENT_INFO" domainObjectName="PayThirdPartyPaymentInfo"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>
    </context>
</generatorConfiguration>

MySql配置主要将配置文件generatorConfig.xml中的classPathEntry(包下载连接:https://download.csdn.net/download/softtechnology/10013669)、jdbcConnection换成mysql相关的信息即可。

注意:如果数据库(Oracle或Mysql)表中存在字段是clob或text等类型,在mybatis逆向工程中,会生成*WithBLOBs.java代码,并继承entity实体类。

看个人是否需要不生成*WithBLOBs.java类,如果不需要生成,有两种办法:

第一种:修改数据库字段的类型(不用clob或text等类型)即可。

第二种:将*WithBLOBs.java中的属性字段添加到配置文件中(会存在失败的情况,如失败,请用第一种方法),如下所示:

<table tableName="PAYMENT_ORDER_LOG_INFO" domainObjectName="PaymentOrderLogInfo"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>

            <!-- 添加属性,使不生成*WithBLOBs.java类 -->
            <columnOverride column="businessRespJson" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <columnOverride column="businessReqJson" javaType="java.lang.String" jdbcType="VARCHAR"/>

        </table>

四、点击Maven Projects下的mybatis-generator:generate即可生成dao、model和mapper

最终生成的结果图如下:

五、单元测试,即可添加数据到数据库

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class PayBankAccountMapperTest {

    @Autowired(required = true)
    PayBankAccountMapper payBankAccountMapper;

    @Test
    public void paymentOrderLogInfo() {
        PayBankAccount payBankAccount = PayBankAccount.builder()
                .accountId("1")
                .accountName("root")
                .depositBankName("123")
                .bankAccountNo("2")
                .parentAccountId("1")
                .accountType("1")
                .accountProp("1")
                .bankSysCode("1")
                .remark("1")
                .state("1")
                .createTime(new Date())
                .updateTime(new Date()).build();
        payBankAccountMapper.insert(payBankAccount);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Generator 是一个能够根据数据库表信息自动创建 MyBatis 相应 DAO 和 Mapping 文件的工具,也就是逆向工程逆向工程需要通过 XML 配置文件来指定数据库连接信息和生成规则。以下是一个简单的 MyBatis Generator 配置文件示例: ```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="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"> </jdbcConnection> <!-- 配置生成规则 --> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user"></table> </context> </generatorConfiguration> ``` 以上配置文件中: - `<jdbcConnection>` 标签指定了数据库连接信息,包括驱动类、连接地址、用户名和密码。 - `<javaModelGenerator>` 标签用于生成实体类(JavaBean),指定了 JavaBean 生成的包名和存放路径。 - `<sqlMapGenerator>` 标签用于生成 Mapper XML 文件,指定了 Mapper XML 生成的包名和存放路径。 - `<javaClientGenerator>` 标签用于生成 Mapper 接口,指定了 Mapper 接口生成的包名和存放路径。 - `<table>` 标签用于指定要生成的表名。 在使用 MyBatis Generator 生成代码时,只需执行以下命令即可: ```sh java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite ``` 其中 `mybatis-generator-core-1.3.7.jar` 是 MyBatis Generator 工具的 JAR 包,`generatorConfig.xml` 是上述配置文件的路径,`-overwrite` 参数表示覆盖已有文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值