官方介绍是mybatis的一个增强,不做修改,只是为了扩展其功能,对开发同学来讲,在使上用会更加方便。详情参见官网链接地址:https://mp.baomidou.com/ 苞米豆,很的意思的名字,希望我们的大咖团队及大咖同学有更多好的框架和工具提供,1086个赞!!
用过mybaits的同学都知道,它是在持久层框加在应用比较广泛的,很多架构都是基于spring+springMVC+mybaits的。虽然Mybaits有generator,可以省去我们自己去写mapper、entity、xml,但是xml文件还是很多的,而且分页也是一块需要额外处理的部分。MP可以很好的解决这个问题了
它的这些特性就不再总结了,直接从官网扒过来给大家参考,官网很不错,建议大家多参看

废话不多说了,直接上代码,走起~~~ 先贴个目录结构给大家参考,我写了一个xml,其实可以不用写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>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给分页插件设置方言
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;
}
}本例中用了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需要继承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:
