做自己没做过的事情叫做成长
做自己不愿做的事情叫做改变
做自己不敢做的事情叫做突破
共勉
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
jpa:
show-sql: true #显示sql语句
hibernate:
ddl-auto: update
naming:
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database: mysql #配置现在所使用的数据库
#配置spring-data-jpa
@Data//生产set get
@Entity//注入到spring容器中
@Table(name = "students")//对应数据库表名
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增长
private Integer id;
private String name;
private String sex;
}
@Repository
public interface StudentDao extends JpaRepository<Student,Integer> {
/**
1.Repository是一个空接口,即是一个标记接口。
2.若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean,纳入到IOC
容器中,进而可以在该接口中定义满足一定规范的方法。IOC容器中实际存放了继承了Repository的接口的实
现类,而这个实现类由spring帮助完成 。在applicationContext.xml中我们配置了springdata:这里的
base-package指定了Repository Bean所在的位置,在这个包下的所有的继承了Repository的接口都会被
IOC容器识别并纳入到容器中,如果没有继承Repository则IOC容器无法识别。
3.我们也可以通过注解的方式替代继承Repository接口@RepositoryDefinition(domainClass=需要处
理的实体类的类型,IdClass=逐渐的类型)。
4.除了使用注解和继承Repository接口我们还可以继承Repository的子接口与实现类。
*/
}
接口:
public interface StudentService {
/**
* 新增
* @param student 新增对象
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
Student save(Student student);
/**
* 修改
* @param student 修改对象
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
Student update(Student student);
/**
* 删除
* @param id 根据id删除
*/
void delete(Integer id);
/**
* 分页
* @param page 页码
* @param size 每页数量
* @param student 用于存储模糊查询参数
* @return
*/
Page<Student> findByPage(Integer page, Integer size,Student student);
/**
* 通过id 查询
* @param id 根据id查询,用于修改
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
Student findById(Integer id);
}
实现接口:
@Service
public class StudentServiceImpl implements StudentService {
/**
* 自动匹配
*/
@Autowired
private StudentDao studentDao;
/**
* 新增
* @param student 新增对象
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
@Override
public Student save(Student student) {
return studentDao.save(student);
}
/**
* 修改
* @param student 修改对象
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
@Override
public Student update(Student student) {
return studentDao.save(student);
}
/**
* 删除
* @param id 根据id删除
*/
@Override
public void delete(Integer id) {
studentDao.deleteById(id);
}
/**
* 分页
* @param page 页码
* @param size 每页数量
* @param student 用于存储模糊查询参数
* @return
*/
@Override
public Page<Student> findByPage(Integer page, Integer size,Student student) {
//模糊查询,需要注意的是 name要和你所需要查询的字段名相同
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name",ExampleMatcher.GenericPropertyMatchers.startsWith());
// Example.of(student,matcher);因为第一个参数只能是对象,所以我们需要用对象存储查询条件
Example<Student> studentExample = Example.of(student,matcher);
//分页初始化
PageRequest request = PageRequest.of(page, size);
//分页查询
Page<Student> studentPage =studentDao.findAll(studentExample,request);
return studentPage;
}
/**
* 通过id 查询
* @param id 根据id查询,用于修改
* @return springData提供的方法是返回的对象,所以我们也需要返回对象
*/
@Override
public Student findById(Integer id) {
return studentDao.findById(id).get();
}
}
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 页面跳转
* @return
*/
@RequestMapping("/add")
public String add(){
return "add";
}
/**
* 新增
*
* @param student
* @return
*/
@RequestMapping("/save")
public String save(Student student,Model model) {
Student save = studentService.save(student);
return "redirect:findByPage";
}
/**
* 修改
*
* @param student
* @return
*/
@RequestMapping("/update")
public String update(Student student) {
Student save = studentService.save(student);
return "";
}
/**
* 删除
*
* @param id
*/
@RequestMapping("/delete")
public String delete(@RequestParam(value = "id",required = false) Integer id,
Model model) {
studentService.delete(id);
return "redirect:findByPage";
}
/**
* 分页
* 在服务层我已经表明的很清楚啦,就不再一一解释了
* @param page
* @param size
* @return
*/
@RequestMapping("/findByPage")
public String findByPage(@RequestParam(value = "page",required = false) Integer page,
@RequestParam(value = "size",required = false) Integer size,
Model model,Student student) {
if (page == null) {
page = 0; //初始化页码,注意他是从0开始
}
if (size == null) {
size = 2; //初始化每页条数
}
Page<Student> byPage = studentService.findByPage(page, size,student);
model.addAttribute("student", byPage);
model.addAttribute("name",student.getName());//用于包存查询条件,不然上一页下一页会数据丢失
return "main";
}
/**
* 通过id 查询
*
* @param id
* @return
*/
@RequestMapping("/findById")
public String findById(@RequestParam(value = "id",required = false) Integer id,
Model model) {
Student byId = studentService.findById(id);
model.addAttribute("student",byId);
return "add";
}
}
好人做到底,就将html也给贴出来吧,各位看官可不要嫌弃丑,毕竟是小demo没有进行美化
我是使用thymeleaf模板哦。
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/findByPage" method="post">
姓名:<input type="text" name="name"/><br />
<input type="submit" value="查询"/>
form>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>编号th>
<th>姓名th>
<th>性别th>
<th colspan="2">操作th>
tr>
<tr th:each="s:${student}">
<td th:text="${s.id}">td>
<td th:text="${s.name}">td>
<td th:text="${s.sex}">td>
<td><a th:href="@{/findById(id=${s.id})}">修改a>td>
<td><a th:href="@{/delete(id=${s.id})}">删除a>td>
tr>
table>
第<span th:text="${student.getNumber()+1}">span>页/共<span th:text="${student.getTotalPages()}">span>页<br/>
<a th:href="@{/findByPage(page=0,name=${name})}">首页a>
<span th:if="${student.getNumber()} > 0">
<a th:href="@{/findByPage(page=${student.getNumber()- 1},name=${name})}">上一页a>
span>
<span th:if="${student.getNumber()} < ${student.getTotalPages()-1}">
<a th:href="@{/findByPage(page=${student.getNumber()+ 1},name=${name})}">下一页a>
span>
<a th:href="@{/findByPage(page=${student.getTotalPages()-1},name=${name})}">尾页a>
<a href="/add">新增a>
body>
html>
我将修改与新增写在同一个页面了,因为save()这个方法他会根据id是否为空去判断,所以我就使用同一个了,大家也可以写两个页面调用不同的方法,这样更规范
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/save" method="post">
<span th:if="${student != null}">
<input type="hidden" name="id" th:value="${student.id}" />
姓名:<input type="text" name="name" th:value="${student.name}"/>
性别:<input type="text" name="sex" th:value="${student.sex}"/>
span>
<span th:if="${student == null}">
姓名:<input type="text" name="name"/>
性别:<input type="text" name="sex"/>
span>
<input type="submit" value="保存"/>
form>
body>
html>
一顿CV过去,项目就能运行了。
觉得可以的,给予一点鼓励,给个赞,谢谢你。