本文講述的內(nèi)容是是在springboot 2.1.3.RELEASE和springdata 2.1.5.RELEASE的基礎(chǔ)上進(jìn)行的,開發(fā)工具為Spring Tool Suite,數(shù)據(jù)庫使用的是mysql。
1、創(chuàng)建一個springboot工程,添加jpa等依賴
2、配置數(shù)據(jù)源及JPA配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jpa?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
format_sql: true
說明:
spring.jpa.hibernate.ddl-auto可選參數(shù) :
create 啟動時刪數(shù)據(jù)庫中的表,然后創(chuàng)建,退出時不刪除數(shù)據(jù)表 ;
create-drop 啟動時刪數(shù)據(jù)庫中的表,然后創(chuàng)建,退出時刪除數(shù)據(jù)表 如果表不存在報錯 ;
update 如果啟動時表格式不一致則更新表,原有數(shù)據(jù)保留 ;
validate 項目啟動表結(jié)構(gòu)進(jìn)行校驗 如果不一致則報錯;
建議使用update
3、創(chuàng)建實體類
@Entity
@Table(name="t_school")
public class School implements Serializable{
private static final long serialVersionUID = 3624350753446288205L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer schoolId;
private String schoolNo;
private String schoolName;
@Temporal(TemporalType.TIMESTAMP)
private Date createDate;
public Integer getSchoolId() {
return schoolId;
}
public void setSchoolId(Integer schoolId) {
this.schoolId = schoolId;
}
public String getSchoolNo() {
return schoolNo;
}
public void setSchoolNo(String schoolNo) {
this.schoolNo = schoolNo;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "School [schoolId=" + schoolId + ", schoolNo=" + schoolNo + ", schoolName=" + schoolName + ", createDate=" + createDate + "]";
}
}
@Entity表明為實體,@Table(name="t_class")指定數(shù)據(jù)庫表名,@Id用于指定主鍵,
@GeneratedValue指定主鍵生成策略,@Temporal用于指定日期類型
此處使用自動創(chuàng)建表
@RunWith(SpringRunner.class)
@SpringBootTest
public class Jpa3ApplicationTests {
@Test
public void contextLoads() {
}
}
執(zhí)行SpringBoot Junit測試會自動創(chuàng)建表。
4、創(chuàng)建DAO
public interface SchoolRepository extends JpaRepository<School, Integer>{
}
JpaRepository為spring data封裝的類,其中包含了很多增刪改查的方法,實際開發(fā)過程中繼承這個類即可。
5、測試
新建:
@Autowired
private SchoolRepository schoolRepository;
@Test
public void insert() {
School school = new School();
school.setSchoolNo("S001");
school.setSchoolName("第一中學(xué)");
school.setCreateDate(new Date());
schoolRepository.save(school);
}
執(zhí)行結(jié)果:
查詢(jpa的查詢方式比較多,此處只做簡單演示)
@Test
public void select() {
School school = schoolRepository.findById(1).get();
System.out.println(school);
}
執(zhí)行結(jié)果:
更新
@Test
public void testUpdate() {
School school = schoolRepository.findById(1).get();
school.setSchoolName("第一中學(xué)=====");
schoolRepository.save(school);
System.out.println(school);
}
執(zhí)行結(jié)果:
刪除
@Test
public void testRemove() {
schoolRepository.deleteById(1);
}
執(zhí)行結(jié)果:
如果要輸出sql參數(shù)的話,增加如下日志配置:
logging:
level:
org.hibernate.type.descriptor.sql.BasicBinder: TRACE