1.环境准备
JDK1.8 、IDEA2018 、 Mysql8.0.15
2.新建一个数据库
创建数据库
create database testmybatis;
创建表
use testmybatis;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30),
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
添加数据待测
insert into category_ values(null,'category 1');
insert into category_ values(null,'category 2');
insert into category_ values(null,'category 3');
insert into category_ values(null,'category 4');
3.新建一个项目
勾选上Web模板
4.修改pom.xml文件
添加下面的依赖
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version><!--我装的是8.0.15版本-->
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<!--添加PageHelper的支持,用于分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
5.修改配置文件
修改application.properties文件
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testmybatis?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=666666
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
由于我用得是Mysql8.0 所以驱动类是 com.mysql.cj.jdbc.Driver如果是其他版本请改成com.mysql.jdbc.Driver
6.新建一个PageHelperConfig 类用于配置
具体内容如下:
package com.eknaij.springbootmybatis.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"); //offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
p.setProperty("rowBoundsWithCount", "true"); //rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
p.setProperty("reasonable", "true");//reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
pageHelper.setProperties(p);
return pageHelper;
}
}
注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。
注解@Bean 表示启动PageHelper这个拦截器。
7.新建实体类
具体内容如下:
package com.eknaij.springbootmybatis.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
8.新建Mybatis的Mapper接口
具体内容:
package com.eknaij.springbootmybatis.mapper;
import java.util.List;
import com.eknaij.springbootmybatis.pojo.Category;
import org.apache.ibatis.annotations.*;
@Mapper
public interface CategoryMapper {
@Select("select * from category_ ")
List<Category> findAll();
@Insert(" insert into category_ ( name ) values (#{name}) ")
public int save(Category category);
@Delete(" delete from category_ where id= #{id} ")
public void delete(int id);
@Select("select * from category_ where id= #{id} ")
public Category get(int id);
@Update("update category_ set name=#{name} where id=#{id} ")
public int update(Category category);
}
使用注解@Mapper 表示这是一个Mybatis Mapper接口。
使用@Select注解表示调用findAll方法会去执行对应的sql语句。
9.新建Controller类
内容:
package com.eknaij.springbootmybatis.controller;
import com.eknaij.springbootmybatis.mapper.CategoryMapper;
import com.eknaij.springbootmybatis.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
CategoryMapper categoryMapper;
@RequestMapping("/listCategory")
//在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5。
public String listCategory(Model model, @RequestParam(value = "start", defaultValue = "0") int start,
@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
//根据start,size进行分页
PageHelper.startPage(start,size);
List<Category> cs=categoryMapper.findAll();
PageInfo<Category> page = new PageInfo<>(cs);
model.addAttribute("page", page);
return "listCategory";
}
@RequestMapping("/addCategory")
public String listCategory(Category c) throws Exception {
categoryMapper.save(c);
return "redirect:listCategory";
}
@RequestMapping("/deleteCategory")
public String deleteCategory(Category c) throws Exception {
categoryMapper.delete(c.getId());
return "redirect:listCategory";
}
@RequestMapping("/updateCategory")
public String updateCategory(Category c) throws Exception {
categoryMapper.update(c);
return "redirect:listCategory";
}
@RequestMapping("/editCategory")
public String listCategory(int id,Model m) throws Exception {
Category c= categoryMapper.get(id);
m.addAttribute("c", c);
return "editCategory";
}
}
10.JSP页面
新建listCategory.jsp页面用于显示所有信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div align="center">
</div>
<div style="width:500px;margin:20px auto;text-align: center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="editCategory?id=${c.id}">编辑</a></td>
<td><a href="deleteCategory?id=${c.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<br>
<div>
<a href="?start=1">[首 页]</a>
<a href="?start=${page.pageNum-1}">[上一页]</a>
<a href="?start=${page.pageNum+1}">[下一页]</a>
<a href="?start=${page.pages}">[末 页]</a>
</div>
<br>
<form action="addCategory" method="post">
name: <input name="name"> <br>
<button type="submit">提交</button>
</form>
</div>
新建editCategory.jsp用于修改信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; width:500px">
<form action="updateCategory" method="post">
name: <input name="name" value="${c.name}"> <br>
<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
</form>
</div>
11.启动测试
浏览器输入http://localhost:8080/listCategory
就可以看到效果了
本文源码:点我进入