【Mybatis】MyBatis Generator插件自动生成代码

本文目录

  • 第一步:创建mybatis maven工程
  • 第二步:完善功能目录
  • 第三步:generatorConfig.xml
  • 第四步:运行

使用MyBatis Generator插件能自动生成MyBatis开发所需的Dao、实体类和映射文件,提高开发效率,使用过程如下:

第一步:创建mybatis maven工程

参考【Mybatis】Mybatis开发入门程序创建一个maven工程。

pom.xml的添加dependencies节点下添加

        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.4.0version>
        dependency>

pom.xml的添加plugins节点下添加

<plugin>
    <groupId>org.mybatis.generatorgroupId>
    <artifactId>mybatis-generator-maven-pluginartifactId>
    <version>1.4.0version>
    <configuration>
        
        <configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
        <verbose>trueverbose>
        <overwrite>trueoverwrite>
    configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifactsid>
            <goals>
                <goal>generategoal>
            goals>
        execution>
    executions>
    <dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.20version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.4.0version>
        dependency>
    dependencies>
plugin>

完整的pom.xml文件如下:



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.lucasgroupId>
    <artifactId>generatorartifactId>
    <version>1.0-SNAPSHOTversion>

    <name>generatorname>
    
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.20version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.12version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.4.0version>
        dependency>
    dependencies>

    <build>
        <pluginManagement>
            <plugins>

                
                <plugin>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.1.0version>
                plugin>
                
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.0version>
                plugin>
                <plugin>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                <plugin>
                    <artifactId>maven-jar-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                <plugin>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
                
                <plugin>
                    <artifactId>maven-site-pluginartifactId>
                    <version>3.7.1version>
                plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-pluginartifactId>
                    <version>3.0.0version>
                plugin>
                <plugin>
                    <groupId>org.mybatis.generatorgroupId>
                    <artifactId>mybatis-generator-maven-pluginartifactId>
                    <version>1.4.0version>
                    <configuration>
                        
                        <configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
                        <verbose>trueverbose>
                        <overwrite>trueoverwrite>
                    configuration>
                    <executions>
                        <execution>
                            <id>Generate MyBatis Artifactsid>
                            <goals>
                                <goal>generategoal>
                            goals>
                        execution>
                    executions>
                    <dependencies>
                        <dependency>
                            <groupId>mysqlgroupId>
                            <artifactId>mysql-connector-javaartifactId>
                            <version>8.0.20version>
                        dependency>
                        <dependency>
                            <groupId>org.mybatisgroupId>
                            <artifactId>mybatisartifactId>
                            <version>3.4.6version>
                        dependency>
                        <dependency>
                            <groupId>org.mybatis.generatorgroupId>
                            <artifactId>mybatis-generator-coreartifactId>
                            <version>1.4.0version>
                        dependency>
                    dependencies>
                plugin>
            plugins>
        pluginManagement>
    build>
project>

第二步:完善功能目录

将工程目录和相应文件夹补充完整如下图所示
【Mybatis】MyBatis Generator插件自动生成代码_第1张图片

第三步:generatorConfig.xml

resources文件夹下创建generatorConfig.xml并添加如下配置:



<generatorConfiguration>
    <context id="context" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&useSSL=false&serverTimezone=UTC"
                        userId="root"
                        password="123456">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.lucas.model" targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="trimStrings" value="false"/>
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        
        
        <javaClientGenerator type="MIXEDMAPPER" targetPackage="com.lucas.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>

        
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        table>

    context>
generatorConfiguration>

第四步:运行

点击Edit Configurations
【Mybatis】MyBatis Generator插件自动生成代码_第2张图片
添加一个maven配置
【Mybatis】MyBatis Generator插件自动生成代码_第3张图片

Comand line中添加mybatis-generator:generate -e -X 并点击Apply
【Mybatis】MyBatis Generator插件自动生成代码_第4张图片

【Mybatis】MyBatis Generator插件自动生成代码_第5张图片
运行maven
【Mybatis】MyBatis Generator插件自动生成代码_第6张图片

执行成功并生成了相应的文件
【Mybatis】MyBatis Generator插件自动生成代码_第7张图片
【Mybatis】MyBatis Generator插件自动生成代码_第8张图片

你可能感兴趣的:(JavaEE,#,Mybatis,#,Spring,数据库,mybatis,maven,mysql)