springboot的增删改查

1. 以下是使用Spring Data JPA进行增删改查的过程

实体类:

@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;

    // Getters and setters omitted for brevity
}

Repository接口:

@Repository
public interface UserRepository extends JpaRepository {
}

Service实现:

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    @Override
    public List getAllUsers() {
        return userRepository.findAll();
    }

    @Override
    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public User createOrUpdateUser(User user) {
        return userRepository.save(user);
    }

    @Override
    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }
}


 

Controller:

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("")
    public List getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public ResponseEntity getUserById(@PathVariable(value = "id") Long id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().body(user);
    }

    @PostMapping("")
    public User createOrUpdateUser(@RequestBody User user) {
        return userService.createOrUpdateUser(user);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteUser(@PathVariable(value = "id") Long id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }
}


 

3. Spring Boot项目的依赖:```xml


    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    
    
        com.h2database
        h2
        runtime
    

    
    
        org.projectlombok
        lombok
        true
    

    
    
        com.fasterxml.jackson.core
        jackson-databind
    


 

实体类:```java

@Entity
@Table(name = "user")
@Data // Lombok注解,自动生成Getter、Setter等方法
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;
}


 

Mapper:

@Mapper
@Repository
public interface UserMapper {
    List findAll();
    User findById(Long id);
    void save(User user);
    void update(User user);
    void deleteById(Long id);
}


 

Service:

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public List getAllUsers() {
        return userMapper.findAll();
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.findById(id);
    }

    @Override
    public void createOrUpdateUser(User user) {
        if (user.getId() == null) {
            userMapper.save(user);
        } else {
            userMapper.update(user);
        }
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteById(id);
    }
}

Controller:

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("")
    public List getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public ResponseEntity getUserById(@PathVariable(value = "id") Long id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().body(user);
    }

    @PostMapping("")
    public ResponseEntity createOrUpdateUser(@RequestBody User user) {
        userService.createOrUpdateUser(user);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteUser(@PathVariable(value = "id") Long id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }
}


 

XML文件(使用MyBatis):```xml


    
        
        
        
    

    

    

    
        INSERT INTO user(name, age) VALUES(#{name}, #{age});
    

    
        UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id};
    

    
        DELETE FROM user WHERE id=#{id};
    

以上是Spring Boot项目中的增删改查操作。其中,实体类定义了数据表中的字段;Mapper定义了数据操作的方法;Service定义了对Mapper方法的调用;Controller定义了API接口及其参数;而XML文件则定义了MyBatis框架中的SQL语句。同时,@Repository、@Service、@Controller等注解可以让Spring框架自动地实例化Bean,并将其加入到应用程序的上下文中。

你可能感兴趣的:(spring,boot,java,spring)