mybatis-generator使用Maven Plugin生成mapper

一开始我是打算用继承通用mapper来解决用手打(或者复制)一系列mapper重复代码的繁琐问题,后来在操作过程中发现bug太多而且填完一个坑又出现另一个坑最后放弃了通用mapper。

现在改成用mybatis-generator来自动生成mapper相关代码,还是挺好用的。


一、新建一个maven项目,勾上mysql与mybatis

mybatis-generator使用Maven Plugin生成mapper_第1张图片

2、把pom.xml的自动生成的原插件替换以下


<plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>

<plugins>

            
            
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.2version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                    
                    
                    
                    
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.21version>
                    dependency>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.2version>
                    dependency>
                    <dependency>
                        <groupId>org.mybatisgroupId>
                        <artifactId>mybatisartifactId>
                        <version>3.4.0version>
                    dependency>
                dependencies>
            plugin>
        plugins>

二、在application.properties中加入数据库的配置

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


三、在resources下建立一个generatorConfig.xml,里头的注释写的很详细了




<generatorConfiguration>
    
    
    
    <context id="my" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
                        password="root"/>
        
        <javaModelGenerator targetPackage="com.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>
        
        <javaClientGenerator targetPackage="com.mapper"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

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

    context>
generatorConfiguration>

四、双击ALT键,弹出maven,如图点击即可自动生成(你也可以用命令行来操作maven)

mybatis-generator使用Maven Plugin生成mapper_第2张图片

成功后可以看到所有东西都帮你建好了。
mybatis-generator使用Maven Plugin生成mapper_第3张图片

你再去把mapper不需要的方法删除即可。

你可能感兴趣的:(springboot)