1.项目菜单
1.aspect
package com.yk.code.aspect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yk.code.util.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* @author yk
*/
@Component
@Aspect
@Slf4j
public class PageAspect {
@Around(value = "execution(* *..*Service.*Pager(..))")
public Object invoke(ProceedingJoinPoint point) throws Throwable {
PageBean pageBean = null;
for (Object e : point.getArgs()) {
if (e instanceof PageBean) {
pageBean = (PageBean) e;
break;
}
}
if (pageBean != null && pageBean.isPagination()) {
PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
}
Object obj = point.proceed(point.getArgs());
if (obj != null) {
if (obj instanceof Page) {
Page page = (Page) obj;
PageInfo pageInfo = new PageInfo(page);
pageBean.setTotal(new Long(pageInfo.getTotal()).intValue());
return pageInfo.getList();
}
}
return null;
}
}
2.controller层
package com.yk.code.controller;
import com.yk.code.pojo.Student;
import com.yk.code.service.StudentService;
import com.yk.code.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@Controller
public class StudentController {
@Autowired
private StudentService service;
@GetMapping
public String index(Model model, HttpServletRequest request){
PageBean pb=new PageBean();
pb.setRequest(request);
model.addAttribute("stu",service.findPager(pb));
model.addAttribute("pb",pb);
return "index";
}
@GetMapping("/del/{id}")
public String delete(@PathVariable Long id){
int n = service.del(new Student().setStuId(id));
return "redirect:/";
}
@PostMapping("/upd")
public String update(Student student){
int n = service.upd(student);
return "redirect:/";
}
@PostMapping("/add2")
public String add2(Student student){
int n = service.add(student);
return "redirect:/";
}
@GetMapping("/add")
public String add(){
return "add";
}
@GetMapping("/selectOne/{id}")
public String selectOne(@PathVariable Long id,Model model){
Student select = service.select(id);
model.addAttribute("select",select);
return "upd";
}
public Object find(HttpServletRequest request){
PageBean pb=new PageBean();
pb.setRequest(request);
return service.findPager(pb);
}
}
3.mapper层?
package com.yk.code.mapper;
import com.yk.code.pojo.Student;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface StudentMapper extends Mapper<Student> {
}
4.pojo实体层
package com.yk.code.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.persistence.*;
/**
* student
* @author zsq
* @date 2022-01-20T18:13:32.288
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Table(name ="student")
public class Student {
/**
*
*/
@Id
@Column(name = "stu_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long stuId;
/**
*
*/
@Column(name = "stu_name")
private String stuName;
/**
*
*/
private String stuPhone;
/**
*
*/
private Long stuClass;
/**
*
*/
private String stuAddress;
}
5.service层
package com.yk.code.service;
import com.yk.code.pojo.Student;
import com.yk.code.util.PageBean;
import org.springframework.stereotype.Service;
import java.util.List;
public interface StudentService {
List<Student> findPager(PageBean pageBean);
Student select(Long id);
int add(Student student);
int upd(Student student);
int del(Student student);
}
6.service实现类
package com.yk.code.service;
import com.github.pagehelper.Page;
import com.yk.code.mapper.StudentMapper;
import com.yk.code.pojo.Student;
import com.yk.code.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentMapper mapper;
@Override
public List<Student> findPager(PageBean pageBean) {
return mapper.selectAll();
}
@Override
public Student select(Long id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public int add(Student student) {
return mapper.insert(student);
}
@Override
public int upd(Student student) {
return mapper.updateByPrimaryKey(student);
}
@Override
public int del(Student student) {
return mapper.deleteByPrimaryKey(student);
}
}
7.分页PageBen
package com.yk.code.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @author 银喾
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private int total;
private int page = 1;
private int rows = 5;
private boolean pagination = true;
private String url;
private Map<String, String[]> ms;
public void setMs(Map<String, String[]> ms) {
this.ms = ms;
}
public int calcStartIndex() {
return (page - 1) * rows;
}
public int calcMaxPage() {
return total % rows == 0 ? total / rows : total / rows + 1;
}
public int nextPage() {
return page + 1 > calcMaxPage() ? calcMaxPage() : page + 1;
}
public int prevPage() {
return page - 1 > 1 ? page - 1 : 1;
}
public void setRequest(HttpServletRequest req) {
setUrl(req.getRequestURL().toString());
setMs(req.getParameterMap());
String page = req.getParameter("page");
if (page == null) {
setPage(1);
} else {
setPage(Integer.parseInt(page));
}
String rows = req.getParameter("rows");
if (rows == null) {
setRows(5);
} else {
setRows(Integer.parseInt(rows));
}
String pagination = req.getParameter("pagination");
if (pagination != null && "false".equals(pagination)) {
setPagination(false);
}
}
}
8.aplication.yml配置文件
server:
port: 8080
spring:
datasource:
username: root
password: root123
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true
freemarker:
#指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
allow-request-override: false
#req访问request
request-context-attribute: req
#后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
suffix: .ftl
#设置响应的内容类型
content-type: text/html;charset=utf-8
#是否允许mvc使用freemarker
enabled: true #是否开启template caching
cache: false
#设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
template-loader-path: classpath:/templates/
#设定Template的编码
charset: UTF-8
logging:
level:
com.yk.code.mapper: debug
9.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yk</groupId>
<artifactId>code</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot_04</name>
<description>SpringBoot_04</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.1</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version> <scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<mainClass>com.yk.code.SpringBoot04Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
pom.xml依赖配置
|