MyBatis自动生成Dao层

MyBatis自动生成Dao层

MyBatis自动生成Dao层,从数据库的表映射到Java的数据层。包括 Mapper接口的定义,Mapper文件中的sql脚本以及接口中用到的对象

参考地址:

http://mybatis.org/generator/running/runningWithMaven.html

http://mybatis.org/generator/configreference/xmlconfig.html

新建Maven项目(我的是基于SpringBoot)

1.配置pom.xml  主要添加build那一块

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.imodule.product</groupId>
    <artifactId>product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>product</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <imodule-nexus.version>0.0.1-SNAPSHOT</imodule-nexus.version>
        <quartz.version>2.3.0</quartz.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>jul-to-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-to-slf4j</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!-- mybatis-generator -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!-- mybatis-generator的配置文件,根据情况调整位置 -->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.4.0</version>
                    </dependency>
                </dependencies>

            </plugin>
        </plugins>
    </build>
</project>

  

2.生成dao的配置文件 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>


    <!--JDBC驱动jar包的位置-->
    <classPathEntry location="D:/app/maven/repository/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar"/>

    <context id="default" targetRuntime="MyBatis3">

        <!--创建Java类时是否取消生成注释-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--JDBC数据库连接-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://10.22.33.86:4000/test1?characterEncoding=utf8"
                        userId="root"
                        password="111">
        </jdbcConnection>

        <!--
        Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
        targetPackage     指定生成的model生成所在的包名
        targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.imodule.product.generated.pojo" targetProject="./src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!-- mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="generator"
                         targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!--
        客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
        type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
        type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
        type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.imodule.product.generated.mapper"
                             targetProject="./src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--tables-->
        <table tableName="PRD_BASE" domainObjectName="PrdBase"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <table tableName="prd_property" domainObjectName="PrdProperty"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

 

生成步骤:

IDEA- 点击Maven - Plugins - 选中 Mybatis-generator 双击即可

控制台输出日志:

如果出现以下类似错误

[WARNING] Table configuration with catalog null, schema null, and table XXX did not resolve to any tables

请注意表明的大小写是否与数据库一致 ,我将表名改为大写就都欧克啦!!!

 生成前的目录结构:

生成后的目录 (红色框框标记处均为自动生成的)

到这里就欧克啦,后期要使用的时候  maven 打包一下即可啦,然后在别的项目直接引用即可!

这里有个不好的点是所有的表要一一配置,我再康康有没有啥更快捷的办法,能对整个数据库自动生成的哈哈哈哈 找到了我再来更新~

猜你喜欢

转载自www.cnblogs.com/DFX339/p/12568360.html