您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Springboot如何整合FreeMarker,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
<!-- <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>
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; @Repository public 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); } }
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
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()); } }
導入依賴
<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); } } }
新建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(); } }
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); } }
更改配置文件,添加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
增加模塊:
然后可選中freemarker直接生成ftl文件
關于“Springboot如何整合FreeMarker”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。