spring boot中使用jsp:
1.添加tomcat依赖和jsp支持
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
2.在application.yml中设置前缀和后缀
spring: mvc: view: prefix: /jsp/ suffix: .jsp
3.在建一个jsp页面
<%-- Created by IntelliJ IDEA. User: wish Date: 2019/12/12 Time: 9:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4> 欢迎光临,${name} </h4> </body> </html>
4.建立controller进行页面跳页面
package com.webboot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/PageJump") public class PageJump { @RequestMapping("/getIndex") public String getIndex(ModelMap modelMap){ modelMap.put("name","王五"); return "index"; } }
spring boot中使用thymeleaf
1.导入thymeleaf的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.在application.yml中设置thymeleaf关闭缓存
spring: thymeleaf: cache: false
3.建立html页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </meta> </head> <body> <ul th:each="stu:${list}"> <li><span th:text="${stu.stuId}"></span><span th:text="${stu.stuName}"></span></li> </ul> </body> </html>
4.通过controller进行页面跳转
package com.boot.controller; import com.boot.entity.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; @Controller public class StuController { @RequestMapping("/getHtml") public String getHtml(Model model){ Student student1 =new Student("李1",1); Student student2 =new Student("李2",2); Student student3 =new Student("李3",3); List<Student> list = new ArrayList<>(); list.add(student1); list.add(student2); list.add(student3); model.addAttribute("list",list); System.out.println("123456"); return "show"; } }
spring boot中使用jpa
1.导入jpa依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2.在application.yml中配置数据库连接参数
##数据库四大连接参数 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/invoicingsystem?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456
3.dao层中的接口继承CrudRepository<object, object>
package com.boot.dao; import com.boot.entity.Users; import org.springframework.data.repository.CrudRepository; /** * 创建一个IUserDao继承CrudRepository */ public interface IUserDao extends CrudRepository<Users, Integer> { }