Springboot整合FreeMarker的实现示例
程序员文章站
2022-06-10 09:10:51
目录一、项目搭建1、新建模块2、导入依赖 :将不相关的依赖删掉3、新建软件包,放入student实体类4、新建studentmapper接口5、springboot04application内引用ma...
一、项目搭建
1、新建模块
2、导入依赖 :将不相关的依赖删掉
<!-- <dependency>--> <!-- <groupid>org.mybatis.spring.boot</groupid>--> <!-- <artifactid>mybatis-spring-boot-starter</artifactid>--> <!-- <version>2.1.4</version>--> <!-- </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>
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<student> { }
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包、启动器导入
导入依赖
<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>
切面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<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 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<student> 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<student> 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文件
到此这篇关于springboot整合freemarker的实现示例的文章就介绍到这了,更多相关springboot整合freemarker 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 安卓模拟器多开挂手游改IP防封号技术讲解
下一篇: Python基础之Socket通信原理
推荐阅读