Maven环境下MyBatisGenerator 配置

Posted 朝圣之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven环境下MyBatisGenerator 配置相关的知识,希望对你有一定的参考价值。

  1. 插件导入; pom文件引入mybatis-generator-maven-plugin
        <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration> <!-- generator 工具配置文件的位置 -->
                        <configurationFile>${basedir}/src/main/resources/mybatisGenerator/generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.16</version>
                        </dependency>
                    </dependencies>
        </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="mybatisGenerator/mybatisGenerator.properties"/>
    
        <context id="lego" targetRuntime="MyBatis3Simple" defaultModelType="flat">
    
            <!-- 注释 -->
            <commentGenerator >
                <!-- 是否取消注释 -->
                <property name="suppressAllComments" value="false"/>
                <!-- 是否生成注释代时间戳-->
                <property name="suppressDate" value="true" />
            </commentGenerator>
    
            <!--数据库连接信息-->
            <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}"
                            userId="${jdbc.userId}" password="${jdbc.password}">
            </jdbcConnection>
    
            <!--类型转换-->
            <javaTypeResolver >
                <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- 生成实体类地址 -->
            <javaModelGenerator targetPackage="${package.do}" targetProject="src/main/java">
                <property name="enableSubPackages" value="false" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!--生成mapper-->
            <sqlMapGenerator targetPackage="${package.mapper}"  targetProject="src/main/resources">
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
    
            <!--dao接口-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="${package.dao}"  targetProject="src/main/java">
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
            <table tableName="lego_user" domainObjectName="UserDO" >
                <!-- 数据库表主键 -->
                <generatedKey column="id" sqlStatement="select last_insert_id()" identity="true" />
            </table>
    
        </context>
    </generatorConfiguration>
  3. generatorConfig.properties 配置(解耦和 ,可忽略)
    #mysql
    jdbc.driverClass=com.mysql.jdbc.Driver
    
    #lego
    jdbc.connectionURL=jdbc:mysql://localhost:3306/lego?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
    jdbc.userId=root
    jdbc.password=123456
    
    #package
    package.do=com.zhangweinan.dao.objects
    package.dao=com.zhangweinan.dao
    package.mapper=mapper

     

    SpringBoot环境下映射关系配置

  1. application.properties文件新增配置
    # mybatis
    mybatis.mapper-locations=classpath:mapper/*.xml
  2. SpringBoot Starter启动类新增注解MapperScan
    @SpringBootApplication
    @MapperScan("com.zhangweinan.dao")
    public class Starter {
    
        public static void main(String[] args) {
            SpringApplication.run(Starter.class, args);
        }
    
    }

     

    tips:mysql-connector-java jar 与 mybatis-generator-maven-plugin 插件 可能会存在冲突问题,因此选择合适的version很重要。配置1中亲测可用

以上是关于Maven环境下MyBatisGenerator 配置的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse下SSM项目的搭建

day01-项目介绍+SSM环境搭建

使用idea建立逆向工程

mac 本地搭建mybatisGenerator代码生成环境

Spring BootMyBatis代码生成

搞定 mybatis generator 三步走