zoukankan      html  css  js  c++  java
  • Mabatis三剑客分别是: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里引入配置

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

    引入generatorConfig.xml 
    generatorConfig.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE generatorConfiguration
    3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    5. <generatorConfiguration>
    6. <!--导入属性配置-->
    7. <properties resource="datasource.properties"></properties>
    8. <!--指定特定数据库的jdbc驱动jar包的位置-->
    9. <classPathEntry location="${db.driverLocation}"/>
    10. <context id="default" targetRuntime="MyBatis3">
    11. <!-- optional,旨在创建class时,对注释进行控制 -->
    12. <commentGenerator>
    13. <property name="suppressDate" value="true"/>
    14. <property name="suppressAllComments" value="true"/>
    15. </commentGenerator>
    16. <!--jdbc的数据库连接 -->
    17. <jdbcConnection
    18. driverClass="${db.driverClassName}"
    19. connectionURL="${db.url}"
    20. userId="${db.username}"
    21. password="${db.password}">
    22. </jdbcConnection>
    23. <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    24. <javaTypeResolver>
    25. <property name="forceBigDecimals" value="false"/>
    26. </javaTypeResolver>
    27. <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
    28. targetPackage 指定生成的model生成所在的包名
    29. targetProject 指定在该项目下所在的路径
    30. -->
    31. <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".srcmainjava">-->
    32. <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
    33. <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
    34. <property name="enableSubPackages" value="false"/>
    35. <!-- 是否对model添加 构造函数 -->
    36. <property name="constructorBased" value="true"/>
    37. <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
    38. <property name="trimStrings" value="true"/>
    39. <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
    40. <property name="immutable" value="false"/>
    41. </javaModelGenerator>
    42. <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
    43. <!--<sqlMapGenerator targetPackage="mappers" targetProject=".srcmain esources">-->
    44. <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
    45. <property name="enableSubPackages" value="false"/>
    46. </sqlMapGenerator>
    47. <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
    48. type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
    49. type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
    50. type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    51. -->
    52. <!-- targetPackage:mapper接口dao生成的位置 -->
    53. <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".srcmainjava">-->
    54. <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
    55. <!-- enableSubPackages:是否让schema作为包的后缀 -->
    56. <property name="enableSubPackages" value="false" />
    57. </javaClientGenerator>
    58. <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    59. <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    60. <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    61. <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    62. <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    63. <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    64. <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    65. <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    66. <columnOverride column="detail" jdbcType="VARCHAR" />
    67. <columnOverride column="sub_images" jdbcType="VARCHAR" />
    68. </table>
    69. <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    70. <!-- mybatis插件的搭建 -->
    71. </context>
    72. </generatorConfiguration>

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

    这里写图片描述

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

    配置完之后,就点击idea的左下角有个小按钮,把右侧的maven project按钮调出来(右侧已有的请忽略) 
    这里写图片描述 
    点击maven project,并选定插件加载(双击就行) 
    这里写图片描述 
    下方的控制台,出现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)(直接跳转到mapper中的sql语句的快捷键是:“Ctrl+ALT+B”)

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

    这里写图片描述

    1、安装插件:

    这里写图片描述 
    然后重启下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图标,点击那个就可以下载别人的项目了 
    这里写图片描述

    (1) windows破解

    首先你要找到mybatis_plus.jar的位置,位置一般在这里 
    C:Usersyouname(你自己的文档).IntelliJIdeaconfigpluginsmybatis_pluslib 
    用winRAR打开 
    这里写图片描述 
    将下载好的破解,与plugin插件压缩包中的文件更换,拖入压缩包,更换就行 
    这里写图片描述

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

    (2)mac破解

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

    find ~ -name "mybatis_plus.jar"

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

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

    看到了2个文件

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

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


    三、Mybatis-pageHelper

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

    1.pom.xml里添加依赖

    1. <dependency>
    2. <groupId>com.github.pagehelper</groupId>
    3. <artifactId>pagehelper</artifactId>
    4. <version>4.1.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.github.miemiedev</groupId>
    8. <artifactId>mybatis-paginator</artifactId>
    9. <version>1.2.17</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>com.github.jsqlparser</groupId>
    13. <artifactId>jsqlparser</artifactId>
    14. <version>0.9.4</version>
    15. </dependency>

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

    1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    2. <property name="dataSource" ref="dataSource"/>
    3. <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
    4. <!-- 分页插件 -->
    5. <property name="plugins">
    6. <array>
    7. <bean class="com.github.pagehelper.PageHelper">
    8. <property name="properties">
    9. <value>
    10. dialect=mysql
    11. </value>
    12. </property>
    13. </bean>
    14. </array>
    15. </property>
    16. </bean>

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

    这样三剑客就都配置OK了

  • 相关阅读:
    1113.层级 Walker
    118.防止高度塌陷 Walker
    1114.实战移入切换 Walker
    116.清除浮动 Walker
    Vue使用——vue设置浏览器显示图标
    MFC如何高效地绘图(转)
    Ugly Windows HDU2487 ACM算法设计
    CImage类的介绍与使用 【图像打开,另存为,转为灰度图像(利用cimage方法实现)】
    Ogre 3D 配置
    WPF文本框只允许输入数字[转]
  • 原文地址:https://www.cnblogs.com/jpfss/p/10462488.html
Copyright © 2011-2022 走看看