Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解

本文用的是maven,idea15

Mabatis三剑客分别是:mybatis-generator、mybatis-plugin、mybatis-pagehelper

一、mybatis-generator

根据我们的数据库自动生成pojo、dao和xml文件 
pojo里面放的是跟数据库字段一一对应的对象、dao层是接口,供service使用,xml是这个dao层接口的实现,sql语句都写在xml里

1.引入mabatis-generator

pom.xml里引入配置

<plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.7source>
          <target>1.7target>
          <encoding>UTF-8encoding>
          <compilerArguments>
            <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib/extdirs>
          compilerArguments>
        configuration>
plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

引入generatorConfig.xml 
generatorConfig.xml




<generatorConfiguration>
    
    <properties resource="datasource.properties">properties>

    
    <classPathEntry location="${db.driverLocation}"/>

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

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

        
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        jdbcConnection>


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


        
        
        <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="constructorBased" value="true"/>
            
            <property name="trimStrings" value="true"/>
            
            <property name="immutable" value="false"/>
        javaModelGenerator>

        
        
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        

        
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
            
            <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>


        
    context>
generatorConfiguration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

2.新建datasource.properties文件,配置数据库连接信息(这里配置本地数据库)

Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第1张图片

db.driverLocation=F:/IdeaProjects/mmall/src/main/tool/mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mmall?useUnicode=true&characterEncoding=UTF-8
db.username=root
db.password=940724
  • 1
  • 2
  • 3
  • 4
  • 5

配置完之后,就点击idea的左下角有个小按钮,把右侧的maven project按钮调出来(右侧已有的请忽略) 
Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第2张图片 
点击maven project,并选定插件加载(双击就行) 
Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第3张图片 
下方的控制台,出现build success就是成功了,就会发现dao的包和pojo包会生成好了接口和数据对象实体类,以及生成一个mapper文件夹,里面存储着数据库里各个实体的xml文件

注意mapper里生成的文件夹,我在创建的时候把时间戳给加里了,如果要完美的用的话,需要把时间戳优化一下

把insert标签下的#{createTime,jdbcType=TIMESTAMP}和#{updateTime,jdbcType=TIMESTAMP}改成now()

把update标签下的#{updateTime,jdbcType=TIMESTAMP}改成now()

这个now()方法是数据库自带的函数,表示现在的时间


二、mybatis-plugin

我用的是idea15,如果其他版本不好用的,可以换成idea 15 
这是一个能够追踪dao接口和mapper文件里xml的一个插件

  • 提供Mapper接口与配置文件中对应SQL的导航

  • 提供Mapper接口与配置文件中对应SQL的导航

  • 编辑XML文件时自动补全

  • 根据Mapper接口, 使用快捷键生成xml文件及SQL标签

  • ResultMap中的property支持自动补全,支持级联(属性A.属性B.属性C)

  • 快捷键生成@Param注解

  • XML中编辑SQL时, 括号自动补全

  • XML中编辑SQL时, 支持参数自动补全(基于@Param注解识别参数)

  • 自动检查Mapper XML文件中ID冲突

  • 自动检查Mapper XML文件中错误的属性值

  • 支持Find Usage

  • 支持重构从命名

  • 支持别名

  • 自动生成ResultMap属性

  • 快捷键: Option + Enter(Mac) | Alt + Enter(Windows)

效果:点击dao可以追踪到xml,,同理点击xml的sql右边小箭头,可以追踪到dao方法

Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第4张图片

1、安装插件:

Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第5张图片 
然后重启下idea

2.mybatis-plugin插件破解

到这个网址去 
https://github.com/myoss/profile/tree/master/idea/plugin/MybatisPlugin 
你看到了一个Git仓库,先clone下来,不会clone的down下来,记住路径 
接下来,你看到一堆版本,选择你安装的mybatis_plugin版本号,比如我的是IDEA15版本的,我安装版本就是v2.64,进入v2.64你会看到一个com文件夹,牢牢记住这个文件夹有大作用

点击如图所示的右上角的fork,登录自己的github,登录之后,会发现右边有个绿色的Dowload图标,点击那个就可以下载别人的项目了 
Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第6张图片

(1) windows破解

首先你要找到mybatis_plus.jar的位置,位置一般在这里 
C:\Users\youname(你自己的文档).IntelliJIdea\config\plugins\mybatis_plus\lib 
用winRAR打开 
Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第7张图片 
将下载好的破解,与plugin插件压缩包中的文件更换,拖入压缩包,更换就行 
Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解_第8张图片

重启你的IDEA,完毕,破解搞定

(2)mac破解

使用find命令在你的用户目录下查找mybatis_plus.jar这个文件

find ~ -name "mybatis_plus.jar"
  • 1

OK,拿到一个地址,然后进去

cd /Users/XXXXX/Library/Application Support/IntelliJIdea15/mybatis_plus/lib
  • 1

看到了2个文件

#创建一个文件夹
mkdir m
#进去 
cd m
#拷贝到m文件夹中 
cp ../mybatis_plus.jar .
#解压jar包
jar xf mybatis_plus.jar 
#复制com文件夹到这里   路径根据你情况而定,版本号也根据你情况而定
cp -r ~/Workspace/github/mybatis_plus/idea/plugin/MybatisPlugin/v2.7\~v2.83/com .
#重新打为jar包
jar cf mybatis_plus.jar *
#复制到m的上层目录
cp mybatis_plus.jar ../
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

重启你的IDEA,完毕,破解搞定


三、Mybatis-pageHelper

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

1.pom.xml里添加依赖

<dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>4.1.0version>
    dependency>

    <dependency>
      <groupId>com.github.miemiedevgroupId>
      <artifactId>mybatis-paginatorartifactId>
      <version>1.2.17version>
    dependency>

    <dependency>
      <groupId>com.github.jsqlparsergroupId>
      <artifactId>jsqlparserartifactId>
      <version>0.9.4version>
    dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.在spring配置文件里添加配置

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>

        
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                        value>
                    property>
                bean>
            array>
        property>

    bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注意不同数据库的方言的使用

这样三剑客就都配置OK了

你可能感兴趣的:(技术了解)