MyBatis三剑客

一、引言

在使用mybatis框架操作底层数据时,经常会涉及到mapper.xml文件的编写,查询结果分页,当你的xml文件方法非常多时,dao接口中的方法和xml文件中的方法对应起来就比较麻烦,而三剑客正是解决这几个问题的好手。

二、mybatis-generator

   mybatis-generator是一个能快速生成xml、dao接口、实体类的代码生成器。 具体使用方法如下:

①引入相关依赖和插件:

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>

 

            <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
                <configuration>
                    <!--是否覆盖(generator产生新的文件时,是否覆盖原来已经生成过的文件)-->
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

②在resource目录下新建文件配置文件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>
    <!--配置Mysql驱动jar包位置-->
    <classPathEntry location="D:\study computer\Git\studyShopDoubleSystem\mysql-connector-java-5.1.6.jar"/>

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

        <!--        不再追加xml内容-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>

        <!--取消字段生成后的注释,使代码简洁。不然注释会特别多-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- mysql数据库连接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mall?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;tinyInt1isBit=false"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <!--配置java类型处理器(默认为false)-->
        <!--
            是否忽略BigDecimals
            自动生成Java对象的时候,会根据number类型的长度不同生成不同的数据类型
                    number长度   Java类型
                    1~4          Short
                    5~9          Integer
                    10~18        Long
                    18+          BigDecimal
         -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.imooc.mall.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <!--除去字段空格,在Getter和Setter方法中调用trim()方法-->
            <!--<property name="trimStrings" value="true" 配置从数据库返回的值去除前后空格 />-->
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--配置java mapper接口生成的位置(interface)-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mall.dao" targetProject="src/main/java">
            <!-- 是否让schema作为包后缀 -->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--
            指定数据库表
                tableName数据库表名
                domainObjectName生成的实体类名
                是否需要mapper配置文件加入sql的where条件查询,需要将enableCountByExample等设为true,会生成一个对应domainObjectName的Example类
         -->
        <table tableName="mall_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

③点击Maven,运行generator插件,自动生成代码。

如图:

 ④生成的文件即可自行根据需求copy或者cut到需要的地方,注意不能忘记yml文件或者properties文件对于mybatis的相关配置

# MyBatis
mybatis:
  # 配置 MyBatis数据返回类型别名(默认别名是类名)
  type-aliases-package: org.ghcode.manager.pojo
  # 配置 MyBatis Mapper 映射文件
  mapper-locations: classpath:mapper/*.xml
  #配置字段属性命名可不与数据库字段命名相同
  configuration:
    map-underscore-to-camel-case: true
    # 控制台日志配置(Mybatis的Sql语句执行打印)
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

至此,mybatis-generator的使用就介绍完了。 

三、Mybatis-Plugin插件

该插件可以实现从dao接口方法与mapper.xml文件方法配置之间的相互快速导航,安装方法:打开idea----->Setting------>plugin,进入商店搜索mybatis-plugin,之前是免费的,但是现在收费了,所以我们使用另一款:Free Mybatis Plugin

 效果如下:点击左侧箭头即可快速导航定位到相对应的dao接口方法处,反之从接口方法处也是一样的道理,可以定位到xml文件。

 

四、Mybatis-pagehelper分页插件

①在pom文件中添加相关依赖:

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.5</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.2.0</version>
    </dependency>

②在resource目录下配置分页插件xml文件

<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--reasonable:分页合理化参数,默认值为false。
                当该参数设置为 true 时,pageNum<=0 时会查询第一页,
                pageNum>pages(超过总数时),会查询最后一页。
                默认false 时,直接根据参数进行查询。-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>

③(1)、定义分页Vo类,设置分页信息(前提有相应的实体类作为超类让Vo类继承)

 (2)、定义好dao层,service层及service的实现类方法,将Vo类作为参数,返回list集合在控制层中测试如下图:

(3)、测试使用分页

 

 至此三剑客介绍完毕

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蔡学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值