建议您看本文章前,先看:[Springboot搭建简单Demp](https://blog.csdn.net/qq_44969643/article/details/106408784)
目录结构(红色代表需要的文件):
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200607153744411.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)
pom.xml
```yaml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
```
application.yml
```yaml
server:
port: 9090
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username: shuang
password: mysql215311?
driver-class-name: com.mysql.cj.jdbc.Driver
```
User
```java
package com.shuang.entity;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Data
public class User {
@NotNull(message="id不能为空")
private Long id;
@NotEmpty(message="姓名不能为空")
@Length(min=2,message="姓名长度不能小于2位")
private String name;
@Min(value=60,message = "成绩必须大于60")
private double score;
}
```
UserRepository
```java
package com.shuang.repository;
import com.shuang.entity.User;
import java.util.List;
public interface UserRepository {
public List<User> findAll();
public User findById(Long id);
public void save(User user);
public void update(User user);
public void deleteById(Long id);
}
```
UserRepositoryImpl
```java
package com.shuang.repository.impl;
import com.shuang.entity.User;
import com.shuang.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> findAll() {
return jdbcTemplate.query("select * from User",new BeanPropertyRowMapper<>(User.class));
}
@Override
public User findById(Long id)
{
return jdbcTemplate.queryForObject("select * from user where id= ?",new Object[]{id},new BeanPropertyRowMapper<>(User.class));
}
@Override
public void save(User user) {
jdbcTemplate.update("insert into user(name,score) values(?,?)",user.getName(),user.getScore());
}
@Override
public void update(User user) {
jdbcTemplate.update("update user set name=?,score=? where id=?",user.getName(),user.getScore(),user.getName());
}
@Override
public void deleteById(Long id) {
jdbcTemplate.update("delete from user where id=?",id);
}
}
```
Handler
```java
package com.shuang.controller;
import com.shuang.entity.User;
import com.shuang.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserHandler {
@Autowired
private UserRepository userRepository;
@GetMapping("/findAll")
public List<User> findAll(){
return userRepository.findAll();
}
@GetMapping("/findById{id}")
public User findById(@PathVariable("id") long id){
return userRepository.findById(id);
}
@PostMapping("/save")
public void save (@RequestBody User user){
userRepository.save(user);
}
@PutMapping("/update")
public void update(@RequestBody User user){
userRepository.update(user);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
userRepository.deleteById(id);
}
}
```