SpringBoot整合通用Mapper以及Pagehelper分頁外掛
前言
最近公司在用的技術,是國內的大神寫的Mybatis外掛,我自己也嘗試搭了一個小demo,搭起來也不復雜,但也有一些坑要注意一下
首先介紹一下這兩項技術:Mapper與Pagehelper,
Mapper:通用Mapper都可以極大的方便開發人員。可以隨意的按照自己的需要選擇通用方法,還可以很方便的開發自己的通用方法。極其方便的使用MyBatis單表的增刪改查。支援單表操作,不支援通用的多表聯合查詢。通用 Mapper 支援 Mybatis-3.2.4 及以上版本。(此段來自官方文件)
Pagehelper:Pagehelper是一個非常好用的分頁外掛,配合前端的資料表格,簡直不要太爽,只需填好分頁引數,在你使用查詢的時候就會有攔截器攔截,新增分頁的通用邏輯,並且無視資料庫,只需配好資料庫方言。
注意:本文只介紹搭建的步驟,如需實現原理請移步該作者的個人網站
建立表:非常簡單隻有兩個欄位
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stu_id` int(11) PRIMARY KEY AUTO_INCREMENT,
`stu_name` varchar(20) NOT NULL
);
專案結構:去掉了一些不相關程式碼,只保留了基本的資料訪問層,使用junit測試介面,整個專案非常簡潔.
pom檔案:
<!-- spring boot 父節點依賴,引入這個之後相關的引入就不需要新增version配置, spring boot會自動選擇最合適的版本進行新增。 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF8</project.build.sourceEncoding> <!-- 指定一下jdk的版本 ,這裡我們使用jdk 1.8 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- spring-boot-starter-web: MVC,AOP的依賴包.... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 新增fastjson 依賴包. --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <!-- mysql 資料庫驅動. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- 通用Mapper,基於 starter 的自動配置 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- 分頁外掛PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 構建節點. --> <build> <plugins> <!-- 新增SpringBoot的外掛 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
properties檔案:
########################################################
###datasource -- mysql
########################################################
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/studentdb
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
# Mybatis
mybatis.typeAliasesPackage=com.zhanghui.entity
mybatis.mapperLocations=classpath:mapper/*.xml
建立實體類:瞭解hibernate看這注解看起來是不是很熟悉,hibernate是面向物件的CRM框架嘛,所以需要實體類跟表進行對映,通用Mapper生成的SQl語句也需要知道你實體類欄位對應表的那個欄位從而生成SQl語句,所以需要這些註解,也可以使用@NameStyle進行規則對映,比如使用"駝峰轉下劃線大寫形式",實體類欄位為"stuName",則認為你的表字段為"stu_name"
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import lombok.Data;
@Data//lombok,可以省略set,get等常用方法
@Table(name="student")//對應資料庫表名
public class Student {
@Id//表明此列為主鍵
@Column(name="stu_id")//資料庫的欄位名
@GeneratedValue(strategy = GenerationType.IDENTITY)//主鍵生成規則,當你使用它的新增方法時會用到
private Integer id;
@Column(name="stu_name")
private String name;
@Transient//忽略該欄位,也就是該欄位在表中不存在
private String clazz;
}
建立StudentMapper介面:因為我們使用通用Mapper提供的方法,所以你不需要寫實現,你會發現繼承了一個BaseMapper的介面,不著急,後面會講到,如果你需要一些自定義的方法,原來怎麼寫現在一樣的寫,沒有任何變化
import com.zhanghui.config.BaseMapper;
import com.zhanghui.entity.Student;
public interface StudentMapper extends BaseMapper<Student>{
}
建立StudentMapper對映檔案:因為上面沒有編寫方法,所以這裡自然也不需要寫實現,寫對映檔案的目的是為了Mybatis能生成對應的實現類
<?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.zhanghui.mapper.StudentDao">
</mapper>
建立BaseMapper:通過原始碼可以看到這些介面的頂級介面最後又被分為增刪改查四大類,所以上面的StudentMapper通過繼承BaseMapper,就間接實現了這些通用的增刪改查介面。當然這一部分介面不需要你自己實現,所以它不能和其它的mapper放在一起,以免被@MapperScan("com.*.Mapper")掃描到,Mybatis掃描後發現你實現了這麼多介面但是在對映檔案卻一個也沒有實現,這時候就會報錯。但是這些介面一定要被通用Mapper的MapperScan掃描到,由它來根據你在實體類與表中配關係生成通用的增刪改查方法。
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 通用mapper,注意:這個mapper不能和其它mapper放在一起
* 也就是不能被Mybatis掃描到
*/
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, IdsMapper<T> {
}
建立MyBatisMapperScannerConfig:這個是最關鍵的一個類,由它進行配置,配置的解釋在註釋中都已經說明白了,記得把mapper類的包名換成自己專案中實際的包名
/**
* 通用mapper與分頁外掛的一些配置
*/
@Configuration
public class MyBatisMapperScannerConfig {
/**
* 使用通用Mapper之前需要初始化的一些資訊
* 使用通用Mapper外掛時請勿使用熱載入,否則報錯,外掛作者後續應該會修復
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.zhanghui.mapper");//普通mapper的位置
Properties properties = new Properties();
properties.setProperty("mappers", BaseMapper.class.getName());//通用mapper的全名
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");//配置資料庫方言
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
/**
* 配置mybatis的分頁外掛pageHelper
*/
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
//設定為true時,會將RowBounds第一個引數offset當成pageNum頁碼使用
properties.setProperty("offsetAsPageNum","true");
//置為true時,使用RowBounds分頁會進行count查詢
properties.setProperty("rowBoundsWithCount","true");
//合理化查詢,啟用合理化時,
//如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁
//未開啟時如果pageNum<1或pageNum>pages會返回空資料
properties.setProperty("reasonable","true");
//配置mysql資料庫的方言
properties.setProperty("dialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
建立SpringBoot啟動類:這個懂SpringBoot的都懂,這裡就不講了
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.zhanghui.config.BaseMapper;
@SpringBootApplication
@MapperScan(basePackages = { "com.zhanghui.mapper" },markerInterface = BaseMapper.class)
public class App extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
建立MapperTest:這一步不是必須的,我這裡是為了方便,也為了快速定位問題。可以看到selectAll與select這兩個方法並不是我實現的而是通用Mapper生成的,更多方法請見通用Mapper方法大全
/**
* 為了簡單隻有資料訪問層,
* 寫測試類測試
* 這裡只用到了查詢的方法,mapper提供
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperTest {
@Resource
private StudentMapper studentMapper;
@Test
public void test1() {
//不分頁查詢
List<Student> lis1 = studentMapper.selectAll();
for (Student stu : lis1) {
System.out.println(stu);
}
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
//分頁查詢,顯示第一頁,每頁五條
PageHelper.startPage(1,5);
List<Student> lis2 = studentMapper.selectAll();
for (Student stu : lis2) {
System.out.println(stu);
}
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
//分頁查詢,顯示第一頁,每頁五條,按id降序
PageHelper.startPage(1,5,"stu_id desc");
List<Student> lis3 = studentMapper.selectAll();
for (Student stu : lis3) {
System.out.println(stu);
}
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
//分頁查詢,顯示第一頁,每頁五條,按id降序
PageHelper.startPage(1,5,"stu_id desc");
Student student = new Student();
student.setName("二班張三");
List<Student> lis4 = studentMapper.select(student);
for (Student stu : lis4) {
System.out.println(stu);
}
}
}
兩個外掛到這裡就整合完了,貌似PageHelper完全沒提到,因為太簡單了,簡單配置然後可以直接使用了,如果這個外掛在專案中使用還會用到一個分頁類PageInfo,比如你在Service層中查詢分頁查詢學生,在controller直接返回PageInfo就好,配合前端的資料表格外掛會很舒服
//分頁查詢,顯示第一頁,每頁五條,按id降序
PageHelper.startPage(1,5,"stu_id desc");
List<Student> lis4 = studentMapper.selectAll();
//使用的時候Pagehelper會自動填充PageInfo,你只需將查到的資訊填充就好
return new PageInfo<>(lis4);
整個專案為了方便初學者學習只保留了主要的枝幹,所以看起來非常簡單,很精簡,哈哈
END~~~
文章有問題歡迎指正,期待能與您交流!
QQ:1251235022
Email:[email protected]
如果能幫助到您,望能博君一讚,這就是對我最大的鼓勵
相關推薦
SpringBoot整合通用Mapper以及Pagehelper分頁外掛
前言最近公司在用的技術,是國內的大神寫的Mybatis外掛,我自己也嘗試搭了一個小demo,搭起來也不復雜,但也有一些坑要注意一下首先介紹一下這兩項技術:Mapper與Pagehelper,Mapper:通用Mapper都可以極大的方便開發人員。可以隨意的按照自己的需要選擇通
MyBatis基於Spring-boot整合通用Mapper以及pagehelper分頁外掛(含原始碼下載)
配置 POM檔案 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-paren
Spring整合MyBatis 通用Mapper以及 pagehelper分頁外掛
先在spring 配置檔案加上這個 <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <
SpringBoot整合Mybatis-Plus和PageHelper分頁外掛,附專案原始碼
1 pom.xml配置檔案 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3
SpringBoot整合Redis,以及MybatisPlus分頁、Mapper的使用(一)
概述 這段時間接觸很多知識,也逐漸深入到專案整個流程開發,對Linux也有逐步地認識。雖然有去探索,但感覺能力還不足以寫出來跟大家分享。撰寫本文是瞭解到有些讀者反饋,對於MP(MybatisPlus縮寫)不太瞭解以及如何去使用,但更多還是筆者用完覺得非常強大,
spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 整合通用maper,與分頁外掛:
spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 整合通用maper,與分頁外掛: pom.xml <parent> <groupId>org.springframe
springboot 整合通用Mapper + 分頁外掛
pom依賴如下: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spri
SpringBoot整合通用Mapper 分頁外掛 Generator
研究了一天終於實現了在springboot中整合通用Mapper,分頁外掛,和Generator,特此記錄一下 首先是整合Generator,有幾種方式,在這裡使用maven整合 在pom.xml中整合Generator外掛 <plugins
SpringBoot入門篇--整合mybatis+generator自動生成程式碼+druid連線池+PageHelper分頁外掛
我們這一一篇部落格講的是如何整合Springboot和Mybatis框架,然後使用generator自動生成mapper,pojo等檔案。然後再使用阿里巴巴提供的開源連線池druid,這個連線池的好處我就不說了,集合了所有連線池的好處,並且還提供了監控等功能,加大了可擴充套件性等等。 1.&
springboot如何整合mybatis的pagehelper分頁外掛
mybatis提供了一個非常好用的分頁外掛,之前整合的時候需要配置mybatis-config.xml的方式,今天我們來看下它是如何整合springboot來更好的服務的。 只能說springboot的強大之處真是不容小覷。 第一步:新增依賴 第二步:配置檔案簡單: 第三
SpringBoot 2.x 整合Mybatis二:PageHelper分頁
Mybatis-PageHelper 簡介 PageHelper 最方便使用的分頁外掛,支援多種資料庫: Oracle Mysql MariaDB SQLite Hsqldb PostgreSQL DB2 SqlServer(2005,2008) I
SpringBoot入門五,添加pagehelper分頁插件支持最簡配置
查詢 end sha spring 執行 service 第一個 mark com 1.pom.xml中引入jar包 <dependency> <groupId>com.github.pagehelper</groupId>
Springboot整合通用mapper、XML、service《spring boot學習五》
1. springmvc之mapper.xml的痛 一般情況下都是一個類寫一個xml或者說即使N個類共用一個XML,其實對於開發者的工作量也是很大的,前期倒沒有什麼,因為可以用自動生成工具來生成,但是後期,如果要新增什麼欄位或者修改欄位的話,對於我們來說真的太噁心了 所以
Springboot 使用PageHelper分頁外掛實現分頁
一、pom檔案中引入依賴 二、application.properties中配置以下內容 pagehelper.helper-dialect=mysqlpagehelper.reasonable=truepagehelper.support-methods-arguments=truepagehelp
SpringBoot入門筆記13——pageHelper分頁外掛的使用
SpringBoot入門筆記14——pageHelper分頁外掛的使用 實現方式有好幾種,無非就是 spring的還是springboot的,是註解的還是配置的。 比如: 1、引入依賴 <!-- 分頁外掛依賴 --> <dependency&g
springboot2.0.5整合mybatis(PageHelper分頁外掛、generator外掛使用)
用IDEA搭建springboot2.0.5專案 選擇Spring initializr就可以輕鬆搭建一個springboot專案,第一次搭建很費時 在Group寫上公司域名,Artifact寫上專案名,打包用Jar 選Web勾選 SQL項,勾選MySQL
PageHelper分頁外掛及通用分頁js
1.物理分頁 物理分頁依賴的是某一物理實體,這個物理實體就是資料庫,比如MySQL資料庫提供了limit關鍵字,程式設計師只需要編寫帶有limit關鍵字的SQL語句,資料庫返回的就是分頁結果。建議使用。 2.邏輯分頁 邏輯分頁依賴的是程式設計師編寫的程式碼。資料庫返回的不是分頁結果,而是全部資料,然
SpringBoot整合Spring Data JPA(包含分頁、排序操作)完成資料獲取
Spring Data JPA是Spring基於Hibernate開發的一個JPA框架。如果用過Hibernate或者MyBatis的話,就會知道物件關係對映(ORM)框架有多麼方便。但是Spring Data JPA框架功能更進一步,為我們做了 一個數據持久層框架幾乎能做的任何事情。
pageHelper分頁外掛使用以及 jsp 中資料處理(記錄)
使用pageHelper 分頁外掛可以很簡單的進行分頁展示,但一段時間不知道怎麼在前端對這些資料進行處理並展示 百度找了很久沒有沒有查到想要的結果,不斷摸索終於是完成了 (記錄一下) 引入jar包(jsqlparser-0.9.5.jar,pagehelper-4.1.
springmvc+mybatis+pagehelper分頁外掛整合AbstractRoutingDataSource動態切換資料來源(含例子程式碼) ...
一、前期工作 1)開發工具Eclipse+maven+jdk1.6以上 2)Tomcat伺服器 3)技術架構Spring+Mybatis 二、在eclipse一個Maven的Web工程(略) 不懂的同學請參考部落格http://www.cnblogs.com/leiOOlei/p/3361633.ht