spring boot
Spring Boot 是一个快速开发框架、Spring boot开启了自动装配、从而简化代码开发、不需要编写各种该配置文件、只需要引入相关依赖就可以迅速搭建一个应用
不需要web.xml 不需要springmvc.xml 不需要tomcat、spring boot本身内嵌tomcat 不需要JSON解析、支持REST
spring boot简单搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.7.RELEASE</version > </parent > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies >
1 2 3 4 5 6 @SpringBootApplication public class Appliaction { public static void main (String[] args) { SpringApplication.run(Appliaction.class,args); } }
spring boot整合JSP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.7.RELEASE</version > </parent > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <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 > <dependency > <groupId > jstl</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.18.6</version > <scope > provided</scope > </dependency > </dependencies >
1 2 3 4 5 6 7 8 9 10 @Data @AllArgsConstructor @NoArgsConstructor public class Student { private Long id; private String name; private Integer sex; private Integer age; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 @Controller @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentRepositoryImpl studentRepository; @GetMapping(value = "/index") public ModelAndView findAll () { ModelAndView modelAndView = new ModelAndView("index" ,"collection" ,studentRepository.findAll()); return modelAndView; } @GetMapping(value = "/findById/{id}") public ModelAndView findById (@PathVariable("id") Long id) { ModelAndView modelAndView = new ModelAndView("update" ,"student" ,studentRepository.findById(id)); return modelAndView; } @DeleteMapping(value = "/deleteById/{id}") public String deleteById (@PathVariable("id") Long id) { studentRepository.deleteById(id); return "redirect:/student/index" ; } @PostMapping(value = "/save") public void save (@RequestBody Student student) { studentRepository.saveAndUpdate(student); } @PutMapping(value = "/update") public String update (Student student) { studentRepository.saveAndUpdate(student); return "redirect:/student/index" ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @Repository public class StudentRepositoryImpl implements StudentRepository { private static Map<Long, Student> map = null ; static { map = new HashMap<>(); map.put(1L ,new Student(1L ,"xiaobo" ,0 ,21 )); map.put(2L ,new Student(2L ,"h" ,1 ,21 )); map.put(3L ,new Student(3L ,"pangzi" ,0 ,22 )); } @Override public Collection<Student> findAll () { return map.values(); } @Override public Student findById (Long id) { return map.get(id); } @Override public void deleteById (Long id) { map.remove(id); } @Override public void saveAndUpdate (Student student) { map.put(student.getId(),student); } }
1 2 3 4 5 6 7 server: port: 8080 spring: mvc: view: prefix: / suffix: .jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script> <html> <head> <title>首页</title> </head> <body> <h1>学生信息</h1> <table> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>操作</th> </tr> <c:forEach items="${collection }" var ="student" > <tr> <td>${student.id }</td> <td>${student.name }</td> <c:if test="${student.sex == 0}" > <td>男</td> </c:if > <c:if test="${student.sex == 1}" var ="女" > <td>女</td> </c:if > <td>${student.age }</td> <td> <form action="/student/findById/${student.id }" method="get" > <input type="submit" value="修改" /> </form> </td> <td> <form action="/student/deleteById/${student.id }" method="post" > <input type="hidden" name="_method" value="delete" /> <input type="submit" value="删除" /> </form> </td> </tr> </c:forEach> </table> <input id="addBut" type="button" value="添加学生" /> </body> </html>
spring boot 整合thymeleaf模板
1 2 3 4 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-thymeleaf</artifactId > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Controller @RequestMapping(value = "/student") public class StudentController { private static List<Student> list = null ; static { list = new ArrayList<>(); list.add(new Student(1L ,"xiaobo" ,21 )); list.add(new Student(2L ,"pangzi" ,22 )); list.add(new Student(3L ,"h" ,21 )); } @GetMapping(value = "/index") public String findAll (Model model) { model.addAttribute("list" ,list); return "index" ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html xmlns:th ="http://www.thymeleaf.org" > </html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > 首页</title > </head > <body > <h1 > 学生信息</h1 > <table > <tr > <th > 编号</th > <th > 姓名</th > <th > 年龄</th > </tr > <tr th:each ="student:${list }" > <td th:text ="${student.id }" > </td > <td th:text ="${student.name}" > </td > <td th:text ="${student.age}" > </td > </tr > </table > </body > </html >
1 2 3 4 5 6 7 8 9 server: port: 8080 spring: thymeleaf: prefix: classpath:/templates/ suffix: .html mode: HTML encoding: UTF-8
Thymeleaf语法
1 2 3 <p th:text ="${name}" > </p > <p th:text ="'学生姓名是'+${name}+ good" > </p > <p th:text ="|学生姓名是,${name}|" > </p >
1 2 <p th:if ="${flag == true}" th:text ="if判断成立" > </p > <p th:unless ="${flag != true}" th:text ="unless判断成立" > </p >
1 2 3 4 5 6 7 <tr th:each ="student,stat:${list}" > <td th:text ="${stat.index}" > </td > <td th:text ="${stat.count}" > </td > <td th:text ="${student.id}" > </td > <td th:text ="${student.name}" > </td > <td th:text ="${student.age}" > </td > </tr >
index 集合中元素的index(从0开始) count 集合中元素的count(从1开始) size 集合的大小 current 当前迭代变量 even/odd 当前迭代是否为偶数/奇数(从0开始计算) first 当前迭代的元素是否是第一个 last 当前迭代的元素是否是最后一个
1 <a th:href ="@{http://www.baidu.com}" > 跳转</a >
1 <input th:value ="${age gt 30?'中年':'青年'}" />
gt great than 大于 ge great equal 大于等于 eq equal 等于 lt less than 小于 le less equal 小于等于 ne not equal 不等于
1 2 3 4 5 <div th:switch ="${gender}" > <p th:case ="女" > 女</p > <p th:case ="男" > 男</p > <p th:case ="*" > 未知</p > </div >
基本对象
1 2 3 <p th:text ="${#request.getAttribute('request')}" > </p > <p th:text ="${#session.getAttribute('session')}" > </p > <p th:text ="${#locale.country}" > </p >
#ctx :上下文对象 #vars:上下文变量 #locale:区域对象 #request:HttpServletRequest 对象 #response:HttpServletResponse 对象 #session:HttpSession 对象 #servletContext:ServletContext 对象
内嵌对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <p th:text ="${#dates.format(date,'yyyy-MM-dd HH:mm:sss')}" > </p > <p th:text ="${#dates.createToday()}" > </p > <p th:text ="${#dates.createNow()}" > </p > <p th:text ="${#strings.isEmpty(name)}" > </p > <p th:text ="${#lists.isEmpty(users)}" > </p > <p th:text ="${#strings.length(name)}" > </p > <p th:text ="${#strings.concat(name,name,name)}" > </p > <p th:text ="${#strings.randomAlphanumeric(count)}" > </p >
dates:java.util.Date 的功能方法 calendars:java.util.Calendar 的功能方法 numbers:格式化数字 strings:java.lang.String 的功能方法 objects:Object 的功能方法 bools:对布尔求值的方法 arrays:操作数组的功能方法 lists:操作集合的功能方法 sets:操作集合的功能方法 maps:操作集合的功能方法
spring boot 数据校验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Data @AllArgsConstructor @NoArgsConstructor public class User { @NotNull(message = "id不能为空") private Long id; @NotEmpty(message = "用户名不能为空") @Length(min = 3,message = "用户名长度不能小于3位") private String username; @NotEmpty(message = "密码不能为空") @Length(min = 6,message = "密码长度不能小于6位") private String password; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @RestController @RequestMapping(value = "/user") public class UserController { @RequestMapping(value = "/login") public void validatorUser (@Valid User user, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { List<ObjectError> allErrors = bindingResult.getAllErrors(); for (ObjectError objectError : allErrors) { System.out.println(objectError.getCode()); System.out.println(objectError.getDefaultMessage()); } } } }
spring boot整合JDBC
1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.11</version > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 @RestController @RequestMapping(value = "/user") public class UserController { @Autowired private UserRepositoryImpl userRepository; @GetMapping(value = "/findAll") public List<User> findAll (Model model) { return userRepository.findAll(); } @GetMapping(value = "/findById/{id}") public User findById (@PathVariable("id") Long id) { System.out.println(userRepository.findById(id)); return userRepository.findById(id); } @PostMapping(value = "/save") public void save (@RequestBody User user) { userRepository.save(user); } @DeleteMapping(value = "/delete/{id}") public void deleteById (@PathVariable("id") Long id) { userRepository.deleteById(id); } @PutMapping(value = "/update") public void update (@RequestBody User user) { userRepository.update(user); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @Repository public class UserRepositoryImpl implements UserRepository { @Autowired private JdbcTemplate jdbcTemplate; @Override public List<User> findAll () { return jdbcTemplate.query("SELECT * FROM user" ,new BeanPropertyRowMapper<>(User.class)); } @Override public User findById (Long id) { return jdbcTemplate.queryForObject("SELECT * FROM user WHERE id = ?" , new Object[]{id},new BeanPropertyRowMapper<>(User.class)); } @Override public void save (User user) { jdbcTemplate.update("INSERT INTO user(id,username,password) VALUES(?,?,?)" ,user.getId(),user.getUsername(),user.getPassword()); } @Override public void deleteById (Long id) { jdbcTemplate.update("DELETE FROM user WHERE id = ?" ,id); } @Override public void update (User user) { jdbcTemplate.update("UPDATE user SET username = ?,password=? WHERE id = ?" ,user.getUsername(),user.getPassword(),user.getId()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 server: port: 8080 spring: thymeleaf: prefix: classpath:/templates/ suffix: .html mode: HTML encoding: UTF-8 datasource: url: jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true&charcaterEncoding=UTF-8&serverTimezone=UTC username: root password: xiaobo driver-class-name: com.mysql.cj.jdbc.Driver
springboot 整合Mybatis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.11</version > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 1.3.1</version > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 mybatis: mapper-locations: classpath:/mapping/*.xml type-aliases-package: com.dream.xiaobo.pojo server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true&charcaterEncoding=UTF-8&serverTimezone=UTC username: root password: xiaobo driver-class-name: com.mysql.cj.jdbc.Driver
1 2 3 4 5 6 7 8 9 10 11 12 public interface UserRepository { public List<User> findAll () ; public User findById (Long id) ; public void save (User user) ; public void deleteById (Long id) ; public void update (User user) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.dream.xiaobo.repository.UserRepository" > <select id ="findAll" resultType ="User" > SELECT * FROM user </select > <select id ="findById" resultType ="User" parameterType ="java.lang.Long" > SELECT * FROM user WHERE id = #{id } </select > <insert id ="save" parameterType ="User" > INSERT INTO user(username,password) VALUES(#{username },#{password }) </insert > <update id ="update" parameterType ="User" > UPDATE user SET username = #{username},password = #{password } WHERE id = #{id } </update > <delete id ="deleteById" parameterType ="java.lang.Long" > DELETE FROM user WHERE id = #{id } </delete > </mapper >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @RestController @RequestMapping(value = "/user") public class UserController { @Autowired private UserRepository userRepository; @GetMapping(value = "/findAll") public List<User> findAll (Model model) { return userRepository.findAll(); } @GetMapping(value = "/findById/{id}") public User findById (@PathVariable("id") Long id) { System.out.println(userRepository.findById(id)); return userRepository.findById(id); } @PostMapping(value = "/save") public void save (@RequestBody User user) { userRepository.save(user); } @DeleteMapping(value = "/delete/{id}") public void deleteById (@PathVariable("id") Long id) { userRepository.deleteById(id); } @PutMapping(value = "/update") public void update (@RequestBody User user) { userRepository.update(user); } }
1 2 3 4 5 6 7 8 @SpringBootApplication @MapperScan("com.dream.xiaobo.repository") public class Application { public static void main (String[] args) { SpringApplication.run(Application.class,args); } }