1、添加依赖(thymeleaf、web、mybatis、devtools、mysql、druid、mapper、pagehelper)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
2、添加配置(application.yml)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///test?serverTimezone=UTC
username: root
password: 12345678
thymeleaf:
cache: false
suffix: .html
mode: HTML
encoding: UTF-8
3、主类的注解
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.test.dao")
config层
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
4、entity层
@Entity
@Table(name="t_student")
public class Student implements Serializable{
private static final long serialVersionUID = -1137297207727042348L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long sid;
@Column
@NotBlank(message = "姓名不能为空")
private String name;
@Column
private Boolean sex;
@Column
@Max(20)
@Min(15)
private Long age;
@Column
@NotBlank(message = "年级不能为空")
private String grade;
@Column
@NotBlank(message = "专业不能为空")
private String major;
@Column
@NotBlank(message = "地址不能为空")
private String address;
5、pageBean
public class PageBean implements Serializable {
private static final long serialVersionUID = 1L;
private int pageNum;
private int maxPage;
private int nextPage;
private int rowsPerPage = 15;
private int rowsNum;
private int lastPage;
public int getLastPage() {
if(pageNum >1)
return pageNum-1;
return 0;
}
public int getNextPage() {
if(pageNum <maxPage)
return pageNum+1;
return 0;
}
6、dao层
public interface StudentMapper extends Mapper<Student>{}
7、biz层
接口
public interface IStuServ {
List<Student> getAll(PageBean pages);
void del(int id);
Student load(int id);
void modify(Student student);
void add(Student student);
}
实现
@Service
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class StuServImpl implements IStuServ {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAll(PageBean pages) {
Page<Student> pageinfo=null;
List<Student> slist=null;
if(pages != null && pages.getRowsPerPage()>0) {
pageinfo=PageHelper.startPage(pages.getPageNum(), pages.getRowsPerPage());
}
slist=studentMapper.selectAll();
if(pages != null && pageinfo != null) {
pages.setPageNum(pageinfo.getPageNum());
pages.setMaxPage(pageinfo.getPages());
pages.setRowsNum(((Number)pageinfo.getTotal()).intValue());
}
return slist;
}
@Override
public void del(int id) {
studentMapper.deleteByPrimaryKey(id);
}
@Override
public Student load(int id) {
return studentMapper.selectByPrimaryKey(id);
}
@Override
public void modify(Student student) {
studentMapper.updateByPrimaryKeySelective(student);
}
@Override
public void add(Student student) {
studentMapper.insertSelective(student);
}
}
8、action
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private IStuServ studentService;
@ModelAttribute(name="sexOption")
public Map<Boolean,String> sex(){
Map<Boolean,String> map = new HashMap<>();
map.put(true, "男");
map.put(false, "女");
return map;
}
@RequestMapping("list")
private String list(Model model) throws Exception {
return this.list(1,model);
}
@RequestMapping("list/{page}/pages")
private String list(@PathVariable int page,Model model) throws Exception {
PageBean pages= new PageBean();
pages.setRowsPerPage(3);
pages.setPageNum(page);
List<Student> slist=studentService.getAll(pages);
model.addAttribute("pages", pages);
model.addAttribute("studentList", slist);
return "student/list";
}
@RequestMapping("{id}/del")
private String del(@PathVariable int id) {
studentService.del(id);
return "redirect:/student/list";
}
@RequestMapping(value="{id}/update",method=RequestMethod.GET)
public String update(@PathVariable int id,Model model) throws Exception {
Student student=studentService.load(id);
model.addAttribute("student", student);
model.addAttribute("action", "update");
return "student/form";
}
@RequestMapping(value="/update",method=RequestMethod.POST)
public String update(@Validated @ModelAttribute("student") Student student,Errors errors,Model model) throws Exception {
model.addAttribute("action","update");
if(errors.hasErrors())
return "student/form";
studentService.modify(student);
return "redirect:/student/list";
}
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add1(Model model) throws Exception {
Student student=new Student();
model.addAttribute("student", student);
model.addAttribute("action", "add");
return "student/form";
}
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add2(@Validated @ModelAttribute("student") Student student,Errors errors,Model model) throws Exception {
model.addAttribute("action","add");
if(errors.hasErrors())
return "student/form";
studentService.add(student);
System.err.println(student.toString());
return "redirect:/student/list";
}
}
9、视图
index.html
<!DOCTYPE html>
<html xmlns="">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<a href="/student/list">学生列表</a>
</body>
</html>
student->form.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form id="student" th:action="@{/student/{action}(action=${action})}"
method="post" th:object="${student}">
<input type="hidden" name="sid" value="1" th:value="*{sid}">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="name" th:value="*{name}">
<span
class="erros" th:if="${#fields.hasErrors('name')}"
th:errors="${student.name}">名字错误</span></td>
</tr>
<tr>
<td>学生性别</td>
<td><span th:each="so:${sexOption}"> <input type="radio"
name="sex" value="true" checked th:value="${so.key}"
th:checked="${so.key==student.sex}"> <label
th:text="${so.value}">女</label>
</span> <span th:remove="all"><input type="radio" name="sex"
value="false"><label>男</label></span></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" th:value="*{age}"> <span
class="erros" th:if="${#fields.hasErrors('age')}"
th:errors="${student.age}">年龄必须在15-20之间</span>
</tr>
<tr>
<td>年级</td>
<td><input type="text" name="grade" th:value="*{grade}">
<span class="error" th:if="${#fields.hasErrors('grade')}"
th:errors="${student.grade}">年级不能为空</span></td>
</tr>
<tr>
<td>专业</td>
<td><input type="text" name="major" th:value="*{major}">
<span class="erros" th:if="${#fields.hasErrors('major')}"
th:errors="${student.major}">专业不能为空</span></td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address" th:value="*{address}">
<span class="error" th:if="${#fields.hasErrors('address')}"
th:errors="${student.address}">地址不能为空</span></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交数据"> <input
type="reset" value="重置数据"></td>
</tr>
</table>
</form>
</body>
</html>
student->list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<table border="1" align="center">
<thead >
<tr>
<th>学员编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>年级</th>
<th>专业</th>
<th>地址</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<tr th:each="stu:${studentList}">
<td th:text="${stu.sid}">1</td>
<td th:text="${stu.name}">zy1</td>
<td th:text="${stu.sex==true?'男':'女'}">男</td>
<td th:text="${stu.age}">17</td>
<td th:text="${stu.grade}">高一</td>
<td th:text="${stu.major}">物联网</td>
<td th:text="${stu.address}">雁塔区</td>
<td>
<a href="student/update" th:href="@{/student/}+${stu.sid}+'/update'">修改</a>
<a href="student/del" th:href="@{/student/}+${stu.sid}+'/del'">删除</a>
<a href="/student/add" th:href="@{/student/}+'add'">增加</a>
</td>
</tr>
</tbody>
<tfoot th:if="${pages != null and pages.maxPage >1}" >
<tr th:object="${pages}">
<td colspan="8">
<a href="/student/list?page=1" th:href="@{/student/list/1/pages}">第一页</a>
<a href="/student/list?page=1" th:if="*{lastPage} gt 0" th:href="@{/student/list/}+*{lastPage}+'/pages'">上一页</a>
<a href="/student/list?page=1" th:if="*{nextPage} gt 0" th:href="@{/student/list/}+*{nextPage}+'/pages'"> 下一页</a>
<a href="/student/list?page=1" th:if="*{maxPage} gt 0" th:href="@{/student/list/}+*{maxPage}+'/pages'">末尾页</a>
</td>
</tr>
</tfoot>
</table>
</body>
</html>