1. 依赖导入
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> </parent> <groupId>com.liu</groupId> <artifactId>springboot_day4</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2. User实体类
@Data @Entity(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; @Column private String phone; @Column private String pwd; }
3. 创建 UserDAO 接口
package com.liu.dao; import com.liu.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository //传入两个泛型,第一个是操作映射的实体类型,第二个是主键字段的类型 public interface UserDAO extends JpaRepository<User,Long> { }
4. 创建controller,注入 UserDAO
package com.liu.controller; import com.liu.dao.UserDAO; import com.liu.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("user") public class UserController { @Autowired private UserDAO userDAO; @GetMapping("/findAll") public List<User> findAll() { return userDAO.findAll(); } @GetMapping("/findById/{id}") public User findById(@PathVariable("id") Long id) { return userDAO.findById(id).get(); } @PostMapping("/save") public User save(@RequestBody User user) { return userDAO.save(user); } @PutMapping("/put") public User put(@RequestBody User user) { return userDAO.save(user); } @GetMapping("/deleteById/{id}") public void deleteById(@PathVariable("id") Long id) { userDAO.deleteById(id); } }
5. application.yml
spring: datasource: url: jdbc:mysql://localhost:3306/db08?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC name: root password: 8485 driver-class-name: com.mysql.cj.jdbc.Driver jpa: show-sql: true properties: hibernate: format_sql: true
6. Spring Boot 启动类
package com.liu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootDay4Application { public static void main(String[] args) { SpringApplication.run(SpringbootDay4Application.class, args); } }