今天就跟大家聊聊有關SpringBoot中怎么集成JPA,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。



server: port: 8090 spring: #通用的數據源配置 datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root jpa: #將默認的存儲引擎切換為 InnoDB database-platform: org.hibernate.dialect.MySQL5InnoDBDialect #配置在日志中打印出執行的 SQL 語句信息。 show-sql: true hibernate: #配置指明在程序啟動的時候要刪除并且創建實體類對應的表 # validate 加載 Hibernate 時,驗證創建數據庫表結構 # create 每次加載 Hibernate ,重新創建數據庫表結構,這就是導致數據庫表數據丟失的原因。 # create-drop 加載 Hibernate 時創建,退出是刪除表結構(退出是指退出sessionFactory) # update 加載 Hibernate 自動更新數據庫結構 # none 不啟用 ddl-auto: none
package my.springboot.jpa.entity; import javax.persistence.*; import java.util.Date; /** * 用戶表實體 * **/ @Entity @Table(name = "userinfo") public class UserInfoDAO { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column private String userName; @Column private Integer age; @Column(length = 500) private String address; @Column(name = "create_date") private Date createDate; @Column(name = "create_user") private String createUser; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } }package my.springboot.jpa.dao; import my.springboot.jpa.entity.UserInfoDAO; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * 倉庫接口類 UserIfoRepository **/ @Repository public interface UserIfoRepository extends JpaRepository<UserInfoDAO, Integer> { }package my.springboot.jpa; import my.springboot.jpa.dao.UserIfoRepository; import my.springboot.jpa.entity.UserInfoDAO; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; import java.util.List; import java.util.Optional; /** * 測試UserInfo用法 **/ @RunWith(SpringRunner.class) @SpringBootTest public class UserInfoTest { @Autowired UserIfoRepository userIfoRepository; @Test public void test() { //插入用戶測試 UserInfoDAO dao = new UserInfoDAO(); dao.setUserName("小明"); dao.setAge(32); dao.setCreateDate(new Date()); dao.setCreateUser("管理員"); dao.setAddress("蘇州"); userIfoRepository.save(dao); UserInfoDAO dao2 = new UserInfoDAO(); dao2.setUserName("小張"); dao2.setAge(35); dao2.setCreateDate(new Date()); dao2.setCreateUser("管理員"); dao2.setAddress("南京"); userIfoRepository.save(dao2); // 查詢多條記錄 打印 List<UserInfoDAO> list = userIfoRepository.findAll(); for (UserInfoDAO item : list) { System.out.println("姓名:" + item.getUserName() + " 年齡:" + item.getAge()); } // 查詢單個記錄 Optional<UserInfoDAO> mo = userIfoRepository.findById(2); System.out.println(mo.get().getUserName()); //更新操作 mo.get().setUserName("小明123"); userIfoRepository.save(mo.get()); System.out.println(mo.get().getUserName()); //刪除記錄 userIfoRepository.delete(mo.get()); } }package my.springboot.jpa; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication @EntityScan(basePackages = {"my.springboot.jpa.entity"}) @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"}) public class JpaApplication { public static void main(String[] args) { SpringApplication.run(JpaApplication.class, args); } } @EntityScan(basePackages = {"my.springboot.jpa.entity"}) @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"})
看完上述內容,你們對SpringBoot中怎么集成JPA有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。