引言:
本文主要分享了有关SpringBoot相关的知识,包括:SpringBoot的简介、SpringBoot环境的搭建、简单是SpringBoot案例、lombok的简单应用、在SpringBoot中加入HTML页面时的操作、SpringBoot的目录解析、SpringBoot整合SSM_html+vue+jquery(附源码)、thymeleaf模板的简述及使用、SpringBoot整合SSM_thymeleaf(附源码);
SpringBoot设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置;SpringBoot不是一门新技术,只是将Spring、SpringMVC等封装起来,就是一个工具集;
SpringBoot特点:
不需要模板化的配置整合三方框架时,只需要导入相应的starter依赖包,会自动整合默认只有一个.properties是我配置文件部署工程时采用的是jar包的方式,内部自动依赖Tomcat容器,提供了多环境的配置SpringCloud需要建立在SpringBoot的基础上lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些Java类中的大量样板代码,lombok能够达到的效果就是在源码中不需要写一些通用的方法,但是在编译生成的字节码文件中会帮我们生成这些方法;
resources/templates下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML页面</title> </head> <body> <h2>SpringBoot Html View</h2> <form action="add" method="POST"> UserName:<input type="text" name="username"/><br/> Password:<input type="password" name="password"/><br/> Tel:<input type="text" name="tel"/><br/> <input type="submit" value="add"/> </form> </body> </html>当访问localhost:8080时显示该页面
package com.sx.kak.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by Kak on 2020/8/31. */ @Controller public class PageController { @RequestMapping("/") public String showIndex(){ return "index"; } }xml与html区别与联系:
联系:二者均可以使用浏览器解析,都在内存中形成dom模型区别: xml:只能有一个root(根)节点xml:自定义标记;而html预定义标记xml:区分大小写,html不区分xml:中标记的属性值必须使用引号,而html可以不用xml:标记必须关闭;而html可以不关闭xml:不可以交叉嵌套在po包下编写实体类
package com.sx.kak.po; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * Created by Kak on 2020/8/31. */ @Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private String sex; private String age; }在mapper中编写StudentMapper
package com.sx.kak.mapper; import com.sx.kak.po.Student; import java.util.List; /** * Created by Kak on 2020/8/31. */ public interface StudentMapper { public List<Student> findAll(); public Student findOneById(int id); public void addStudent(Student student); public void updateStudent(Student student); public void deleteStudent(int id); }在resources/mapping下编写
<?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.sx.kak.mapper.StudentMapper"> <select id="findAll" resultType="com.sx.kak.po.Student"> select id,name,sex,age from student </select> <select id="findOneById" parameterType="int" resultType="com.sx.kak.po.Student"> SELECT id,name,sex,age FROM student WHERE id=#{id} </select> <insert id="addStudent" parameterType="com.sx.kak.po.Student"> insert into student (id,name,sex,age) VALUES (#{id},#{name},#{sex},#{age}) </insert> <update id="updateStudent" parameterType="com.sx.kak.po.Student"> UPDATE student set name=#{name},sex=#{sex},age=#{age} WHERE id=#{id} </update> <delete id="deleteStudent" parameterType="int"> DELETE FROM student where id=#{id} </delete> </mapper>设置mapper扫描基础包路径
package com.sx.kak; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //设置mapper扫描基础包路径 @MapperScan(basePackages = {"com.sx.kak.mapper"}) public class SpringbootSsmApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSsmApplication.class, args); } }在service下
package com.sx.kak.server; import com.sx.kak.po.Student; import java.util.List; /** * Created by Kak on 2020/8/31. */ public interface StudentService { public List<Student> findAllService(); public Student findOneStudentService(int id); public void addStudentService(Student student); public void updateStudentService(Student student); public void deleteStudentService(int id); }在service/impl下
package com.sx.kak.server.impl; import com.sx.kak.mapper.StudentMapper; import com.sx.kak.po.Student; import com.sx.kak.server.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by Kak on 2020/8/31. */ @Service//标注service实现对象有spring接管 public class StudentServiceImpl implements StudentService{ //注入mapper接口 @Autowired(required = false) private StudentMapper studentMapper; @Override public List<Student> findAllService() { List<Student> all = studentMapper.findAll(); return all; } @Override public Student findOneStudentService(int id) { Student oneById = studentMapper.findOneById(id); return oneById; } @Override public void addStudentService(Student student) { studentMapper.addStudent(student); } @Override public void updateStudentService(Student student) { studentMapper.updateStudent(student); } @Override public void deleteStudentService(int id) { studentMapper.deleteStudent(id); } }用于视图跳转
package com.sx.kak.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by Kak on 2020/8/31. */ @Controller public class ViewController { @RequestMapping("/studentView") public String showList(){ return "showAllStudent"; } @RequestMapping("/addStudent") public String addStudent(){ return "addStudent"; } }业务控制层
package com.sx.kak.controller; import com.sx.kak.po.Student; import com.sx.kak.server.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by Kak on 2020/8/31. */ @Controller public class StudentController { //注入service接口 @Autowired(required = false) private StudentService studentService; //全查 @RequestMapping(value = "/students",method = RequestMethod.GET) @ResponseBody public ResponseEntity<List<Student>> findAllStu(){ List<Student> allService = studentService.findAllService(); if(allService!=null){ return new ResponseEntity<List<Student>>(allService, HttpStatus.OK); }else { return new ResponseEntity<List<Student>>(allService, HttpStatus.NOT_FOUND); } } //增加 @RequestMapping(value = "addStudents",method = RequestMethod.POST) public String addStu(Student student){ studentService.addStudentService(student); return "redirect:studentView"; } //单查 @RequestMapping(value = "findByIdStudents",method = RequestMethod.GET) @ResponseBody public ResponseEntity<Student> findByIdStu(@RequestParam("id") int id){ Student oneStudentService = studentService.findOneStudentService(id); if(oneStudentService!=null){ return new ResponseEntity<Student>(oneStudentService, HttpStatus.OK); }else{ return new ResponseEntity<Student>( HttpStatus.NOT_FOUND); } } //修改 @RequestMapping(value = "/updateStudents",method = RequestMethod.POST) @ResponseBody public ResponseEntity<Student> updateStu(@RequestBody Student student){ System.out.println(student); studentService.updateStudentService(student); return new ResponseEntity<Student>(HttpStatus.OK); } //删除 @RequestMapping(value = "deleteStudents",method = RequestMethod.GET) @ResponseBody public ResponseEntity<Student> deleteStu(@RequestParam("id") int id){ studentService.deleteStudentService(id); return new ResponseEntity<Student>(HttpStatus.OK); } }Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎,可以处理HTML、XML、JavaScript、CSS等;目的是提供优雅且高度可维护的模板创建方式,提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器等功能;
可以用th标签动态替换掉静态数据
<!--thymeleaf操作--> <tr th:each="s:${students}"> <td th:text="${s.id}"></td> <td th:text="${s.name}"></td> <td th:text="${s.sex}"></td> <td th:text="${s.age}"></td> <td> <a href="findByIdStudents?id=10" th:href="@{findByIdStudents(id=${s.id})}">修改</a> <a href="deleteStudents?id=10" th:href="@{deleteStudents(id=${s.id})}">删除</a> </td> </tr> <!--vue操作--> <tr v-for="s in students"> <td> {{s.id}}</td> <td> {{s.name}}</td> <td> {{s.age}}</td> <td> {{s.sex}}</td> <td> <button type="button" @click="findOneById(s.id)">修改</button> <button type="button" @click="delectStudent(s.id)">删除</button> </td> </tr>在po包下编写实体类
package com.sx.kak.po; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * Created by Kak on 2020/8/31. */ @Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private String sex; private String age; }在mapper中编写StudentMapper
package com.sx.kak.mapper; import com.sx.kak.po.Student; import java.util.List; /** * Created by Kak on 2020/8/31. */ public interface StudentMapper { public List<Student> findAll(); public Student findOneById(int id); public void addStudent(Student student); public void updateStudent(Student student); public void deleteStudent(int id); }在resources/mapping下编写
<?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.sx.kak.mapper.StudentMapper"> <select id="findAll" resultType="com.sx.kak.po.Student"> select id,name,sex,age from student </select> <select id="findOneById" parameterType="int" resultType="com.sx.kak.po.Student"> SELECT id,name,sex,age FROM student WHERE id=#{id} </select> <insert id="addStudent" parameterType="com.sx.kak.po.Student"> insert into student (id,name,sex,age) VALUES (#{id},#{name},#{sex},#{age}) </insert> <update id="updateStudent" parameterType="com.sx.kak.po.Student"> UPDATE student set name=#{name},sex=#{sex},age=#{age} WHERE id=#{id} </update> <delete id="deleteStudent" parameterType="int"> DELETE FROM student where id=#{id} </delete> </mapper>设置mapper扫描基础包路径
package com.sx.kak; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //设置mapper扫描基础包路径 @MapperScan(basePackages = {"com.sx.kak.mapper"}) public class SpringbootSsmApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSsmApplication.class, args); } }在service下
package com.sx.kak.server; import com.sx.kak.po.Student; import java.util.List; /** * Created by Kak on 2020/8/31. */ public interface StudentService { public List<Student> findAllService(); public Student findOneStudentService(int id); public void addStudentService(Student student); public void updateStudentService(Student student); public void deleteStudentService(int id); }在service/impl下
package com.sx.kak.server.impl; import com.sx.kak.mapper.StudentMapper; import com.sx.kak.po.Student; import com.sx.kak.server.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by Kak on 2020/8/31. */ @Service//标注service实现对象有spring接管 public class StudentServiceImpl implements StudentService{ //注入mapper接口 @Autowired(required = false) private StudentMapper studentMapper; @Override public List<Student> findAllService() { List<Student> all = studentMapper.findAll(); return all; } @Override public Student findOneStudentService(int id) { Student oneById = studentMapper.findOneById(id); return oneById; } @Override public void addStudentService(Student student) { studentMapper.addStudent(student); } @Override public void updateStudentService(Student student) { studentMapper.updateStudent(student); } @Override public void deleteStudentService(int id) { studentMapper.deleteStudent(id); } }业务控制层
package com.sx.kak.controller; import com.sx.kak.po.Student; import com.sx.kak.server.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by Kak on 2020/8/31. */ @Controller public class StudentController { //注入service接口 @Autowired(required = false) private StudentService studentService; @RequestMapping("/addStudentView") public String addStudent(){ return "addStudent"; } //全查 @RequestMapping(value = "/students",method = RequestMethod.GET) public String findAllStu(Model model){ List<Student> allService = studentService.findAllService(); model.addAttribute("students",allService); return "showAllStudent"; } //增加 @RequestMapping(value = "addStudents",method = RequestMethod.POST) public String addStu(Model model,Student student){ studentService.addStudentService(student); return "redirect:students"; } //单查 @RequestMapping(value = "findByIdStudents",method = RequestMethod.GET) public String findByIdStu(Model model,int id){ Student oneStudentService = studentService.findOneStudentService(id); model.addAttribute("student",oneStudentService); return "updateStudent"; } //修改 @RequestMapping(value = "/updateStudents",method = RequestMethod.POST) public String updateStu(Model model,Student student){ studentService.updateStudentService(student); return "redirect:students"; } //删除 @RequestMapping(value = "deleteStudents",method = RequestMethod.GET) public String deleteStu(Model model, int id){ studentService.deleteStudentService(id); return "redirect:students"; } }