第一个SpringData JPA 应用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
@Entity
@Table(name = "tbl_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
}
- 编写 Dao 层接口继承 jpaRespository接口,泛型中第一个参数为要处理的实体类名,第二个参数为实体类中属性 id 的类型
public interface UserDao extends JpaRepository<User, Integer> {
}
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping(value = "/user/{id}")
public User getUserById(@PathVariable("id") Integer id){
return userDao.findById(id).get();
}
}