Home  >  Article  >  Database  >  MyBatis reverse engineering analysis and simple tutorial (with code)

MyBatis reverse engineering analysis and simple tutorial (with code)

php是最好的语言
php是最好的语言Original
2018-08-02 14:11:451935browse

MyBatis's reverse engineering

1: What is reverse engineering.

MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

当数据库表比较多的时候,重复的创建pojo对象和简单的数据库表的(CRUD)操作的mapper,效率低,官方给出了使用mybatis Generator用来根据数据库表逆向生成pojo和mapper文件,极大的方便开发。

2: Simple tutorial

  • Add plug-in to maven’s pom.xml

 <packaging>jar</packaging>
<dependencies>

        <!-- 添加对mybatis的依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- mybatis逆向工程 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Create the configuration file 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>
    <!--引入数据库配置文件方便修改-->
    <properties resource="jdbc.properties"/>
    <!--数据库驱动文件  需要修改 TODO-->
    <classPathEntry
            location="/Volumes/Tool/JAVA_Tool/maven/Maven_Repository/mysql/mysql-connector-java/5.1.32/mysql-connector-java-5.1.32.jar"/>

    <context id="context" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <!-- 是否去除自动生成的注释,true:是;false:否 -->
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="false"/>
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="${jdbc.driverClassName}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.username}"
                        password="${jdbc.password}"/>
        <!--默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,-->
        <!--为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成PO类的位置 配置生成的实体包-->
        <!--targetPackage:生成的实体包位置,默认存放在src目录下-->
        <!--targetProject:相对路径  路径拼接结果 src/main/java/one/domain -->
        <javaModelGenerator targetPackage="one.domain" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 同上 -->
        <sqlMapGenerator targetPackage="MapperXml" targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--targetPackage:mapper接口生成的位置-->
        <javaClientGenerator targetPackage="one.mapper" type="XMLMAPPER" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--配置表-->
        <!--schema:不用填写-->
        <!--tableName: 表名 TODO-->
        <!--enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:-->
        <!--去除自动生成的注释例子-->
        <table tableName="tb_user" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>
  • Run the Generator class to generate

I have already completed the project and downloaded it You can run

Related articles:

Getting started with MyBatis (7)---Reverse Engineering

Spring SpringMVC MyBatis in-depth learning and construction ——MyBatis reverse engineering

Related videos:

MySQLi process-oriented quick introduction

The above is the detailed content of MyBatis reverse engineering analysis and simple tutorial (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn