mybatis分頁(yè)
導(dǎo)入pagehelper-spring-boot-starter,在執(zhí)行查詢之前,設(shè)置分頁(yè)參數(shù)
PageHelper.startPage(1, 2);
List<Dept> list = deptDao.findAll();
mybatis 多個(gè)SQL參數(shù)映射
使用@Param("標(biāo)識(shí)符")定義參數(shù),SQL使用#{標(biāo)識(shí)符}映射?!?/p>
@Select("select * from dept limit #{begin},#{size}")
public List<Dept> findPage(@Param("begin")int begin,@Param("size")int size);
springmvc請(qǐng)求參數(shù)設(shè)置默認(rèn)值
使用@ReuqestParam設(shè)置默認(rèn)值。
@GetMapping("/dept/list")
public List<Dept> list(
? ? ? ? @RequestParam(name="pageSize",required=false,defaultValue="2")Integer pageSize,
? ? ? ? @RequestParam(name="pageNumber",required=false,defaultValue="1")Integer pageNumber){? ?//... ...}
SpringBoot MVC應(yīng)用
第一種:開(kāi)發(fā)restful服務(wù)
HTTP請(qǐng)求-->MVC-->返回JSON結(jié)果
適合前后分離架構(gòu)應(yīng)用,前端可以開(kāi)發(fā)出多個(gè)不同的界面,比如瀏覽器、手機(jī)APP、小程序等。后端統(tǒng)一調(diào)用restful服務(wù)處理。
1.創(chuàng)建maven project (jar類型)
2.導(dǎo)入pom.xml jar包,spring-boot-starter-web
3.包含了springmvc、tomcat、restful支持的jar包
4.application.properties定義server.port
5.定義啟動(dòng)類,追加@SpringBootApplication
6.定義Controller、Service、Dao組件,采用注入方式建立關(guān)系應(yīng)用
第二種:開(kāi)發(fā)瀏覽器HTML應(yīng)用(JSP)
HTTP請(qǐng)求-->MVC-->JSP生成HTML結(jié)果
適合只開(kāi)發(fā)瀏覽器應(yīng)用。
1.創(chuàng)建maven project
2.導(dǎo)入pom.xml jar包,spring-boot-starter-web、tomcat-embed-jasper包
3.application.properties定義參數(shù)
server.port=8888
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/
4.定義啟動(dòng)類,使用@SpringBootApplication
5.在src/main目錄下創(chuàng)建webapp,添加dept_list.jsp文件
6.定義DeptController組件
@Controller
public class DeptController {
? ? @RequestMapping("/dept/list")
? ? public ModelAndView list() {
? ? ? ? ModelAndView mav = new ModelAndView();
? ? ? ? mav.setViewName("dept_list");//dept_list.jsp
? ? ? ? //mav.getModel().put("depts", list);
? ? ? ? return mav; } }
開(kāi)發(fā)瀏覽器HTML應(yīng)用*(Thymeleaf模板技術(shù))
HTTP請(qǐng)求-->MVC-->Thymeleaf模板生成HTML結(jié)果
Thymeleaf技術(shù)屬于模板技術(shù),是對(duì)JSP技術(shù)的替代。
模板和JSP區(qū)別:
1.運(yùn)行機(jī)制不同
????模板文件+模型數(shù)據(jù)=響應(yīng)輸出
????JSP--》Servlet--》編譯--》響應(yīng)輸出
2.模板技術(shù)比JSP簡(jiǎn)單、易用
????模板支持模板表達(dá)式語(yǔ)言
????JSP支持EL、JSTL、<%%>、指令等
Thymeleaf模板技術(shù),是由模板文件(.html)+模型數(shù)據(jù)(th表達(dá)式)=產(chǎn)生HTML輸出
適合只開(kāi)發(fā)瀏覽器應(yīng)用。
1.創(chuàng)建maven project
2.導(dǎo)入pom.xml jar包,spring-boot-starter-web、spring-boot-starter-thymeleaf包
3.application.properties定義參數(shù)
server.port=8888
4.定義啟動(dòng)類,使用@SpringBootApplication
5.在src/main/resources目錄下創(chuàng)建templates,添加dept_list.html文件
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
? ? <meta charset="UTF-8">
? ? <title>Insert title here</title>
</head>
<body>
? ? <h1 th:text="${msg}"></h1>
? ? <input th:value="${msg}">
? ? <ul>
? ? ? ? <li th:each="n : ${names}" th:text="${n}">xx</li>
? ? </ul>
</body>
</html>
6.定義DeptController組件(與JSP相同)
常用的Thymeleaf表達(dá)式語(yǔ)言
提示:使用前要在< html>元素里定義xmlns:th="https://www.thymeleaf.org"導(dǎo)入
- th:text? //將模型數(shù)據(jù)取出放入元素中間文本顯示
- th:each? //循環(huán)
- th:if? ? //判斷
- th:value //設(shè)置元素的value屬性值
- th:href? //動(dòng)態(tài)拼url或參數(shù)
- [[${xxx}]]? //原地顯示模型數(shù)據(jù)