Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件

Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件

文章目录

    • Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件
      • 1.引入依赖
      • 2.mybatis-generator-config.xml插件配置文件
      • 3.设置启动器并执行

事先准备:先把数据库的表都设计好

1.引入依赖

dependencies和plugin下,需要添加依赖和配置如下所示

    <dependencies> 
        //....其他依赖

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
           <groupId>mysqlgroupId>
           <artifactId>mysql-connector-javaartifactId>
           <version>5.1.40version>
         dependency>
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.7version>
        dependency>
    dependencies>


    <build>
        <plugins>
            <plugin>
                
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>
                <configuration>
                    
                    <configurationFile>src/main/resources/mybatis-generator-config.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>5.1.40version>
                    dependency>
                    
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.7version>
                    dependency>
                dependencies>
            plugin>
        plugins>

2.mybatis-generator-config.xml插件配置文件

主要有三个地方需要自行配置

  • 数据库的相关配置 :jdbcConnection标签下的内容 驱动类,连接,用户名密码
  • 各类文件的生成位置配置:javaModelGenerator标签、sqlMapGenerator标签、sqlMapGenerator标签、javaClientGenerator标签下的内容,指定生成路径和包名
  • 相关表的配置: table标签下的内容,指定需要生成的方法,enable开头的设置项


<generatorConfiguration>
    <context id="context" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="org.mysql.Driver" connectionURL="jdbc:mysql://ip/dbname?useUnicode=true;characterEncoding=utf-8" userId="username" password="password"/>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.piao.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>
        
        <javaClientGenerator targetPackage="com.piao.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>
        
        <table tableName="factor_result" domainObjectName="FactorResult"  enableInsert="true" enableDeleteByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableSelectByPrimaryKey="true" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table tableName="factor_weight" domainObjectName="FactorWeight" enableInsert="true" enableDeleteByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableSelectByPrimaryKey="true" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table tableName="factor_score" domainObjectName="FactorScore" enableInsert="true" enableDeleteByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableSelectByPrimaryKey="true" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table tableName="extra_factor_score" domainObjectName="ExtraFactorScore" enableInsert="true" enableDeleteByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableSelectByPrimaryKey="true" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
    context>
generatorConfiguration>

3.设置启动器并执行

新建一个启动器,如下图所示,入口为执行的绿色三角左侧,选择Edit Configurations…

Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件_第1张图片

加号、选择maven、设置名称和指令

mybatis-generator:generate -e

Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件_第2张图片

执行,执行时控制台信息如下,文件就自动生成了

[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ storm_score ---
[INFO] Connecting to the Database
[INFO] Introspecting table factor_result
[INFO] Introspecting table factor_weight
[INFO] Introspecting table factor_score
[INFO] Introspecting table extra_factor_score
[INFO] Generating Record class for table factor_result
[INFO] Generating Mapper Interface for table factor_result
[INFO] Generating SQL Map for table factor_result
[INFO] Generating Record class for table factor_weight
[INFO] Generating Mapper Interface for table factor_weight
[INFO] Generating SQL Map for table factor_weight
[INFO] Generating Record class for table factor_score
[INFO] Generating Mapper Interface for table factor_score
[INFO] Generating SQL Map for table factor_score
[INFO] Generating Record class for table extra_factor_score
[INFO] Generating Mapper Interface for table extra_factor_score
[INFO] Generating SQL Map for table extra_factor_score
[INFO] Saving file FactorResultMapper.xml
[INFO] Saving file FactorWeightMapper.xml
[INFO] Saving file FactorScoreMapper.xml
[INFO] Saving file ExtraFactorScoreMapper.xml
[INFO] Saving file FactorResult.java
[INFO] Saving file FactorResultMapper.java
[INFO] Saving file FactorWeight.java
[INFO] Saving file FactorWeightMapper.java
[INFO] Saving file FactorScore.java
[INFO] Saving file FactorScoreMapper.java
[INFO] Saving file ExtraFactorScore.java
[INFO] Saving file ExtraFactorScoreMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.603 s
[INFO] Finished at: 2021-03-23T15:07:23+08:00
[INFO] ------------------------------------------------------------------------

Mybatis笔记(六) ——Mybatis Generator自动生成entity实体类/mapper接口/xml文件_第3张图片

你可能感兴趣的:(#,MyBatis,mybatis,自动生成)