Intellij IDEA 2017 integrates MyBatis Three Musketeers

MyBatis Three Musketeers refer to: MyBatis-Generate, Mybatis Plus, MyBatis-PageHelper


MyBatis-Generate

Use the Mybatis Generator maven plugin to quickly generate Dao classes, mapper configuration files and Model classes.

MyBatis Generator (MBG for short) is a code generator for MyBatis. It can automatically query all tables in the database , and then generate basic object types that can access the tables. It solves some simple CRUD operations that have the greatest impact on database operations. However, handwritten SQL statements and objects are still required for union queries and stored procedures.

MyBatis Generator Chinese Documentation

1. Add the plugin to the pom file

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

2. Create an xml file and a properties resource file in the resource in the maven project 

- The name can be chosen arbitrarily, here it is named generatorConfig.xml 
- The resource file is datasource.properties file, this is not necessary, it is used here because it is convenient for management

3. Configure generatorConfig.xml and resource files

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "file:///Users/mac/Documents/work/tools/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--Import property configuration-->
    <properties resource="datasource.properties"></properties>

    <!--Specify the location of the jdbc driver jar package for a specific database-->
    <classPathEntry location="${db.driverLocation}"/>

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

        <!-- optional, designed to control annotations when creating a class -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc database connection-->
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        </jdbcConnection>


        <!-- optional, type handler, conversion control between database types and java types -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model model generator, used to generate classes with primary keys, record classes and query Example classes
            targetPackage specifies the package name where the generated model is generated
            targetProject specifies the path under the project
        -->
        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
        <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
            <!-- Whether to allow subpackages, ie targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- Whether to add a constructor to the model -->
            <property name="constructorBased" value="true"/>
            <!-- Whether to trim the data of the CHAR-like column -->
            <property name="trimStrings" value="true"/>
            <!-- Whether the created Model object is immutable, that is, the generated Model object will not have a setter method, only a constructor method -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--The directory where the mapper mapping file is generated generates the corresponding SqlMap file for each database table -->
        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- client-side code that generates easy-to-use code for Model objects and XML configuration files
                type="ANNOTATEDMAPPER", generate Java Model and annotation-based Mapper objects
                type="MIXEDMAPPER", generate annotation-based Java Model and corresponding Mapper object
                type="XMLMAPPER", generate SQLMap XML file and independent Mapper interface
        -->

        <!-- targetPackage: The location where the mapper interface dao is generated -->
        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
            <!-- enableSubPackages: Whether to use schema as the suffix of the package -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>


        <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <columnOverride column="detail" jdbcType="VARCHAR" />
            <columnOverride column="sub_images" jdbcType="VARCHAR" />
        </table>
        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>


        <!-- The construction of the geelynote mybatis plugin-->
    </context>
</generatorConfiguration>

datasource.properties

db.driverLocation=/Users/mac/Documents/work/mysqljar/mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/mmall?characterEncoding=utf-8db.username=root
db.password=123456
db.initialSize = 20
db.maxActive = 50
db.maxIdle = 20
db.minIdle = 10
db.maxWait = 10
db.defaultAutoCommit = true
db.minEvictableIdleTimeMillis = 3600000

4. Operation 
- method: 
 There is an icon in the lower left corner of the idea, as shown in the figure:

 

 Click the icon to bring up the Maven Projects window on the right, which already exists on the right and can be ignored. Then select the plugin and double-click it:


 
The console below shows the build success is successful. You will find that the packages of dao and pojo generate interface and data object entity classes and the contents of the mapper folder.

Note: Timestamps in mapper can be optimized according to your needs.

eg:

Change #{createTime,jdbcType=TIMESTAMP} and #{updateTime,jdbcType=TIMESTAMP} under the insert tag to now()

Change #{updateTime,jdbcType=TIMESTAMP} under the update tag to now()

The now() method is a function that comes with the database, indicating the current time

 

Mybatis-Plus

Mybatis Plus (MP for short) is an enhancement tool for Mybatis. It only enhances and does not change on the basis of Mybatis. It is born to simplify development and improve efficiency. 
Mybatis Plus Chinese Documentation

1. Function

  • Provides the Mapper interface and the navigation corresponding to SQL in the configuration file
  • Autocompletion when editing XML files
  • According to the Mapper interface, use shortcut keys to generate xml files and SQL tags
  • The property in ResultMap supports auto-completion and cascading (property A.property B.property C)
  • Shortcut keys generate @Param annotations
  • When editing SQL in XML, parentheses are automatically completed
  • When editing SQL in XML, parameter auto-completion is supported (parameter identification based on @Param annotation)
  • Automatically check for ID conflicts in Mapper XML files
  • Automatically check for incorrect attribute values ​​in Mapper XML files
  • Support Find Usage
  • Support for refactoring from naming
  • Aliases are supported
  • Automatically generate ResultMap properties
  • Shortcut: Option + Enter (Mac) | Alt + Enter (Windows)

2. Installation and cracking

  • This is an IDE plugin, which is currently charged, here I use Intellij IDEA 
    Install
  • 装完之后把Intellij IDEA关了,然后打开下面的链接跟着步骤来就可以大功告成 
  • 想安装正版但是破解好像是没有成功。以后再补充。

Mybatis-pageHelper

是一个开源的分页插件(如下网址有插件的全介绍) 
https://github.com/pagehelper/Mybatis-PageHelper 
它的原理,是通过spring的AOP来实现的,这个插件能在执行sql的时候,把相关的数据再执行一次

git上详细介绍了这个开源插件支持的DB,使用方法,在不同的Spring版本及Spring Boot中的配置。具体的可以自行研究。

 1:在pom.xml中添加依赖:

<!-- mybatis pager -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>com.github.miemiedev</groupId>
    <artifactId>mybatis-paginator</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.4</version>
</dependency>

 2:在spring配置文件中添加配置:

     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>
<!-- 分页插件 -->
<property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <value>
dialect=mysql
                    </value>
                </property>
            </bean>
        </array>
    </property>
</bean>

   

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326170809&siteId=291194637
Recommended