pom.xml添加的代码
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.1.0
com.alibaba
druid
1.1.6
org.apache.velocity
velocity-engine-core
2.0
server.port=8080
server.context-path=/
server.tomcat.uri-encoding=UTF-8
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/example?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=zl19990510
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
实体类book
package com.example.demo.pojo;
import lombok.Data;
@Data
public class book {
private Integer id;
private String name;
private String author;
private Double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
mapper
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.Collection;
@Repository
@Mapper
public interface bookMybaticPlus extends BaseMapper {
}
service文件下
package com.example.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.book;
public interface MybaticService extends IService {
public IPage selectPage(long page, long size);
}
package com.example.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.bookMybaticPlus;
import com.example.demo.pojo.book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class MybaticServiceImpl extends ServiceImpl implements MybaticService {
@Autowired
bookMybaticPlus bookMybaticPlus;
@Override
public IPage selectPage(long page, long size) {
IPage iPage = bookMybaticPlus.selectPage(new Page<>(page, size), null);
return iPage;
}
}
controller文件下:
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.pojo.book;
import com.example.demo.service.MybaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class MybaticPlusController {
@Autowired
MybaticService MybaticService;
@ResponseBody
@RequestMapping(value="/findAll/{page}/{size}", produces = "application/json;charset=utf-8") /*格式:/findAll/1/5;1为页数,5为每页条数*/
public List getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
//需要在Config配置类中配置分页插件
IPage Page = new Page<>();
//每页条数
Page = MybaticService.selectPage(page,size);
System.out.println("------page------");
System.out.println(Page.getRecords());
System.out.println("------page------");
return Page.getRecords();
}
}
如控制台出现乱码:pom.xml文件需要加入:
org.springframework.boot
spring-boot-maven-plugin
true
-Dfile.encoding=UTF-8