SpringBoot集成mybatis generator

Published on 2020-12-20 21:19 in 分类: 博客 with 狂盗一枝梅
分类: 博客

第一步,加入依赖

加入maven依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

第二步,配置maven plugin

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

第三步,配置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>
    <classPathEntry location="C:\Users\kdyzm\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
    <context id="mybatis-generator-demo" targetRuntime="MyBatis3">

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/company"
                        userId="root" password="root" />

        <javaModelGenerator targetPackage="com.kdyzm.companymanagement.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
            <!--<property name="rootClass" value="com.test.model.BaseModel"/>-->
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.kdyzm.companymanagement.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <table tableName="company" domainObjectName="Company">
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

第四步,运行命令,生成mapper xml文件和实体类等

mvn mybatis-generator:generate

第五步,创建BaseMapper类


public interface MybatisBaseMapper<T, P, E> {

    int countByExample(E example);

    int deleteByExample(E example);

    int deleteByPrimaryKey(P id);

    int insert(T record);

    int insertSelective(T record);

    List<T> selectByExample(E example);

    T selectByPrimaryKey(P id);

    int updateByExampleSelective(@Param("record") T record, @Param("example") E example);

    int updateByExample(@Param("record") T record, @Param("example") E example);

    int updateByPrimaryKeySelective(T record);

    int updateByPrimaryKey(T record);
}

第六步,创建Mapper类

public interface CompanyMapper extends MybatisBaseMapper<Company,Integer, CompanyExample>

做完以上步骤使用的时候会报错,因为还漏了一步:

最后一步,配置pom文件,打包的时候不能漏了.xml文件

<resources>
    <resource>
        <directory>src/main/java</directory>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.*</include>
        </includes>
    </resource>
    <resource>
        <directory>${project.basedir}</directory>
        <targetPath>${project.build.directory}</targetPath>
        <includes>
            <include>*.properties</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>

其中

<resource>
    <directory>src/main/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
</resource>

是关键。

完毕


#java #mybatis #maven
目录