最近在深啃jvm虚拟机,主要方法是阅读《深入java虚拟机》这本书,写的很形象深入,简单易懂。今天上web课程老师讲到servlet时突然想到boot的页面跳转与数据响应。
之前的springmvc在boot中也有集成,但是boot的常用页面是html5,不是jsp页面,所以对于数据的显示与获取有了一定的不适。不过这个得到了解决,入手的springboot+vue实战开发这本书中对于boot开发简单的项目讲解的比较好。
基于上一次静态资源访问的基础上,我们搭建一个具备增删改查的图书信息测试。
一、dao层
@Mapper
public interface BookDao {
// 利用mybatis框架写好需要使用的数据库操作方法
/*查看全部*/
@Select("select * from test.bootsm_book")
List<Book> allBook();
/*按条件查询*/
@Select("select * from test.bootsm_book where bookId = #{bookId}")
Book queryById(Integer bookId);
/*修改*/
@Update("update test.bootsm_book set bookName=#{bookName},bookPrice=#{bookPrice},bookType=#{bookType} where bookId=#{bookId}")
Boolean update(Book book);
/*插入*/
@Insert("insert into test.bootsm_book values (#{bookId},#{bookName},#{bookPrice},#{bookType})")
Boolean save(Book book);
/*按条件删除*/
@Delete("delete from test.bootsm_book where bookId=#{bookId}")
Boolean delete(Integer bookId);
/*实现登录*/
@Select("select * from test.bootsm_book where bookId=#{bookId} and bookName=#{bookName}")
Boolean login(Integer bookId,String bookName);
/*实现*/
}
二、service层
public interface BookService {
List<Book> allBook();
Book queryById(Integer bookId);
Boolean update(Book book);
Boolean save(Book book);
Boolean delete(Integer bookId);
Boolean login(Integer bookId,String bookName);
}
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public List<Book> allBook() {
return bookDao.allBook();
}
@Override
public Book queryById(Integer bookId) {
return bookDao.queryById(bookId);
}
@Override
public Boolean update(Book book) {
return bookDao.update(book);
}
@Override
public Boolean save(Book book) {
return bookDao.save(book);
}
@Override
public Boolean delete(Integer bookId) {
return bookDao.delete(bookId);
}
@Override
public Boolean login(Integer bookId, String bookName) {
return bookDao.login(bookId,bookName);
}
}
三、controller层
@Controller
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("books")
public ModelAndView allBook(){
ModelAndView modelAndView = new ModelAndView();
List<Book> bookList = bookService.allBook();
modelAndView.addObject("bookList",bookList);
modelAndView.setViewName("list");
return modelAndView;
}
@RequestMapping("query")
@ResponseBody
public Book query(Integer bookId){
return bookService.queryById(bookId);
}
/*先到add页面提交保存数据,再由真正的save方法接收数据并操作数据库*/
@RequestMapping("add")
public ModelAndView add(){
ModelAndView modelAndView = new ModelAndView("add");
return modelAndView;
}
@RequestMapping("save")
public ModelAndView save(Book book){
ModelAndView modelAndView = new ModelAndView();
bookService.save(book);
List<Book> bookList = bookService.allBook();
modelAndView.addObject("bookList",bookList);
modelAndView.setViewName("list");
return modelAndView;
}
/*测试使用模板引擎实现的页面跳转,很强!*/
@RequestMapping("test")
public ModelAndView test(){
ModelAndView modelAndView = new ModelAndView("test");
return modelAndView;
}
}
四、pojo模型
@Data
public class Book {
private int bookId;
private String bookName;
private String bookPrice;
private String bookType;
}
五、templates文件夹
list.html
<!DOCTYPE html>
<html lang="en" xmlns:span="http://www.w3.org/1999/html" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>显示书目列表</h2>
<span th:text="${bookList}"></span>
<h3>搜索书籍</h3>
<form action="/query" method="post">
<p><input type="text" name="bookId"></p>
<p><input type="submit"></p>
</form>
<a href="/add">增加新书信息</a>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="Thymeleaf 官网网址">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>注册新书目信息</h2>
<form method="post" action="/save">
<p>书号<input type="text" name="bookId"></p>
<p>书名<input type="text" name="bookName"></p>
<P>价格<input type="text" name="bookPrice"></P>
<p>类型<input type="text" name="bookType"></p>
<p>提交<input type="submit"></p>
</form>
</body>
</html>
test.html4
<!DOCTYPE html>
<html lang="zh" xmlns:th="Thymeleaf 官网网址">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
test success!
</body>
</html>
六、application.yml
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
# 提供的模板引擎设置热部署,不使用cache,部署的文件类型是HTML5
thymeleaf:
cache: false
mode: HTML5
# mvc:
# static-path-pattern: /static/*
七、跳转所需的静态模板引擎坐标
<!--导入静态加载的模板引擎坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
八、测试页面(为了测试页面跳转并没有加上修饰)
学习多线程、并发编程、jvm最近更新boot可能有点慢。补一补java高级再深入spring框架的源码,希望计划能顺利。天气越热心就浮躁起来了。
版权声明:本文为m0_59588838原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。