一. 创建一个Spring Starter Project,加入以下支持: 打开application.properties进行配置:这里我改为了yml格式:
server: port: 8888 servlet: context-path: / spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useSSL=false username: root password: root jpa: hibernate: ddl-auto: update show-sql: true二. 创建一个student类:
package com.example.student; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="student") public class Student { @Id @GeneratedValue private Integer id; @Column(length=100) private String name; @Column(length=50) private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }因为此处id设置为自增,所以mysql中的id要进行自增设置:
写一个StudentDao接口实现jpa:
package com.example.student; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import com.example.test.Book; public interface StudentDao extends JpaRepository<Student,Integer>,JpaSpecificationExecutor<Student>{ }最后写一个控制类StudentController:
package com.example.student; import java.util.List; import javax.annotation.Resource; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.test.Book; @Controller @RequestMapping("/student") public class StudentController { @Resource private StudentDao studentDao; //显示当前信息 @RequestMapping("/list") public ModelAndView list(){ ModelAndView model=new ModelAndView(); model.addObject("studentlist",studentDao.findAll()); model.setViewName("listshow"); return model; } /** * 增加操作 */ @RequestMapping(value="/add",method=RequestMethod.POST) public String addStudent(Student student) { studentDao.save(student); return "forward:/student/list"; } /** * 删除操作 */ @RequestMapping(value="/delete",method=RequestMethod.GET) public String delete(Student student) { studentDao.delete(student); return "forward:/student/list"; } /** * 修改操作 */ @GetMapping(value="/update/{id}") public ModelAndView updateStudent(@PathVariable("id") Integer id){ ModelAndView mav=new ModelAndView(); mav.addObject("student", studentDao.getOne(id)); mav.setViewName("updatestudent"); return mav; } @RequestMapping(value="/preupdate") public String update(Student student) { studentDao.save(student); return "forward:/student/list"; } /** * 查询操作 */ @RequestMapping(value="/find") public ModelAndView findStudent(Student student) { ModelAndView mav=new ModelAndView(); List<Student> studentList=studentDao.findAll(new Specification<Student>(){ //固定的动态查询操作 @Override public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { // TODO Auto-generated method stub Predicate predicate=criteriaBuilder.conjunction(); if(student!=null) { if(student.getName()!=null&&!"".equals(student.getName())) { predicate.getExpressions().add(criteriaBuilder.like(root.get("name"), "%"+student.getName()+"%")); } if(student.getAge()!=null&&!"".equals(student.getAge())) { predicate.getExpressions().add(criteriaBuilder.like(root.get("age"), "%"+student.getAge()+"%")); } } return predicate; } }); mav.addObject("studentlist",studentList); mav.setViewName("listshow"); return mav; } }由于使用Freemarker,我们需在该路径下: 创建ftl文件显示网页:(两个book是无用的,我懒得删了) 其中listshow是主要显示文件,内容如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="/addstudent.html">添加学生</a> <a href="/findstudent.html">查询学生</a> <form action="/student/list" method="post"> <table> <tr> <th>学生id</th> <th>学生姓名</th> <th>学生年龄</th> <th>操作</th> </tr> <#list studentlist as student> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> <td> <a href="/student/update/${student.id}">修改</a> <a href="/student/delete?id=${student.id}">删除</a> </td> </tr> </#list> </table> </form> </body> </html>可以从内容看到额外引入两个html文件放在以下路径:(book无关) 内容分别为:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/student/add" method="post"> 学生姓名:<input type="text" name="name"/> 学生年龄:<input type="text" name="age"/> <input type="submit" value="添加"/> </form> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/student/find" method="post"> 学生姓名:<input type="text" name="name"/> 学生年龄:<input type="text" name="age"/> <input type="submit" value="查询"/> </form> </body> </html>最后别忘记updatestudent.ftl文件:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/student/preupdate" method="post"> <input type="hidden" name="id" value="${student.id}"/><br/> 学生姓名:<input type="text" name="name" value="${student.name}"/><br/> 学生年龄:<input type="text" name="age" value="${student.age}"/><br/> <input type="submit" value="更改"/> </form> </body> </html>以上代码结构完成
三. 运行Spring Boot App。 打开网页:http://localhost:8888/student/list 可以看到: 然后具体测试一下即可。
