1、
2、Dependencies有jar包爆红处理方法
- 先clean一下然后package下载Jar包,下载完成后,删除pom.xml中的对应jar包依赖,再clean一下,然后重新添加jar包依赖导入完成。
3、
下面一个@RequesstMapping继承于上面一个。
POJO:是有属性值的实体对象类
*****注:Thymeleaf有个大坑,pom中引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
properties文件中还需要配置以下内容才能访问到
# 项目的启动服务端口号 server.port=80 # 项目的起始路径 spring.thymeleaf.prefix= classpath:/templates/ spring.thymeleaf.mode=LEGACYHTML5 # 不进未关闭标签检查,需配合nekohtml使用 spring.thymeleaf.cache=false
4、springboot + thymeleaf + html
1 package com.mashibing.springboot03.controller; 2 3 import com.mashibing.springboot03.domain.City; 4 import com.mashibing.springboot03.service.CityService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 8 import org.springframework.ui.Model; 9 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 13 import java.util.List; 14 15 16 17 @Controller 18 @RequestMapping("/user") 19 public class MainController { 20 21 @Autowired 22 CityService cityService; 23 @RequestMapping("/list") 24 public String list(Model map){ 25 List<City> list = cityService.findAll(); 26 27 map.addAttribute("list",list); 28 return "list"; 29 } 30 31 @RequestMapping("/add") 32 public String add(@RequestParam("id") Integer id,@RequestParam("name") String name,Model map){ 33 System.out.println("===============add============"); 34 String success = cityService.add(id,name); 35 System.out.println(success); 36 map.addAttribute("success",success); 37 return "add"; 38 } 39 40 @RequestMapping("/addPage") 41 public String addPage(){ 42 return "add"; 43 } 44 } 45 46 ########################## 47 package com.mashibing.springboot03.service; 48 49 import com.mashibing.springboot03.dao.CityDao; 50 import com.mashibing.springboot03.domain.City; 51 import org.springframework.beans.factory.annotation.Autowired; 52 import org.springframework.stereotype.Service; 53 54 import java.util.List; 55 56 @Service 57 public class CityService { 58 59 @Autowired 60 CityDao cityDao; 61 62 public List<City> findAll() { 63 64 return cityDao.findAll(); 65 } 66 67 public String add(Integer id, String name) { 68 69 City city = new City(); 70 city.setId(id); 71 city.setName(name); 72 73 try { 74 cityDao.save(city); 75 return "保存成功"; 76 } catch (Exception e) { 77 return "保存失败"; 78 } 79 80 } 81 } 82 83 ################# 84 package com.mashibing.springboot03.dao; 85 86 import com.mashibing.springboot03.domain.City; 87 import org.springframework.stereotype.Repository; 88 89 import java.util.*; 90 91 @Repository 92 public class CityDao { 93 94 /** 95 * 在内存里虚拟出来一份数据 96 * List 97 * Map 98 *需要保证线程安全 99 * 100 * 101 * @return 102 */ 103 104 //把Map变成同步Map 105 static Map<Integer,City> dataMap = Collections.synchronizedMap(new HashMap<Integer,City>()); 106 107 public List<City> findAll() { 108 109 return new ArrayList<>(dataMap.values()); 110 } 111 112 public void save(City city) throws Exception { 113 City data = dataMap.get(city.getId()); 114 if (data != null){ 115 throw new Exception("数据已经存在"); 116 } else { 117 dataMap.put(city.getId(),city); 118 System.out.println("数据已经添加"); 119 } 120 121 } 122 } 123 ######################### 124 package com.mashibing.springboot03.domain; 125 126 public class City { 127 128 private Integer id; 129 130 private String name; 131 132 public Integer getId() { 133 return id; 134 } 135 136 public void setId(Integer id) { 137 this.id = id; 138 } 139 140 public String getName() { 141 return name; 142 } 143 144 public void setName(String name) { 145 this.name = name; 146 } 147 } 148 ################### 149 add.html 150 <html> 151 152 City Add 153 <hr> 154 155 <h1 th:text="${success}"></h1> 156 <hr> 157 <!--这里的add就是表单提交到controller中/add--> 158 <form action="add" method="post"> 159 id : <input name="id" type="text"> 160 <br/> 161 name: <input name="name" type="text"> 162 <br/> 163 <input type="submit" value="提交"> 164 </form> 165 166 </html> 167 168 ########################## 169 list.html 170 <html> 171 172 <body> 173 City list 174 <hr> 175 176 <table> 177 <tr> 178 <th>ID</th> 179 <th>NAME</th> 180 </tr> 181 182 <tr th:each="city : ${list}"> 183 <!--去访问到实体类里面的getId方法--> 184 <td th:text="${city.id}"></td> 185 <td th:text="${city.name}">/td> 186 </tr> 187 </table> 188 </body> 189 </html> 190 191 ######################### 192 # 项目的启动服务端口号 193 server.port=80 194 # 项目的起始路径 195 spring.thymeleaf.prefix= classpath:/templates/ 196 spring.thymeleaf.mode=LEGACYHTML5 # 不进未关闭标签检查,需配合nekohtml使用 197 spring.thymeleaf.cache=false
5、springboot + thymeleaf + jpa
1 package com.mashibing.springbootmvc01.controller; 2 3 import com.mashibing.springbootmvc01.entity.City; 4 import com.mashibing.springbootmvc01.service.CityService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 11 import java.util.List; 12 13 @Controller 14 @RequestMapping("/city") 15 public class MainController { 16 17 @Autowired 18 CityService cityService; 19 @RequestMapping("list") 20 public String list(Model map){ 21 List<City> list = cityService.finAll(); 22 map.addAttribute("list",list); 23 return "list"; 24 } 25 26 @RequestMapping("list/{id}") 27 public String getOne(@PathVariable("id") Integer id,Model map){ 28 City city = cityService.findOne(id); 29 map.addAttribute("city",city); 30 return "one"; 31 } 32 } 33 34 ########################3 35 package com.mashibing.springbootmvc01.service; 36 37 import com.mashibing.springbootmvc01.entity.City; 38 import com.mashibing.springbootmvc01.dao.CityRepository; 39 import org.springframework.beans.factory.annotation.Autowired; 40 import org.springframework.stereotype.Service; 41 42 import java.util.List; 43 import java.util.Optional; 44 45 @Service 46 public class CityService { 47 48 49 @Autowired 50 CityRepository cityRepo; 51 public List<City> finAll() { 52 List<City> all = cityRepo.findAll(); 53 return all; 54 } 55 56 public City findOne(Integer id) { 57 58 return cityRepo.getOne(id); 59 } 60 } 61 62 ######################## 63 package com.mashibing.springbootmvc01.dao; 64 65 import com.mashibing.springbootmvc01.entity.City; 66 import org.springframework.data.jpa.repository.JpaRepository; 67 import org.springframework.stereotype.Repository; 68 69 /** 70 * JpaRepository<City,Integer> 第一个表示哪个类,第二个表示id的类型 71 */ 72 73 public interface CityRepository extends JpaRepository<City,Integer>{ 74 75 } 76 77 ####################### 78 package com.mashibing.springbootmvc01.entity; 79 80 import org.springframework.beans.factory.annotation.Value; 81 82 import javax.persistence.*; 83 84 /** 85 * @Entity表示和数据库表关联 86 */ 87 @Entity 88 @Table(name="city") 89 public class City { 90 //@Id表示标注表里面的id,@GeneratedValue表示告诉他生成策略 91 @Id 92 @GeneratedValue(strategy = GenerationType.IDENTITY) 93 private Integer id; 94 95 private String name; 96 97 public Integer getId() { 98 return id; 99 } 100 101 public void setId(Integer id) { 102 this.id = id; 103 } 104 105 public String getName() { 106 return name; 107 } 108 109 public void setName(String name) { 110 this.name = name; 111 } 112 } 113 114 ########################### 115 list.html 116 <html> 117 118 <body> 119 City list 120 <hr> 121 122 <table border="1"> 123 <tr> 124 <th>ID</th> 125 <th>NAME</th> 126 </tr> 127 <tr th:each="city : ${list}"> 128 <th th:text="${city.id}"></th> 129 <th th:text="${city.name}"></th> 130 </tr> 131 </table> 132 </body> 133 </html> 134 ################### 135 # 项目的启动服务端口号 136 server.port=80 137 # 项目的起始路径 138 spring.thymeleaf.prefix= classpath:/templates/ 139 spring.thymeleaf.mode=LEGACYHTML5 # 不进未关闭标签检查,需配合nekohtml使用 140 spring.thymeleaf.cache=false 141 142 # 数据库配置 143 spring.datasource.url=jdbc:mysql://localhost:3306/world?characterEncoding=utf8&useSSL=false&serverTimezone=UTC 144 spring.datasource.username=root 145 spring.datasource.password=123456
6、springboot + jsp
<!--用jsp--> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jasper--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!--用jsp-->
1 package com.mashibing.springbootmvc01.controller; 2 3 import com.mashibing.springbootmvc01.entity.City; 4 import com.mashibing.springbootmvc01.service.CityService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 import java.util.List; 13 14 @Controller 15 @RequestMapping("/city") 16 public class MainController { 17 18 @Autowired 19 CityService cityService; 20 @RequestMapping("list") 21 public String list(Model map){ 22 List<City> list = cityService.finAll(); 23 map.addAttribute("list",list); 24 return "list"; 25 } 26 27 @RequestMapping("list/{id}") 28 public String getOne(@PathVariable("id") Integer id,Model map){ 29 City city = cityService.findOne(id); 30 map.addAttribute("city",city); 31 return "list1"; 32 } 33 } 34 35 ############################### 36 package com.mashibing.springbootmvc01.service; 37 38 import com.mashibing.springbootmvc01.entity.City; 39 import com.mashibing.springbootmvc01.dao.CityRepository; 40 import org.springframework.beans.factory.annotation.Autowired; 41 import org.springframework.stereotype.Service; 42 43 import java.util.List; 44 import java.util.Optional; 45 46 @Service 47 public class CityService { 48 49 50 @Autowired 51 CityRepository cityRepo; 52 public List<City> finAll() { 53 List<City> all = cityRepo.findAll(); 54 return all; 55 } 56 57 public City findOne(Integer id) { 58 59 return cityRepo.getOne(id); 60 } 61 } 62 63 ############################ 64 package com.mashibing.springbootmvc01.dao; 65 66 import com.mashibing.springbootmvc01.entity.City; 67 import org.springframework.data.jpa.repository.JpaRepository; 68 import org.springframework.stereotype.Repository; 69 70 /** 71 * JpaRepository<City,Integer> 第一个表示哪个类,第二个表示id的类型 72 */ 73 74 public interface CityRepository extends JpaRepository<City,Integer>{ 75 76 } 77 78 ############################ 79 package com.mashibing.springbootmvc01.entity; 80 81 import org.springframework.beans.factory.annotation.Value; 82 83 import javax.persistence.*; 84 85 /** 86 * @Entity表示和数据库表关联 87 */ 88 @Entity 89 @Table(name="city") 90 public class City { 91 //@Id表示标注表里面的id,@GeneratedValue表示告诉他生成策略 92 @Id 93 @GeneratedValue(strategy = GenerationType.IDENTITY) 94 private Integer id; 95 96 private String name; 97 98 public Integer getId() { 99 return id; 100 } 101 102 public void setId(Integer id) { 103 this.id = id; 104 } 105 106 public String getName() { 107 return name; 108 } 109 110 public void setName(String name) { 111 this.name = name; 112 } 113 } 114 115 ######################### 116 list.html 117 <html> 118 119 <body> 120 City list 121 <hr> 122 123 <table border="1"> 124 <tr> 125 <th>ID</th> 126 <th>NAME</th> 127 </tr> 128 <tr th:each="city : ${list}"> 129 <th th:text="${city.id}"></th> 130 <th th:text="${city.name}"></th> 131 </tr> 132 </table> 133 </body> 134 135 </html> 136 ######################### 137 list1.jsp 138 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 139 <!doctype html> 140 <html lang="en"> 141 <head> 142 <meta charset="UTF-8"> 143 <meta name="viewport" 144 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 145 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 146 <title>Document</title> 147 </head> 148 <body> 149 hi 150 </body> 151 </html>
pom.xml
<!--用jsp--> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jasper--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!--用jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!doctype html> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <table> <tr> <th>id</th> <th>name</th> </tr> <c:forEach items="${list}" var="city"> <tr> <td>${city.id}</td> <td>${city.name}</td> </tr> </c:forEach> </table> </body> </html>