IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Springboot.整合FreeMarker -> 正文阅读

[Java知识库]Springboot.整合FreeMarker

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依赖配置

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章           查看所有文章
加:2022-02-09 20:33:44  更:2022-02-09 20:36:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 13:04:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码