Java advanced notes: Spring Boot integrates Mybatis

Keywords: Mybatis MySQL Java Database

Mybatis is an excellent persistence framework that supports customized SQL, stored procedures, and advanced mapping. Mybatis avoids almost all JDBC code and manual setting of parameters and getting result sets. This article introduces the steps of integrating Mybatis with Spring Boot. MYSQL is selected as the database

Create a Spring Boot project

Modify pom.xml

Modify the Spring Boot configuration file

Here, use the configuration file in the format of YML. Change the name of application.properties to application.yml. Please modify the following configuration according to your own needs

Spring Boot will automatically load the application.yml related configuration, the data source will be automatically injected into s qlSessionFactory, and sqlSessionFactory will be automatically injected into Mapper.

Using plug-ins to quickly generate code

Configure pom.xml

Add the mybatis generator plug-in



Write configuration file generatorConfig.xml

In the IntelliJ IDEA development environment, this file needs to be placed in the resource root directory. mybatis generator loads the configuration file of this directory by default. The following files need to be configured according to their own conditions

<?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>

    <!--Database driven jar -->

    <classPathEntry

        location="D:\.m2\repository\mysql\mysql-connector-java\5.1.33\mysql-connector-java-5.1.33.jar" />

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

        <!--Remove notes -->

        <commentGenerator>

            <property name="suppressDate" value="true"/>

            <property name="suppressAllComments" value="true" />

        </commentGenerator>

        <!--Database connection -->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

            connectionURL="jdbc:mysql://129.1.18.18:3306/ssm_demo" userId="root"

            password="root">

        </jdbcConnection>

        <!--default false Java type resolver will always use java.math.BigDecimal if

            the database column is of type DECIMAL or NUMERIC. -->

        <javaTypeResolver>

            <property name="forceBigDecimals" value="false" />

        </javaTypeResolver>

        <!--The build entity class specifies the package name and the generated address (you can customize the address, but the path does not exist and will not be automatically created and used Maven Generated in target Under directory, it will be created automatically.) -->

        <javaModelGenerator targetPackage="com.myapp.api.entity"

                            targetProject="src/main/java">

            <property name="enableSubPackages" value="true" />

            <property name="trimStrings" value="true" />

        </javaModelGenerator>

        <!--generate SQLMAP file -->

        <sqlMapGenerator targetPackage="mapping"

                        targetProject="src/main/resources">

            <property name="enableSubPackages" value="false" />

        </sqlMapGenerator>

        <!-- generate DAO Package name and location for-->

        <!-- XMLMAPPER generate xml Mapping file, ANNOTATEDMAPPER Generated dao Write with comments sql -->

        <javaClientGenerator type="ANNOTATEDMAPPER"

                            targetPackage="com.myapp.api.mapper"

                            targetProject="src/main/java">

            <property name="enableSubPackages" value="false" />

        </javaClientGenerator>

        <!--Corresponding database table mysql You can add a primary key auto increment field name, ignore a field, etc -->

        <table tableName="" domainObjectName=""

            enableCountByExample="false" enableUpdateByExample="false"

            enableDeleteByExample="false" enableSelectByExample="false"

            selectByExampleQueryId="false" />

    </context>

</generatorConfiguration>


Configuring in IntelliJ IDEA

Configuration command mybatis generator: generate - e

Click the next run button after configuration

Of course, you can also find mybatis generator in plugins. Double click to run or right-click to run

Posted by jwmessiah on Sat, 30 Nov 2019 13:34:09 -0800