SpringBoot集成JPA用法筆記
作者:郝光明
今天給大家整理SpringBoot集成JPA用法。希望對(duì)大家能有所幫助!
今天給大家整理SpringBoot集成JPA用法。希望對(duì)大家能有所幫助!
1.搭建SpringBoot項(xiàng)目
2.新建配置文件 application.yml
- 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
3、新建用戶實(shí)體類 UserInfoDAO.java
- 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;
- }
- }
4、倉(cāng)庫接口類 UserIfoRepository
- 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> {
- }
5、新建測(cè)試用戶類 UserInfoTest.java
- 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());
- }
- }
6、配置生成數(shù)據(jù)表
- 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"})
7、項(xiàng)目結(jié)構(gòu)圖
本文轉(zhuǎn)載自微信公眾號(hào)「IT技術(shù)分享社區(qū)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系IT技術(shù)分享社區(qū)公眾號(hào)。
責(zé)任編輯:姜華
來源:
IT技術(shù)分享社區(qū)