<!-- <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-b质量法规oot-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>
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;}
package com.lv.code.mapper; import com.lv.code.pojo.student;import org.springframework.stereotype.repository;import tk.mybatis.mapper.common.mapper; @repositorypublic interface studentmapper extends mapper<student> { }
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); } }
rver: port: 8080# 配置数据源spring: datasource: urname: root password: 123456 driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://localhost:3306/aaa?urssl=fal&rvertimezone=asia/shanghai&uunicode=true&characterencoding=utf-8&allowpublickeyretrieval=true# 日志打印logging: level: com.lv.code.mapper: debug
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; @springboottestclass springboot04applicationtests { @autowired studentmapper mapper; @test void contextloads() { system.out.println(mapper.lectall()); } }
导入依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifa专科警校ctid>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@slf4jpublic class pageaspect { @around(value = "execution(* *..*rvice.*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.ttotal(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.rvlet.http.httprvletrequest;import java.util.map; /** *小屋子 @author 银喾 */@data@noargsconstructor@allargsconstructorpublic 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 tms(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 trequest(httprvletrequest req) { turl(req.getrequesturl().tostring()); tms(req.getparametermap()); string page = req.getparameter("page"); if (page == null) { tpage(1); } el { tpage(integer.parint(page)); } string rows = req.getparameter("rows"); if (rows == null) { trows(5); } el { trows(integer.parint(rows)); } string pagination = req.getparameter("pagination"); if ("fal".equals(pagination)) { tpagination(fal); } } }
新建studentrvice接口:
package com.lv.code.rvice; import com.lv.code.pojo.student;import com.lv.code.util.pagebean; import java.util.list; public interface studentrvice { list<student> findpager(pagebean pagebean); }
实现studentrvice接口:studentrviceimpl
package com.lv.code.rvice; 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.rvice; import java.util.list; @rvicepublic class studentrviceimpl implements studentrvice { @autowired private studentmapper mapper; @override public list<student> findpager(pagebean pagebean) { return mapper.lectall(); }}
studentcontroller类:
package com.lv.code.controller; import com.github.pagehelper.pagerowbounds;import com.lv.code.rvice.studentrvice;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.rvlet.http.httprvletrequest; @restcontrollerpublic class studentcontroller { @autowired private studentrvice rvice; @getmapping public object find(httprvletrequest request){ pagebean pagebean=new pagebean(); pagebean.trequest(request); return rvice.findpager(pagebean); } }
更改配置文件,添加freemarker配置
rver: port: 8080# 配置数据源spring: datasource: urname: root password: 123456 driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://localhost:3306/aaa?urssl=f冒险岛之旅al&rvertimezone=asia/shanghai&uunicode=true&characterencoding=utf-8&allowpublickeyretrieval=true freemarker: #指定httprvletrequest的属性是否可以覆盖controller的model的同名项 allow-request-override: fal #req访问request request-context-attribute: req #后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html suffix: .ftl #设置响应的内容类型 content-type: text/html;chart=utf-8 #是否允许mvc使用freemarker enabled: true #是否开启template caching cache: fal #设定模板的加载路径,多个以逗号分隔,默认: [ “classpath:/templates/” ] template-loader-path: classpath:/templates/ #设定template的编码 chart: utf-8# 日志打印logging: level: com.lv.code.mapper: debug
增加模块:
然后可选中freemarker直接生成ftl文件
到此这篇关于springboot整合freemarker的实现示例的文章就介绍到这了,更多相关springboot整合freemarker 内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 23:22:30,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0ebe3587b5d4cd85b678404647a419c3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Springboot整合FreeMarker的实现示例.doc
本文 PDF 下载地址:Springboot整合FreeMarker的实现示例.pdf
留言与评论(共有 0 条评论) |