SpringBoot 简单CRUD操作Spring-Data-JPA(四)

本文介绍如何使用SpringData JPA框架简化持久层开发工作,包括引入依赖、配置文件、创建实体类、DAO接口及控制器,并演示了增删改查功能的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Data JPA 框架,主要针对的是 Spring 唯一没有简化到的业务逻辑代码,至此,开发者连仅剩的实现持久层业务逻辑的工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!

JPA(Java Persistence API)定义了一系列对象持久化的标准  目前实现这一规范的产品有Hibernate、TopLink等。

具体操作如下:

 1. 引入jpa和mysql驱动支持; 进入pom.xml,alt+/   进入编辑视图




2.配置application.yml文件

 


3.在数据库中创建test数据库;再创建实体类,

/**
 * 实体类
 * 
 * @author Administrator
 *
 */
@Entity
@Table(name = "t_student") // 指定表名
public class Student {

	@Id // 主键
	@GeneratedValue //自动增长
	private int id;
	@Column(length = 20)
	private String name;
	@Column(length = 6)
	private String pass;
    省略setXXX and getXXX

4.启动Hello3Application类 (刷新就在数据库中创建这个t_student表)

5.写底层数据继承JpaRepository

package com.hlx.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.hlx.entity.Student;
/**
 *  数据底层  只要继承这个类就OK!
 * @author Administrator
 * 
 * JpaRepository<Student实体类, Integer主键数据类型>
 *
 */
public interface StudentDao extends JpaRepository<Student, Integer> {

}

6.写Controller控制器

package com.hlx.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hlx.dao.StudentDao;
import com.hlx.entity.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Resource // 注入dao
	private StudentDao studentDao;

	/**
	 * 查询所有的数据
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		// 试图对象
		ModelAndView view = new ModelAndView("all");
		// 保存数据,调用查询所有的方法
		view.addObject("students", studentDao.findAll());
		return view;
	}

	/**
	 * 添加数据
	 * 
	 * @param stu
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Student stu) {
		studentDao.save(stu); // 调用添加方法
		return "forward:/student/list"; // 转发
	}

	/**
	 * 根据ID查找实体对象
	 * @return
	 */
	@RequestMapping("/pre/{id}")
	public ModelAndView preUpdate(@PathVariable("id") Integer id){
		//视图对象==>到更新页面哦!
		ModelAndView view= new ModelAndView("update");
		//调用查询方法
		view.addObject("student", studentDao.getOne(id));
		return view;
	}
	
	
	/**
	 * 根据ID更新数据
	 * @return
	 */
	@PostMapping("/update")
	public  String update(Student stu) {
		studentDao.save(stu); // 调用添加方法,如果没有就添加,有就更新
		return "forward:/student/list"; // 转发
	}
	
	
	/**
	 * 根据ID删除数据
	 * @return
	 */
	@GetMapping("/del")
	public  String delete(Integer id) {
		studentDao.deleteById(id); //调用删除方法
		return "forward:/student/list"; // 转发
	}
	
	

}

7.写页面

 (a)add.html 



 (b)all.ftl

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>显示学生列表信息</title>
</head>
<body>
<a href="/add.html">add</a>
  <table border="1" width="75%">
    <tr>
      <td>编号</td>
      <td>姓名</td>
      <td>密碼</td>
      <td>操作</td>    
    </tr>
   <#list students as student>
   <tr>
    <td>${student.id}</td>
    <td>${student.name}</td>
    <td>${student.pass}</td>
    <td>
        <a href="/student/pre/${student.id}">update</a>   
        <a href="/student/del?id=${student.id}">delete</a>
    </td>
   </tr>
   </#list>
  
  </table>
</body>
</html>

(c)update.ftl

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="/student/update" method="post">
       <input type="hidden" name="id" value="${student.id}"/>
       username:<input name="name" value="${student.name}"/><br/>
       password:<input  type="password" name="pass" value="${student.pass}"/><br/>
	   <input type="submit" id="but" value="update">
	</form>
</body>
</html>

工程结构如下:


运行效果

列表页面


添加页面


列表页面


修改页面

修改之后的数据

删除页面





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值