目录
MyBatis与Spring集成
导入pom依赖
导入generatorConfig.xml
导入spring-mybatis.xml
自动生成mapper文件
编写接口类:BookBiz
aop整合PageHelper分页插件
编写分页查询
编写pagebean
编写PagerAspect类
测试结果
4.0.0
org.example
Spring
1.0-SNAPSHOT
UTF-8
1.8
1.8
3.7.0
5.0.2.RELEASE
3.4.5
5.1.44
5.1.2
1.3.1
2.1.1
2.4.3
2.9.1
4.12
4.0.0
1.18.2
org.springframework
spring-context
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
mysql
mysql-connector-java
${mysql.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
org.apache.commons
commons-dbcp2
${commons.dbcp2.version}
org.apache.commons
commons-pool2
${commons.pool2.version}
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-web
${log4j2.version}
junit
junit
${junit.version}
javax.servlet
javax.servlet-api
${servlet.version}
provided
org.projectlombok
lombok
${lombok.version}
provided
Spring
src/main/java
**/*.xml
src/main/resources
jdbc.properties
*.xml
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin.version}
${maven.compiler.target}
${project.build.sourceEncoding}
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
${mysql.version}
true
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
导入spring-context.xml
helperDialect=mysql
package com.xiaoxu.biz;
import com.xiaoxu.model.Book;
import com.xiaoxu.utlis.PageBean;
import java.util.List;
/**
* @author小徐
* @site www.veryedu.cn
* @company xu集团
* @create 2023-08-25 15:17
*/
public interface BookBiz {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List listPage(Book book, PageBean pageBean);
}
编写接口实现类:
package com.xiaoxu.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.biz.BookBiz;
import com.xiaoxu.mapper.BookMapper;
import com.xiaoxu.model.Book;
import com.xiaoxu.utlis.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author小徐
* @site www.veryedu.cn
* @company xu集团
* @create 2023-08-25 15:19
*/
@Service
public class BookBizimpl implements BookBiz {
//相当于set,get方法
@Autowired
private BookMapper bookMapper;
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public List listPage(Book book, PageBean pageBean) {
// if (pageBean !=null && pageBean.isPagination())
// PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
// List list = bookMapper.listPage(book);
// if (pageBean !=null && pageBean.isPagination()){
// PageInfo info = new PageInfo<>(list);
// pageBean.setTotal((int)info.getTotal());
// }
return bookMapper.listPage(book);
}
}
编写实体类:
package com.xiaoxu.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private Integer bid;
private String bname;
private Float price;
public Book(Integer bid, String bname, Float price) {
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
super();
}
public Integer getBid() {
return bid;
}
public void setBid(Integer bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"bid=" + bid +
", bname='" + bname + '\'' +
", price=" + price +
'}';
}
}
编写接口测试类:
package com.xiaoxu.impl;
import com.xiaoxu.biz.BookBiz;
import com.xiaoxu.model.Book;
import com.xiaoxu.utlis.PageBean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* @author小徐
* @site www.veryedu.cn
* @company xu集团
* @create 2023-08-25 16:04
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-context.xml"})
public class BookBizimplTest {
@Autowired
private BookBiz bookBiz;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void deleteByPrimaryKey() {
System.out.println(this.bookBiz.deleteByPrimaryKey(1));
}
@Test
public void insert() {
}
@Test
public void insertSelective() {
}
@Test
public void selectByPrimaryKey() {
System.out.println(bookBiz.selectByPrimaryKey(37));
}
@Test
public void updateByPrimaryKeySelective() {
}
@Test
public void updateByPrimaryKey() {
}
@Test
public void listPage() {
Book book = new Book();
PageBean pageBean = new PageBean();
pageBean.setPage(2);
pageBean.setRows(20);
book.setBname("圣墟");
this.bookBiz.listPage(book,pageBean).forEach(System.out::println);
}
}
测试结果:
package com.xiaoxu.utlis;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;
public class PageBean implements Serializable {
private static final long serialVersionUID = 2422581023658455731L;
//页码
private int page=1;
//每页显示记录数
private int rows=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private Map map;
public PageBean() {
super();
}
//设置请求参数
public void setRequest(HttpServletRequest req) {
String page=req.getParameter("page");
String rows=req.getParameter("rows");
String pagination=req.getParameter("pagination");
this.setPage(page);
this.setRows(rows);
this.setPagination(pagination);
this.url=req.getContextPath()+req.getServletPath();
this.map=req.getParameterMap();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
if(null!=page&&!"".equals(page.trim()))
this.page = Integer.parseInt(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
if(null!=rows&&!"".equals(rows.trim()))
this.rows = Integer.parseInt(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return isPagination;
}
public void setPagination(boolean isPagination) {
this.isPagination = isPagination;
}
public void setPagination(String isPagination) {
if(null!=isPagination&&!"".equals(isPagination.trim()))
this.isPagination = Boolean.parseBoolean(isPagination);
}
/**
* 获取分页起始标记位置
* @return
*/
public int getStartIndex() {
//(当前页码-1)*显示记录数
return (this.getPage()-1)*this.rows;
}
/**
* 末页
* @return
*/
public int getMaxPage() {
int totalpage=this.total/this.rows;
if(this.total%this.rows!=0)
totalpage++;
return totalpage;
}
/**
* 下一页
* @return
*/
public int getNextPage() {
int nextPage=this.page+1;
if(this.page>=this.getMaxPage())
nextPage=this.getMaxPage();
return nextPage;
}
/**
* 上一页
* @return
*/
public int getPreivousPage() {
int previousPage=this.page-1;
if(previousPage<1)
previousPage=1;
return previousPage;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
+ "]";
}
}
package com.xiaoxu.aspect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.utlis.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author小徐
* @site www.veryedu.cn
* @company xu集团
* @create 2023-08-25 17:24
*
* * *..*Service.*Pager(..)
* *:任意返回值类型
* *..:任意包,不限层级
* *service:以serveice结尾的类/接口
* *page:以page结尾的方法
* ..:任意参数
* 符合上述条件,即为目标类或者目标方法
*/
@Aspect//代表当前类为切面类
@Component//代表当前类交给spring进行管理
public class PagerAspect {
@Around("execution(* *..*Service.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable {
PageBean pageBean = null;
Object[] params = args.getArgs();
for (Object param : params) {
if(param instanceof PageBean){
pageBean = (PageBean)param;
break;
}
}
if(pageBean != null && pageBean.isPagination())
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
Object list = args.proceed(params);
if(null != pageBean && pageBean.isPagination()){
PageInfo pageInfo = new PageInfo((List) list);
pageBean.setTotal(pageInfo.getTotal()+"");
}
return list;
}
}