Springboot整合FreeMarker的实现示例

网友投稿 267 2022-08-29


Springboot整合FreeMarker的实现示例

目录一、项目搭建1、新建模块2、导入依赖 :将不相关的依赖删掉3、新建软件包,放入student实体类4、新建StudentMapper接口5、Springboot04Application内引用mapper6、application.yml文件配置7、测试8、将切面、util包、启动器导入9、新建service层10、新建controller层11、运行启动类Springboot04Application,访问localhost:8080网址二、freemarker介绍1、新建资源文件application.yml2、模板语法 3、在资源文件中的templates文件下新建index.ftl文件

一、项目搭建

1、新建模块

2、导入依赖 :将不相关的依赖删掉

tk.mybatis

mapper-spring-boot-starter

2.0.2

javax.persistence

persistence-api

1.0

compile

3、新建软件包,放入student实体类

package com.lv.code.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.experimental.Accessors;

/**

* 学生表 student

* @author hgh

* @date 2022-01-21T13:35:43.912

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

@Accessors(chain = true)

public class Student {

/**

* 学生编号 主键 自增

*/

private Long stuId;

/**

* 学生名字

*/

private String stuName;

/**

* 学生电话

*/

private String stuPhone;

/**

* 学生班级

*/

private Long stuClass;

/**

* 学生地址

*/

private String stuAddress;

}

4、新建StudentMapper接口

package com.lv.code.mapper;

import com.lv.code.pojo.Student;

import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.common.Mapper;

@Repository

public interface StudentMapper extends Mapper {

}

5、Springboot04Application内引用mapper

package com.lv.code;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan("com.lv.code.mapper")

public class Springboot04Application {

public static void main(String[] args) {

SpringApplication.run(Springboot04Application.class, args);

}

}

6、application.yml文件配置

server:

port: 8080

# 配置数据源

spring:

datasource:

username: root

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/aaa?userSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true

# 日志打印

logging:

level:

com.lv.code.mapper: debug

7、测试

Springboot04ApplicationTests:

package com.lv.code;

import com.lv.code.mapper.StudentMapper;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

class Springboot04ApplicationTests {

@Autowired

StudentMapper mapper;

@Test

void contextLoads() {

System.out.println(mapper.selectAll());

}

}

8、将切面、util包、启动器导入

导入依赖

org.springframework.boot

spring-boot-starter-aop

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.10

切面PageAspect:

package com.lv.code.aspect;

import com.github.pagehelper.Page;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.lv.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 obj;

}

}

util包中的PageBean类

package com.lv.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 ms;

public void setMs(Map 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 Math.min(page + 1, calcMaxPage());

}

public int prevPage() {

return Math.max(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 ("false".equals(pagination)) {

setPagination(false);

}

}

}

9、新建service层

新建StudentService接口:

package com.lv.code.service;

import com.lv.code.pojo.Student;

import com.lv.code.util.PageBean;

import java.util.List;

public interface StudentService {

List findPager(PageBean pageBean);

}

实现StudentService接口:StudentServiceImpl

package com.lv.code.service;

import com.lv.code.mapper.StudentMapper;

import com.lv.code.pojo.Student;

import com.lv.code.util.PageBean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper mapper;

@Override

public List findPager(PageBean pageBean) {

return mapper.selectAll();

}

}

10、新建controller层

StudentController类:

package com.lv.code.controller;

import com.github.pagehelper.PageRowBounds;

import com.lv.code.service.StudentService;

import com.lv.code.util.PageBean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController

public class StudentController {

@Autowired

private StudentService service;

@GetMapping

public Object find(HttpServletRequest request){

PageBean pageBean=new PageBean();

pageBean.setRequest(request);

return service.findPager(pageBean);

}

}

11、运行启动类Springboot04Application,访问localhost:8080网址

二、freemarker介绍

1、新建资源文件application.yml

更改配置文件,添加freemarker配置

server:

port: 8080

# 配置数据源

spring:

datasource:

username: root

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/aaa?userSSL=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.lv.code.mapper: debug

2、模板语法

3、在资源文件中的templates文件下新建index.ftl文件

增加模块:

然后可选中freemarker直接生成ftl文件


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python对app页面元素进行封装并设置查找时间操作实例
下一篇:python字符串内建函数操作实例(cmp、str、enumerate、zip等)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~