SpringBoot+Jpa实现增删改查以及分页查询

SpringBoot+Jpa实现增删改查以及分页查询

一,项目所用得框架

1,使用工具IDEA

2,使用springboot框架+JPA+Mysql

二,项目的一个总体目录

img

1,springboot+jpa实现增删改查

​ 里面分为四部分,分别是

​ controller——->控制层

​ domain———->实体层

​ repository——->接口

​ service————>业务逻辑

三,每一层代码是如何实现的

一,domain

​ domain层中有几点很重要

​ 1,@Entity

​ 2,@Table(name=”employee”)

​ 表示根据实体类所生成的数据库中的表名

​ 3,@Id

​ 表示id为主键,

​ 4,@GeneratedValue(strategy=GenerationType.AUTO)

​ 表示是主键生成策略,如果加上了这句话,那么注意此时,id的类型一定是Integer类型的,不然程序肯定爆炸

​ 5,@Colum

​ 可写可不写,因为默认将其当做字符串

package com.sudojava.springjpa.domain;


import javax.persistence.*;

@Entity  
@Table(name = "employee")
public class Employee {
    

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column
    private String name;
    @Column
    private String address;
    @Column
    private String photo;

   ...set和get方法,toString方法,有参和无参构造
}

二,service层

    ###  
包括xxxService:
package com.sudojava.springjpa.service;

import com.sudojava.springjpa.domain.Employee;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface EmployeeService {

     Employee saveEmployee(Employee employee);

     void deleteEmployee(Integer id);

     boolean updateEmployee(Employee employee);

     Page<Employee> findAll(Pageable pageable);

     Page<Employee> findAllByName(String name,Pageable pageable);
}

​ 1,分别写了增删改查,和分页的方法

​ 2,增的方法:返回实体类对象,参数为实体类对象,因为将新添加的保存到对象中

​ 3,删除:根据id进行删除,所以参数为id,返回值为空

​ 4,修改:返回boolean类型,参数为实体类对象

​ 5,查找:传入参数Pageable

​ 6,根据名字进行分页查询,传入名字的参数和Pageable (String name,Pageable pageable)

包括xxxServiceimpl
package com.sudojava.springjpa.service;

import com.sudojava.springjpa.domain.Employee;
import com.sudojava.springjpa.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ResourceBundle;

@Service
public class EmployeeServiceImp implements EmployeeService {
   

    //注入dao࿰
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较常见的全栈开发需求,以下是一个简单的示例代码: 后端部分(使用Spring Boot框架): 1. 建立实体类 ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // getter和setter方法省略 } ``` 2. 建立DAO层接口 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 3. 建立Service层接口和实现 接口: ```java public interface UserService { List<User> findAll(); User findById(Long id); User save(User user); void deleteById(Long id); } ``` 实现: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public List<User> findAll() { return userRepository.findAll(); } @Override public User findById(Long id) { return userRepository.findById(id).orElse(null); } @Override public User save(User user) { return userRepository.save(user); } @Override public void deleteById(Long id) { userRepository.deleteById(id); } } ``` 4. 建立Controller层 ```java @RestController @RequestMapping("/api/user") public class UserController { @Autowired private UserService userService; @GetMapping("") public List<User> findAll() { return userService.findAll(); } @GetMapping("/{id}") public User findById(@PathVariable Long id) { return userService.findById(id); } @PostMapping("") public User save(@RequestBody User user) { return userService.save(user); } @DeleteMapping("/{id}") public void deleteById(@PathVariable Long id) { userService.deleteById(id); } } ``` 前端部分(使用Vue框架): 1. 建立Vue组件 ```vue <template> <div> <div> <button @click="showAddModal">新增</button> </div> <table> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="user in userList" :key="user.id"> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.age }}</td> <td> <button @click="showEditModal(user)">编辑</button> <button @click="deleteUser(user.id)">删除</button> </td> </tr> </tbody> </table> <div v-if="showAdd"> <div> <label>姓名:</label> <input v-model="newUser.name"> </div> <div> <label>年龄:</label> <input v-model="newUser.age"> </div> <div> <button @click="addUser">确定</button> <button @click="hideAddModal">取消</button> </div> </div> <div v-if="showEdit"> <div> <label>姓名:</label> <input v-model="selectedUser.name"> </div> <div> <label>年龄:</label> <input v-model="selectedUser.age"> </div> <div> <button @click="editUser">确定</button> <button @click="hideEditModal">取消</button> </div> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { userList: [], showAdd: false, showEdit: false, newUser: { name: '', age: '', }, selectedUser: null, }; }, methods: { showAddModal() { this.showAdd = true; }, hideAddModal() { this.showAdd = false; this.newUser.name = ''; this.newUser.age = ''; }, addUser() { axios.post('/api/user', this.newUser) .then(() => { this.userList.push(this.newUser); this.hideAddModal(); }) .catch((err) => { console.error(err); }); }, showEditModal(user) { this.showEdit = true; this.selectedUser = user; }, hideEditModal() { this.showEdit = false; this.selectedUser = null; }, editUser() { axios.put(`/api/user/${this.selectedUser.id}`, this.selectedUser) .then(() => { this.hideEditModal(); }) .catch((err) => { console.error(err); }); }, deleteUser(id) { axios.delete(`/api/user/${id}`) .then(() => { this.userList = this.userList.filter(user => user.id !== id); }) .catch((err) => { console.error(err); }); }, loadUserList() { axios.get('/api/user') .then((res) => { this.userList = res.data; }) .catch((err) => { console.error(err); }); }, }, mounted() { this.loadUserList(); }, }; </script> ``` 2. 在页面中使用Vue组件 ```html <!DOCTYPE html> <html> <head> <title>用户管理</title> </head> <body> <div id="app"> <user-list></user-list> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="/js/user-list.js"></script> </body> </html> ``` 以上代码只是一个简单的示例,如果需要更进一步的开发,还需要考虑数据验证、分页、搜索等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值