SpringBoot中集成mybatis-generator配置

1.引入pom.xml依赖
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.1</version>
 </dependency>
 <dependency>
	<groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
 </dependency>
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.1</version>
 </dependency>
 
<plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.3.7</version>
     <configuration>
	     <!-- mybatis-generator的配置文件,根据情况调整位置 -->
	     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
         <verbose>true</verbose>
         <overwrite>true</overwrite>
     </configuration>
 </plugin>
2.配置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="application.properties"></properties>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
            <property name="nullCatalogMeansCurrent" value="true"/>
            <property name="remarks" value="true"/>
        </jdbcConnection>
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.coderlife.qingxin.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.coderlife.qingxin.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <table tableName="数据表名称" domainObjectName="实体pojo类名"></table>
    </context>
</generatorConfiguration>

其中,properties 引入项目项目启动时数据库配置项,方便修改

<properties resource="application.properties"></properties>

引入插件,RowBoundsPlugin为分页插件,若配置xml为无注释属性,即nullCatalogMeansCurrent =true,则UnmergeableXmlMappersPlugin插件需要被引入,去掉重复代码。

<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

按顺序配置model、mapper.xml以及mapper接口的相应目标工程路径以及包路径

<javaModelGenerator targetPackage="com.coderlife.qingxin.pojo" targetProject="src/main/java">
    <property name="enableSubPackages" value="true" />
    <property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
    <property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.coderlife.qingxin.mapper"
                     targetProject="src/main/java">
    <property name="enableSubPackages" value="true" />
</javaClientGenerator>

最后配置数据库表名及相应实体类名

<table tableName="数据表名称" domainObjectName="实体pojo类名"></table>

由于采用maven管理项目,直接终端使用maven命令运行mybatis-generator脚本,前提是在pom.xml引入插件时配置了configurationFile和overwrite属性

mvn mybatis-generator:generate
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值