SpringBoot整合Mybatis实现逆向工程自动生成mapper和bean

公司最近的一个项目需要做数据迁移,历史数据为原数据库导出的excel表格,需要迁移到oracle中。一开始是用POI直接对excel解析,但是遇到一个问题就是在一张excel表数据量过大的时候性能下降的很厉害甚至引起内存溢出的情况,于是想着用Navicate直接把源数据存到mysql中,再对数据库进行操作。

这时就需要根据mysql中的表格建立对应的mapper.xml和POJO类,当然是采用Mybatis官方的逆向工程完成这个任务,不然自己写mapper文件肯定头都大了。(顺带总结一下这个过程中踩过的一些坑)

贴上工程项目结构先~

很简单的结构,因为也就只是用来对导入的数据进行取值而已,其中application.properties的配置很简单:

然后就是generateCongif.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 = "application.properties"/>
    <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
    <classPathEntry location="E:\Downloads\mysql-connector-java-5.1.41.jar" />
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释,true:是;false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="${jdbc.driver}"
                        connectionURL="${jdbc.url}" userId="${jdbc.username}"
                        password="${jdbc.password}">
        </jdbcConnection>
        <!--Oracle数据库的连接信息-->
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
            userId="yycg"
            password="yycg">
        </jdbcConnection> -->

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.example.mybatisdemo.bean"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.example.mybatisdemo.mapper"
                         targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--
        targetPackage:mapper接口生成的位置,遵循MyBatis规范,让mapper.xml
        和mapper.java在同一目录下
         -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.mybatisdemo.mapper"
                             targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="dk"
               domainObjectName="Dk" enableCountByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               enableUpdateByExample="true">
        </table>
        <table tableName="qj"
               domainObjectName="Qj" enableCountByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               enableUpdateByExample="true">
        </table>
        <table tableName="lp"
               domainObjectName="Lp" enableCountByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               enableUpdateByExample="true">
        </table>
        <table tableName="tq"
               domainObjectName="Tq" enableCountByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               enableUpdateByExample="true">
        </table>
        <table tableName="volume"
               domainObjectName="Volume" enableCountByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               enableUpdateByExample="true">
        </table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->

        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

这里要说的第一个坑就是,因为用springBoot时间不长,一直以为application.properties的配置可以直接在项目中用{"jdbc.driver"}这样的格式获取到,但是在运行的时候报错这个属性不存在。所以才在generateCongif.xml中引入了applicaiton.properties

<!-- 引入applicaiton.properties -->
<properties resource = "application.properties"/>

第二个坑是,本以为引入了Mysql的依赖就可以不用加载驱动包,结果发现并不可以。。。于是老老实实的引入了驱动包

 <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
 <classPathEntry location="E:\Downloads\mysql-connector-java-5.1.41.jar" />

第三个坑在网上当时没有找到,但是根据错误提示猜到了一点,那就是新增的generateConfig.xml在springBoot中是没有注入的,直接配置在resource文件下下面好像并不能被springBoot扫描到,所以还是通过注解的方式配置了一下。

package com.example.mybatisdemo;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations = {"classpath:generateConfig.xml"})
public class configClass {

}

这里新建了一个类,通过@Configuration注解和@ImportResource引入外部的xml文件注入到springBoot中,应该有更简便的方法,希望大佬能在评论区告知一下。

这一切准备就绪后就差启动方式了,我用的是maven指令,直接贴图。

运行成功后就能看到你的mapper文件夹和bean文件夹下面有自动生成的代码了~

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值