您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)SpringBoot中怎么集成JPA,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
server: port: 8090 spring: #通用的數(shù)據(jù)源配置 datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root jpa: #將默認(rèn)的存儲(chǔ)引擎切換為 InnoDB database-platform: org.hibernate.dialect.MySQL5InnoDBDialect #配置在日志中打印出執(zhí)行的 SQL 語句信息。 show-sql: true hibernate: #配置指明在程序啟動(dòng)的時(shí)候要?jiǎng)h除并且創(chuàng)建實(shí)體類對(duì)應(yīng)的表 # validate 加載 Hibernate 時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu) # create 每次加載 Hibernate ,重新創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫表數(shù)據(jù)丟失的原因。 # create-drop 加載 Hibernate 時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)(退出是指退出sessionFactory) # update 加載 Hibernate 自動(dòng)更新數(shù)據(jù)庫結(jié)構(gòu) # none 不啟用 ddl-auto: none
package my.springboot.jpa.entity; import javax.persistence.*; import java.util.Date; /** * 用戶表實(shí)體 * **/ @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; /** * 倉(cāng)庫接口類 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; /** * 測(cè)試UserInfo用法 **/ @RunWith(SpringRunner.class) @SpringBootTest public class UserInfoTest { @Autowired UserIfoRepository userIfoRepository; @Test public void test() { //插入用戶測(cè)試 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()); } // 查詢單個(gè)記錄 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"})
看完上述內(nèi)容,你們對(duì)SpringBoot中怎么集成JPA有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。