Mybatis利用配置文件自动生成实体以及简单CRUD方法

使用mybatis配置文件自动生成实体及方法需要引入的jar包(我这边是springboot + mybatis的整合)

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<groupId>com.miaoshaproject</groupId>
<artifactId>miaosha</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.41</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>

</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.41</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>mybatis generator</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允许移动生成的文件-->
                    <verbose>true</verbose>
                    <!--允许自动覆盖文件-->
                    <overwrite>true</overwrite>
                    <!--配置文件的放置路劲-->
                    <configurationFile>
                         src/main/resources/mybatis-generator.xml
                    </configurationFile>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Mybatis利用配置文件自动生成实体以及简单CRUD方法Mybatis利用配置文件自动生成实体以及简单CRUD方法Mybatis利用配置文件自动生成实体以及简单CRUD方法

接下来这里就需要在resources里面新建一个mybatis-generator.xml ,mybatis官方也有提供,按自己需要的方式修改就可以,下面是我自己的

<?xml version="1.0" encoding="UTF-8"?>
<context id="DB2Tables" targetRuntime="MyBatis3">
    <!--对应数据库的名字-->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha"
                    userId="root"
                    password="root">
    </jdbcConnection>

    <!--根据数据库对应字段生成实体放的位置-->
    <javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
        <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!--根据数据库的对应字段,生成mapping文件-->
    <sqlMapGenerator targetPackage="mapping"  targetProject="src/main/resources">
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!--根据数据库的对应字段,生成dao接口方法-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao"  targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!--对应的数据库名字,我这里是在mysql数据库中建了user_info , user_password两张表,
        domainObjectName:对应的是想要生成实体类的名字;
        enableCountByExample:false 。。。这些作用是在mapping中不生成对应的方法,
        比如:enableDeleteByExample:false,表示不生成deleteByExample这个方法;
    -->
    <table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
    enableUpdateByExample="false" enableDeleteByExample="false"
    enableSelectByExample="false" selectByExampleQueryId="false"></table>

    <table tableName="user_password" domainObjectName="UserPasswordDO"
           enableCountByExample="false"
           enableUpdateByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>

Mybatis利用配置文件自动生成实体以及简单CRUD方法需要注意的是自己对应需要生成的包路劲不要搞错了,第一次我自己也是多加了其他的,还有路径不对导致生成失败;

Mybatis利用配置文件自动生成实体以及简单CRUD方法这些操作完成后,idea点击run–>Edit Configurations

Mybatis利用配置文件自动生成实体以及简单CRUD方法

完成后点击run就可以生成想要的文件和配置啦

Mybatis利用配置文件自动生成实体以及简单CRUD方法
以上是自己个人的操作实践,如果有问题希望各位大佬及时指正,java从入门到放弃,成长的道路需要各位的指正;