笔记66 Spring Boot快速入门(六)
SpringBoot中使用Mybatis
一、注解方式
1.创建映射文件CategoryMapper.java
使用注解@Mapper 表示这是一个Mybatis Mapper接口。
使用@Select注解表示调用findAll方法会去执行对应的sql语句。
package com.example.springbootmybatisdemo.mapper; import com.example.springbootmybatisdemo.pojo.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper
public interface CategoryMapper {
@Select("select * from category")
List<Category> findAll();
}
2.创建CategoryController.java
有问题:无法自动加载mapper,显示有错误,但是运行结果正确。
package com.example.springbootmybatisdemo.controller; import com.example.springbootmybatisdemo.mapper.CategoryMapper;
import com.example.springbootmybatisdemo.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller
public class CategoryController {
@Autowired
private CategoryMapper categoryMapper; @RequestMapping("/listCategory")
public String listCategory(Model model) throws Exception{
List<Category> categories=categoryMapper.findAll();
model.addAttribute("category",categories);
System.out.println();
return "listCategory";
}
}
3.listCategory.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
</head>
<body>
<div class="showing">
<h2>SpringBoot+mybatis</h2> <table>
<thead>
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr th:each="c: ${category}">
<td align="center" th:text="${c.id}"></td>
<td align="center" th:text="${c.name}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
二、xml方式(有问题)
三、SpringBoot+Mybatis抽插数据库
1.使用第三方插件PageHelper进行分页查询
增加依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
2.配置PageHelper
PageHelperConfig.java
package com.example.springbootmybatisdemo.config; import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。
注解@Bean 表示启动PageHelper这个拦截器。
offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。比jpa中Pageable进行分页查询时更合理,不需要再去处理。
3.修改映射mapper
CategoryMapper.java
package com.example.springbootmybatisdemo.mapper; import com.example.springbootmybatisdemo.pojo.Category;
import org.apache.ibatis.annotations.*; import java.util.List; @Mapper
public interface CategoryMapper {
@Select("select * from category")
List<Category> findAll(); @Insert("insert into category(name) values (#{name})")
int save(Category category); @Delete(" delete from category where id= #{id} ")
void delete(int id); @Select("select * from category where id= #{id} ")
Category get(int id); @Update("update category set name=#{name} where id=#{id} ")
int update(Category category);
}
4.控制层
CategoryController.java
package com.example.springbootmybatisdemo.controller; import com.example.springbootmybatisdemo.mapper.CategoryMapper;
import com.example.springbootmybatisdemo.pojo.Category;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller
public class CategoryController {
@Autowired
private CategoryMapper categoryMapper; @RequestMapping("/listCategory")
public String listCategory(Model model) throws Exception{
List<Category> categories=categoryMapper.findAll();
model.addAttribute("category",categories);
System.out.println();
return "listCategory";
} @RequestMapping("/listCategories")
public String listCategories(Model model,
@RequestParam(value = "start",defaultValue = "0")int start,
@RequestParam(value = "size",defaultValue = "5") int size) throws Exception{
PageHelper.startPage(start,size,"id desc");
List<Category> categories=categoryMapper.findAll();
PageInfo<Category> pageInfo=new PageInfo<>(categories);
System.out.println(pageInfo.getPageNum());
model.addAttribute("pageInfo",pageInfo);
return "listCategories";
} @RequestMapping("/addCategory")
public String addCategory(Category category) throws Exception{
categoryMapper.save(category);
return "redirect:listCategories";
}
@RequestMapping("/deleteCategory")
public String deleteCategory(Category category)throws Exception{
categoryMapper.delete(category.getId());
return "redirect:listCategories";
}
@RequestMapping("/updateCategory")
public String updateCategory(Category category)throws Exception{
categoryMapper.update(category);
return "redirect:listCategories";
}
@RequestMapping("/editCategory")
public String editCategory(int id,Model model)throws Exception{
Category category=categoryMapper.get(id);
model.addAttribute("category",category);
return "editCategory";
} }
根据start,size进行分页,并且设置id 倒排序
因为PageHelper的作用,这里就会返回当前分页的集合了
根据返回的集合,创建PageInfo对象
5.视图层
listCategories.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
</head>
<body>
<div class="showing">
<h2>SpringBoot+Mybatis</h2>
<div style="width:500px;margin:20px auto;text-align: center">
<table align="center" border="1" cellspacing="0">
<thead>
<tr>
<th>id</th>
<th>name</th>
<td>编辑</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<tr th:each="c: ${pageInfo.list}">
<td align="center" th:text="${c.id}"></td>
<td align="center" th:text="${c.name}"></td>
<td align="center" ><a th:href="@{/editCategory(id=${c.id})}">编辑</a></td>
<td align="center" ><a th:href="@{/deleteCategory(id=${c.id})}">删除</a></td>
</tr>
</tbody>
</table>
<br />
<div>
<a th:href="@{/listCategories(start=1)}">[首 页]</a>
<a th:href="@{/listCategories(start=${pageInfo.pageNum -1})}">[上一页]</a>
<a th:href="@{/listCategories(start=${pageInfo.pageNum +1})}">[下一页]</a>
<a th:href="@{/listCategories(start=${pageInfo.pages})}">[末 页]</a>
</div>
<form action="/addCategory" method="post">
name:<input name="name"/><br/>
<button type="submit">提交</button>
</form>
</div>
</div>
</body>
</html>
editCategory.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
</head>
<body>
<div class="showing">
<h2>springboot+jpa</h2> <div style="margin:0px auto; width:500px"> <form action="/updateCategory" method="post"> name: <input name="name" th:value="${category.name}" /> <br/> <input name="id" type="hidden" th:value="${category.id}" />
<button type="submit">提交</button> </form>
</div>
</div>
</body>
</html>
5.测试
6.问题:当编辑完以后就返回首页了。
修改:在编辑时传入当前分页数,然后跳转到listCategories的时候传入start。
四、代码
https://github.com/lyj8330328/springboot-mybatis-demo
笔记66 Spring Boot快速入门(六)的更多相关文章
- 笔记61 Spring Boot快速入门(一)
IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...
- 笔记63 Spring Boot快速入门(三)
SpringBoot中使用JSP Springboot的默认视图支持是Thymeleaf,但是Thymeleaf还没开始学,熟悉的还是jsp,所以要让Springboot支持 jsp. 一.在pom. ...
- 笔记65 Spring Boot快速入门(五)
SpringBoot+JPA 一.什么是JPA? JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期 ...
- 笔记70 Spring Boot快速入门(八)(重要)
上传文件 一.方式一 1.上传页面 upLoadPage.html <!DOCTYPE html> <html lang="en"> <head> ...
- 笔记64 Spring Boot快速入门(四)
SpringBoot中错误处理.端口设置和上下文路径以及配置切换 一.错误处理 假设在访问首页的时候会出现一些错误,然后将这些错误当作异常抛出,反馈给用户. 1.修改IndexController.j ...
- 笔记62 Spring Boot快速入门(二)
SpringBoot部署 一.jar方式 1.首先安装maven. <1>下载最新的maven版本:https://maven.apache.org/download.cgi <2& ...
- 笔记67 Spring Boot快速入门(七)
SpringBoot+RESTful+JSON 一.RESTful架构 REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. ...
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
随机推荐
- linuxprobe培训第3节课笔记2019年7月7日
linux常用命令: echo:在终端输出字符串或变量提取后的值 date:显示及设置系统的时间或日期 reboot:重启 poweroff:关机 wget:下载 ps:查看系统中的进程状态(常用参数 ...
- nginx的虚拟主机和反向代理和一些技巧应用
前言 nginx是什么,一般用来做反向代理,也可以用来放静态htnl等文件..当然代理tcp协议也没啥问题,但做好是别用哈.性能不咋样. 虚拟主机 什么是虚拟主机? 个人理解,比如一台主机装一个ngi ...
- linux-ftp install
ftp安装 yum install vsftpd vi /etc/vsftpd/vsftpd.conf (可以先备份,但不要放在此目录下,不然启动vsftpd的时候 也会加载其备份文件,报启动错误,因 ...
- python基础--逻辑运算
#and or not#and 且 :两边都为真才是真#or 或:一个真就是真(一真为真)#ont 非:相反#优先级:1.not>and>or#同一优先级由左向右以此计算!#列子:prin ...
- jquery实现放大镜简单方法
网上有许多放大镜的jquery的插件,但是用着不是那么得心应手,现在一页代码实现一个放大镜功能,如果需要附加的功能可以手动修改,原理都在注释里 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- Python3.5-20190506-廖老师-自我笔记函数
函数就是将你的代码封装起来,可以重复利用.不需要每次就写重复的代码 def 函数名(位置参数,默认参数=10,可变参数,关键字参数): 代码块 return 值 定义函数时,需要确定函数名和参数个数: ...
- JAXB中的@XmlRootElement注解详解
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...
- testNG之顺序执行
@Test testNG1.java: import org.testng.annotations.Test; public class testNG1 { @Test public void t ...
- nucleus学习
task的TCB结构: typedef struct TC_TCB_STRUCT { /* Standard thread information first. This information is ...
- Python基础教程(011)--程序开发中的错误及原因
前言 排查代码开发中的错误 内容 1,编写的程序不能正常执行,或者执行的结果不是我们期望的 2,俗称bug,是程序开发常见的,初学常见的原因有 手误 对已经学习的知识点理解不足 对语音还有需要学习和提 ...