使用springboot进行增删改查操作

本文介绍了如何在SpringBoot项目中利用JPA进行数据库的增删改查操作。通过展示从数据库配置、实体类定义、 Dao接口、Service层到Controller层的完整代码流程,阐述了SpringBoot与JPA的简洁性和易用性。尽管在编码过程中遇到了一些小错误,但最终成功运行并展示了list和edit页面的效果。
摘要由CSDN通过智能技术生成

今天继续和crud杠

刚学ssm时候觉得ssm好用,现在学springboot又觉得springboot强无敌,只需要一个配置文件,而且配置文件里也不需要写那么多东西,对新手是真的友好。

下面就直接放代码了:
数据库:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  `sex` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

包结构:
在这里插入图片描述
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cm</groupId>
  <artifactId>springboot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <type>pom</type>
</dependency>
    
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.1.2.RELEASE</version>
</dependency>
        <!-- servlet依赖. -->
      <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet<
下面是一个使用Spring Boot进行增删改查操作的示例代码: 1. 创建一个实体类 ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // 省略getter/setter方法 } ``` 2. 创建一个repository ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 3. 创建一个service ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> findAll() { return userRepository.findAll(); } public User findById(Long id) { return userRepository.findById(id) .orElseThrow(() -> new NotFoundException("User not found with id " + id)); } public User save(User user) { return userRepository.save(user); } public void deleteById(Long id) { userRepository.deleteById(id); } public User update(Long id, User updatedUser) { User user = userRepository.findById(id) .orElseThrow(() -> new NotFoundException("User not found with id " + id)); user.setName(updatedUser.getName()); user.setAge(updatedUser.getAge()); return userRepository.save(user); } } ``` 4. 创建一个controller ```java @RestController @RequestMapping("/users") 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 create(@RequestBody User user) { return userService.save(user); } @PutMapping("/{id}") public User update(@PathVariable Long id, @RequestBody User user) { return userService.update(id, user); } @DeleteMapping("/{id}") public void deleteById(@PathVariable Long id) { userService.deleteById(id); } } ``` 这样就完成了一个简单的增删改查操作的代码示例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值