[梦中一夜下江南总结]SpringBoot整合mybatis的mapper生成器

  • Post author:
  • Post category:其他




工作总结


mapper生成器的GIthub地址直接点击



第一步,在你的porm.xml文件里面替换

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

替换为

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>



第二步在启动类上加上包扫描器MapperScan

@MapperScan(basePackages = "com.liuyun")
@SpringBootApplication
public class UserCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserCenterApplication.class, args);
    }
}



配置数据源application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/user_center
    hikari:
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver



第三步在porm.xml配置插件<plugins>父节点下

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.18</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>



编写resources\generator\generatorConfig.xml文件

<!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="generator/config.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
        </plugin>

        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.liuyun.usercenter.domain.entity.${moduleName}"
                            targetProject="src/main/java"/>

        <sqlMapGenerator targetPackage="com.liuyun.usercenter.dao.${moduleName}"
                         targetProject="src/main/resources"/>

        <javaClientGenerator targetPackage="com.liuyun.usercenter.dao.${moduleName}"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <table tableName="${tableName}">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>



再进行编写配置generator\config.properties文件 localhost 需要改一下

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user_center
jdbc.user=root
jdbc.password=123456

moduleName=user
tableName=user



这里的前提是你必须有一个对应数据库和一些表



点击右侧的maven插件里面的mybatis-generator:generate开始生成。



其他问题

同名表的问题url上面加上

nullCatalogMeansCurrent=true


Lombok高效插件



porm.xml 添加

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>



generatorConfig.xml 文件添加

<property name="lombok" value="Getter,Setter,ToString"/>



忠告配置完注解,记得build一下。



这里讲的是maven加速

C:\Users\Administrator.m2\settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
    <mirrors>
        <mirror>
            <id>central</id>
            <name>central</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>



idea 启用settings.xml



版权声明:本文为v2810769原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。