久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

SpringBoot集成JPA用法筆記

 IT技術(shù)分享社區(qū) 2022-10-20 發(fā)布于江蘇

               

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

  1. 搭建SpringBoot項目

               

               

               

  1. 新建配置文件 application.yml

server:port: 8090spring:#通用的數(shù)據(jù)源配置  datasource:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8username: rootpassword: rootjpa:#將默認(rèn)的存儲引擎切換為 InnoDB    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect#配置在日志中打印出執(zhí)行的 SQL 語句信息,。    show-sql: true    hibernate:#配置指明在程序啟動的時候要刪除并且創(chuàng)建實體類對應(yīng)的表      # validate 加載 Hibernate 時,驗證創(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 時創(chuàng)建,退出是刪除表結(jié)構(gòu)(退出是指退出sessionFactory)      # update 加載 Hibernate 自動更新數(shù)據(jù)庫結(jié)構(gòu)      # none 不啟用      ddl-auto: none

3,、新建用戶實體類 UserInfoDAO.java

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;@Columnprivate String userName;@Columnprivate 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、倉庫接口類 UserIfoRepository

package my.springboot.jpa.dao;import my.springboot.jpa.entity.UserInfoDAO;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;/** * 倉庫接口類 UserIfoRepository **/@Repositorypublic interface UserIfoRepository extends JpaRepository<UserInfoDAO, Integer> {}


5,、新建測試用戶類 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;
/** * 測試UserInfo用法 **/@RunWith(SpringRunner.class)@SpringBootTestpublic 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()); }}

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、項目結(jié)構(gòu)圖

               

IT技術(shù)分享社區(qū)

個人博客網(wǎng)站:https://

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多