日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
SpringBoot集成JPA用法筆記

今天給大家整理SpringBoot集成JPA用法。希望對(duì)大家能有所幫助!

1.搭建SpringBoot項(xiàng)目

2.新建配置文件 application.yml

 
 
 
 
  1. server:
  2. port: 8090
  3. spring:
  4. #通用的數(shù)據(jù)源配置
  5.   datasource:
  6. driverClassName: com.mysql.jdbc.Driver
  7. url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
  8. username: root
  9. password: root
  10. jpa:
  11. #將默認(rèn)的存儲(chǔ)引擎切換為 InnoDB
  12.     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  13. #配置在日志中打印出執(zhí)行的 SQL 語(yǔ)句信息。
  14.     show-sql: true
  15.     hibernate:
  16. #配置指明在程序啟動(dòng)的時(shí)候要?jiǎng)h除并且創(chuàng)建實(shí)體類對(duì)應(yīng)的表
  17.       # validate 加載 Hibernate 時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)
  18.       # create 每次加載 Hibernate ,重新創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的原因。
  19.       # create-drop 加載 Hibernate 時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)(退出是指退出sessionFactory)
  20.       # update 加載 Hibernate 自動(dòng)更新數(shù)據(jù)庫(kù)結(jié)構(gòu)
  21.       # none 不啟用
  22.       ddl-auto: none

 3、新建用戶實(shí)體類 UserInfoDAO.java

 
 
 
 
  1. package my.springboot.jpa.entity;
  2. import javax.persistence.*;
  3. import java.util.Date;
  4. /**
  5.  * 用戶表實(shí)體
  6.  * **/
  7. @Entity
  8. @Table(name = "userinfo")
  9. public class UserInfoDAO {
  10. @Id
  11. @GeneratedValue(strategy = GenerationType.IDENTITY)
  12. private  Integer id;
  13. @Column
  14. private String userName;
  15. @Column
  16. private Integer age;
  17. @Column(length = 500)
  18. private String address;
  19. @Column(name = "create_date")
  20. private Date createDate;
  21. @Column(name = "create_user")
  22. private String createUser;
  23. public Integer getId() {
  24. return id;
  25.     }
  26. public void setId(Integer id) {
  27. this.id = id;
  28.     }
  29. public String getUserName() {
  30. return userName;
  31.     }
  32. public void setUserName(String userName) {
  33. this.userName = userName;
  34.     }
  35. public Integer getAge() {
  36. return age;
  37.     }
  38. public void setAge(Integer age) {
  39. this.age = age;
  40.     }
  41. public String getAddress() {
  42. return address;
  43.     }
  44. public void setAddress(String address) {
  45. this.address = address;
  46.     }
  47. public Date getCreateDate() {
  48. return createDate;
  49.     }
  50. public void setCreateDate(Date createDate) {
  51. this.createDate = createDate;
  52.     }
  53. public String getCreateUser() {
  54. return createUser;
  55.     }
  56. public void setCreateUser(String createUser) {
  57. this.createUser = createUser;
  58.     }
  59. }

4、倉(cāng)庫(kù)接口類 UserIfoRepository

 
 
 
 
  1. package my.springboot.jpa.dao;
  2. import my.springboot.jpa.entity.UserInfoDAO;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.stereotype.Repository;
  5. /**
  6.  * 倉(cāng)庫(kù)接口類 UserIfoRepository
  7.  **/
  8. @Repository
  9. public interface UserIfoRepository extends 
  10. JpaRepository {
  11. }

5、新建測(cè)試用戶類 UserInfoTest.java

 
 
 
 
  1. package my.springboot.jpa;
  2. import my.springboot.jpa.dao.UserIfoRepository;
  3. import my.springboot.jpa.entity.UserInfoDAO;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.Optional;
  12. /**
  13.  * 測(cè)試UserInfo用法
  14.  **/
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. public class UserInfoTest {
  18. @Autowired
  19.     UserIfoRepository userIfoRepository;
  20. @Test
  21.     public void test() {
  22. //插入用戶測(cè)試
  23.         UserInfoDAO dao = new UserInfoDAO();
  24.         dao.setUserName("小明");
  25.         dao.setAge(32);
  26.         dao.setCreateDate(new Date());
  27.         dao.setCreateUser("管理員");
  28.         dao.setAddress("蘇州");
  29. userIfoRepository.save(dao);
  30.         UserInfoDAO dao2 = new UserInfoDAO();
  31.         dao2.setUserName("小張");
  32.         dao2.setAge(35);
  33.         dao2.setCreateDate(new Date());
  34.         dao2.setCreateUser("管理員");
  35.         dao2.setAddress("南京");
  36. userIfoRepository.save(dao2);
  37. // 查詢多條記錄 打印
  38.         List list = userIfoRepository.findAll();
  39. for (UserInfoDAO item : list) {
  40.             System.out.println("姓名:" + item.getUserName() 
  41. + " 年齡:" + item.getAge());        }
  42. // 查詢單個(gè)記錄
  43.         Optional mo = userIfoRepository.findById(2);
  44.         System.out.println(mo.get().getUserName());
  45. //更新操作
  46.         mo.get().setUserName("小明123");
  47. userIfoRepository.save(mo.get());
  48.         System.out.println(mo.get().getUserName());
  49. //刪除記錄
  50.         userIfoRepository.delete(mo.get());
  51.     }
  52. }

6、配置生成數(shù)據(jù)表

 
 
 
 
  1. package my.springboot.jpa;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  6. @SpringBootApplication
  7. @EntityScan(basePackages = {"my.springboot.jpa.entity"})
  8. @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"})
  9. public class JpaApplication {
  10. public static void main(String[] args) {
  11.         SpringApplication.run(JpaApplication.class, args);
  12.     }
  13. }
  14. @EntityScan(basePackages = {"my.springboot.jpa.entity"})
  15. @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"})

7、項(xiàng)目結(jié)構(gòu)圖

本文轉(zhuǎn)載自微信公眾號(hào)「IT技術(shù)分享社區(qū)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系IT技術(shù)分享社區(qū)公眾號(hào)。


當(dāng)前文章:SpringBoot集成JPA用法筆記
網(wǎng)頁(yè)URL:http://m.5511xx.com/article/ccegisc.html