若是是Oracle的話java
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
// 使用JPA註解,配置與表的映射關係 @Entity // 告訴JPA這是一個實體類(對應數據表),不是普通的javabean @Table(name = "tbl_user") // 不寫這個註解,默認爲這個類的小寫做爲名字 public class User { @Id // 標識這是主鍵 @GeneratedValue(strategy = GenerationType.AUTO) // 根據數據庫自動選則主鍵自增類型 private Integer id; @Column(name="lastname",length = 50) // 標識這個列的列名和屬性 private String lastName; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
spring: datasource: url: jdbc:oracle:thin:@127.0.0.1:1521:orcl username: scott password: tiger driver-class-name: oracle.jdbc.OracleDriver jpa: hibernate: # 更新或建立數據表 ddl-auto: update # 控制檯打印sql show-sql: true
// 第一個泛型是哪一個實體的dao層 // 第二個泛型表示主鍵類型 public interface UserRepository extends JpaRepository<User,Integer>{ }
@Controller public class UserController { @Resource private UserRepository userRepository; @GetMapping("/user/{id}") public User findById(Integer id){ User user = userRepository.findOne(id); return user; } @GetMapping("/user") public User insert(User user){ User save = userRepository.save(user); return save; } }