mybaits-plus学习

什么是mybatis-plus

官方介绍是mybatis的一个增强,不做修改,只是为了扩展其功能,对开发同学来讲,在使上用会更加方便。详情参见官网链接地址:https://mp.baomidou.com/
苞米豆,很的意思的名字,希望我们的大咖团队及大咖同学有更多好的框架和工具提供,1086个赞!!

为什么会有MP呢

用过mybaits的同学都知道,它是在持久层框加在应用比较广泛的,很多架构都是基于spring+springMVC+mybaits的。虽然Mybaits有generator,可以省去我们自己去写mapper、entity、xml,但是xml文件还是很多的,而且分页也是一块需要额外处理的部分。MP可以很好的解决这个问题了

MP有哪些特性

它的这些特性就不再总结了,直接从官网扒过来给大家参考,官网很不错,建议大家多参看

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

MP框架结构

在这里插入图片描述

Springboot整 合Mybatis-plus

废话不多说了,直接上代码,走起~~~
先贴个目录结构给大家参考,我写了一个xml,其实可以不用写xml的
在这里插入图片描述

pom.xml

 <!--本例中用到的是mysql,mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--MP boot整合JAR-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--MP generator-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.2</version>
        </dependency>

MP 配置

application.yml配置mysql和mp

spring:
  datasource:
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
        username: root
        password: 
        driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
    mapper-locations: classpath*:mapper/*Mapper.xml
         type-aliases-package: com.chnenergy.monitoring.supervison.calculator.dao.domain
         configuration:
         map-underscore-to-camel-case: true
  • 注:对于低版本的MP,本例中用的版本,好像去了也可以,没测试,大家可以自己试试看。如果数据库中字段是下划线的,bean里是驼峰命名,需要配置map-underscore-to-camel-case: true。eg:数据库字段file_type。bean里是fileType,则需要配置这一句话才能查出字段值

给分页插件设置方言

package com.zl.test.mp.demo.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        //设置方言类型
        page.setDialectType("mysql");
        return page;
    }
}

实体bean

本例中用了lombok,所以省略了getter 和 setter,关于lombok大家可自行百度,本文不做延展。
另外忽略我加的注解 @TableField(),本以为这个可以做为与数据库字段的映射,实际没卵用,可见官网具体用法。

package com.zl.test.mp.demo.dao.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("T_TARGET_FILE")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class TargetFile implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId("id")
    private Long id;
    @TableField("COAL_MINE_ID")
    private String coalMineId;
    @TableField("FRONT_ID")
    private Integer frontId;
    @TableField("SENDER_ID")
    private String senderId;
    @TableField("SERIAL_ID")
    private Long serialId;
    @TableField("SOURCE_SYSTEM")
    private String sourceSystem;
    @TableField("FILE_TYPE")
    private String fileType;
    @TableField("FILE_TIME")
    private String fileTime;
    @TableField("INIT_TIME")
    private Date initTime;
    @TableField("RECEIVE_TIME")
    private Date receiveTime;
    @TableField("PROCESS_STATUS1")
    private Boolean processStatus1;
    @TableField("PROCESS_STATUS2")
    private Boolean processStatus2;
    @TableField("PROCESS_STATUS3")
    private Boolean processStatus3;
    @TableField("PROCESS_STATUS4")
    private Boolean processStatus4;
    @TableField("PROCESS_STATUS5")
    private Boolean processStatus5;
    @TableField("PROCESS_TIME1")
    private Date processTime1;
    @TableField("PROCESS_TIME2")
    private Date processTime2;
    @TableField("PROCESS_TIME3")
    private Date processTime3;
    @TableField("PROCESS_TIME4")
    private Date processTime4;
    @TableField("PROCESS_TIME5")
    private Date processTime5;
    @TableField("FILE_CONTENT")
    private byte[] fileContent;

}

mapper

mapper需要继承BaseMapper
本例中的方法是一个单表查询,是为了写个例子给大家,单表的话,自带的封装方法完全可以满足,不需要自己再手写,多表关联可以参见本例写法即可~~

package com.zl.test.mp.demo.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chnenergy.monitoring.supervison.calculator.dao.domain.TargetFile;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TargetFileMapper  extends BaseMapper<TargetFile> {
    @Select(" SELECT * FROM T_TARGET_FILE WHERE COAL_MINE_ID=#{mineCode} ORDER BY FILE_TIME DESC LIMIT 1")
    TargetFile getTargetFileByMineCode(@Param("mineCode") String mineCode)
            ;
}


如果还是想用xml,就正常写好了。不用在方法中加SQL,直接在xml里配制如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.chnenergy.monitoring.supervison.calculator.dao.mapper.TargetFileMapper">
    <resultMap id="targetFile" type="com.chnenergy.monitoring.supervison.calculator.dao.domain.TargetFile"/>
    <select id="getTargetFileByMineCode" parameterType="String" resultType="com.chnenergy.monitoring.supervison.calculator.dao.domain.TargetFile">
        SELECT * FROM T_TARGET_FILE WHERE COAL_MINE_ID=#{mineCode} ORDER BY FILE_TIME DESC LIMIT 1
    </select>
</mapper>

测试类

这些就可以了,下面我们生成一个测试 类来试试吧

package com.zl.test.mp.demo.dao;

import com.zl.test.mp.demo.dao.domain.TargetFile;
import com.zl.test.mp.demo.dao.mapper.TargetFileMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TargetFileMapperTest {
    @Autowired
    private TargetFileMapper mapper;

    @Test
    public void getTargetFileByMineCode() {
        String mineCode = "10000000";
        TargetFile tt = mapper.getTargetFileByMineCode(mineCode);
        System.out.println(tt.getFileTime());
        System.out.println(tt.getFileContent().length);
//        Wrapper wrapper = new QueryWrapper<TargetFile>().eq("COAL_MINE_ID", mineCode).orderByDesc("FILE_TIME");
//        TargetFile t = mapper.selectOne(wrapper);
//        System.out.println(t.getFileTime()+"\t------"+t.getFileContent().length);
    }
}

结果如下图所示,是不是so easy:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值