<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=db_username spring.datasource.password=db_password import javax.persistence.*; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; } import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } @PostMapping("/") public User createUser(@RequestBody User user) { return userRepository.save(user); } }


上一篇:
下一篇:
切换中文