国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

2021-05-14 11:19小知哥 Java教程

這篇文章主要介紹了springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

我們在上一篇搭建了一個(gè)簡單的springboot應(yīng)用,這一篇將介紹使用spring-data-jpa操作數(shù)據(jù)庫。

新建一個(gè)mysql數(shù)據(jù)庫,這里數(shù)據(jù)庫名為springboot,建立user_info數(shù)據(jù)表,作為我們示例操作的表對象。

user_info信息如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
drop table if exists `user_info`;
create table `user_info` (
 `id` int(11) not null auto_increment,
 `username` varchar(255) default null,
 `password` varchar(255) default null,
 primary key (`id`)
) engine=myisam auto_increment=3 default charset=utf8;
 
-- ----------------------------
-- records of user_info
-- ----------------------------
insert into `user_info` values ('1', 'java之音', '12345');
insert into `user_info` values ('2', '張無忌', '123');

數(shù)據(jù)庫及表創(chuàng)建成功后,回到我們的工程中

第零步,首先引入mysql及jpa的maven依賴包:

?
1
2
3
4
5
6
7
8
9
<dependency>
   <groupid>mysql</groupid>
   <artifactid>mysql-connector-java</artifactid>
 </dependency>
 
 <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-data-jpa</artifactid>
 </dependency>

第一步,在yml配置文件中配置數(shù)據(jù)庫連接信息:

?
1
2
3
4
5
6
7
8
spring:
 datasource:
  driver-class-name: com.mysql.jdbc.driver
  url: jdbc:mysql://localhost:3306/springboot?useunicode=true&characterencoding=utf-8&usessl=false
  username: root
  password: 1011
 jpa:
  show-sql: true

第二步,創(chuàng)建一個(gè)實(shí)體類,對應(yīng)數(shù)據(jù)表實(shí)體映射:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.javazhiyin;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
 
/**
* created by 57783 on 2018/7/4.
*/
@entity
public class userinfo {
 
  @id
  @generatedvalue(strategy = generationtype.identity)
  private integer id;
 
  private string username;
 
  private string password;
 
  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 string getpassword() {
    return password;
  }
 
  public void setpassword(string password) {
    this.password = password;
  }
 
  public userinfo(){
 
  }
}

第三步,創(chuàng)建一個(gè)repository類,繼承jparepository類:

?
1
2
3
4
5
6
7
package com.javazhiyin;
import org.springframework.data.jpa.repository.jparepository;
/**
* created by 57783 on 2018/7/4.
*/
public interface userinforepository extends jparepository<userinfo,integer>{
}

這里繼承了jparepository類,其封裝了一些對數(shù)據(jù)庫操作的基本方法,我們通過源碼查看一下jparepository有哪些方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// source code recreated from a .class file by intellij idea
// (powered by fernflower decompiler)
//
package org.springframework.data.repository;
import java.util.optional;
@norepositorybean
public interface crudrepository<t, id> extends repository<t, id> {
  <s extends t> s save(s var1);
  <s extends t> iterable<s> saveall(iterable<s> var1);
  optional<t> findbyid(id var1);
  boolean existsbyid(id var1);
  iterable<t> findall();
  iterable<t> findallbyid(iterable<id> var1);
  long count();
  void deletebyid(id var1);
  void delete(t var1);
  void deleteall(iterable<? extends t> var1);
  void deleteall();
}

第四步,新建一個(gè)controller,實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.javazhiyin;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import java.util.list;
/**
* created by 57783 on 2018/7/4.
*/
@restcontroller
public class userinfocontroller {
 
  @autowired
  private userinforepository userinforepository;
 
  /**
   * 查
   * @return
   */
  @getmapping(value = "/list")
  public list<userinfo> getuserlist(){
    return userinforepository.findall();
  }
 
  /**
   * 增
   * @param username
   * @param password
   * @return
   */
  @postmapping(value = "/adduser")
  public userinfo adduser(@requestparam("username") string username,
              @requestparam("password") string password){
    userinfo user = new userinfo();
    user.setusername(username);
    user.setpassword(password);
    return userinforepository.save(user);
  }
 
  /**
   * 改
   * @param id
   * @param username
   * @param password
   * @return
   */
  @putmapping(value = "upduser/{id}")
  public userinfo upduser(@pathvariable("id") integer id,
            @requestparam("username") string username,
            @requestparam("password") string password){
    userinfo user = new userinfo();
    user.setid(id);
    user.setusername(username);
    user.setpassword(password);
    return userinforepository.save(user);
 
  }
 
  /**
   * 刪
   * @param id
   */
  @deletemapping(value = "deluser/{id}")
  public void deluser(@pathvariable("id") integer id){
    userinfo user = new userinfo();
    user.setid(id);
    userinforepository.delete(user);
  }
}

測試上述代碼,這里我們使用postman測試,非常方便:

查詢測試:

springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

新增測試:

springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

修改測試:

springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

刪除測試:

springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

我們可以看到可以全部測試通過,springboot使用spring-data-jpa進(jìn)行增刪改查的操作確實(shí)挺方便的。

幾個(gè)問題:

1、對象實(shí)體映射類中的注解@generatedvalue的概念及用法?

jpa要求每一個(gè)實(shí)體entity,必須有且只有一個(gè)主鍵,而@generatedvalue注解會(huì)為一個(gè)實(shí)體生成一個(gè)唯一標(biāo)識的主鍵。

jpa提供了四種主鍵生成策略,被定義在枚舉類generationtype中,分別為:

  1. generationtype.table
  2. generationtype.sequence
  3. generationtype.identity
  4. generationtype.auto

generationtype.table

使用一個(gè)特定的數(shù)據(jù)庫表格來保存主鍵,持久化引擎通過關(guān)系數(shù)據(jù)庫的一張?zhí)囟ǖ谋砀駚砩芍麈I。這種策略的好處是不依賴于外部環(huán)境和數(shù)據(jù)庫的具體實(shí)現(xiàn),在不同數(shù)據(jù)庫間可以很容易的進(jìn)行移植。但由于其不能充分利用數(shù)據(jù)庫的特性,所以不會(huì)優(yōu)先使用。

generationtype.sequence

在某些數(shù)據(jù)庫中不支持主鍵自增長,比如oracle。其提供了一種叫做”序列(sequence)”的機(jī)制生成主鍵。此時(shí),generationtype.sequence就可以作為主鍵生成策略。該策略的不足之處正好與table相反,由于只有部分?jǐn)?shù)據(jù)庫(oracle,postgresql,db2)支持序列對象,所以該策略一般不應(yīng)用于其他數(shù)據(jù)庫。

generationtype.identity

主鍵自增長策略,數(shù)據(jù)庫在插入數(shù)據(jù)時(shí),會(huì)自動(dòng)給主鍵賦值,比如mysql可以在創(chuàng)建表時(shí)聲明”auto_increment” 來指定主鍵自增長。該策略在大部分?jǐn)?shù)據(jù)庫中都提供了支持(指定方法或關(guān)鍵字可能不同),但還是有少數(shù)數(shù)據(jù)庫不支持,所以可移植性略差。

generationtype.auto

把主鍵生成策略交給持久化引擎(persistence engine),持久化引擎會(huì)根據(jù)數(shù)據(jù)庫在以上三種主鍵生成策略中選擇其中一種。此種主鍵生成策略比較常用,由于jpa默認(rèn)的生成策略就是generationtype.auto,所以使用此種策略時(shí)可以顯式的指定@generatedvalue(strategy = generationtype.auto)也可以直接@generatedvalue。

2、spring data jpa提供了哪些接口,可以實(shí)現(xiàn)哪些功能?

  • repository:最頂層的接口,是一個(gè)空的接口,目的是為了統(tǒng)一所有repository的類型,且能讓組件掃描的時(shí)候自動(dòng)識別。
  • crudrepository :是repository的子接口,提供crud的功能
  • pagingandsortingrepository:是crudrepository的子接口,添加分頁和排序的功能
  • jparepository:是pagingandsortingrepository的子接口,增加了一些實(shí)用的功能,比如:批量操作等。
  • jpaspecificationexecutor:用來做負(fù)責(zé)查詢的接口
  • specification:是spring data jpa提供的一個(gè)查詢規(guī)范,要做復(fù)雜的查詢,只需圍繞這個(gè)規(guī)范來設(shè)置查詢條件即可

本文可運(yùn)行的源碼:https://github.com/javazhiyin/springboot/tree/master/bootdemo_01

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/javazhiyin/p/9297743.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文av电影 | 成人影院在线观看 | 国产精品一区二区久久久 | 亚洲情综合五月天 | 国产精品99久久久久久久vr | 久久99精品一区二区三区 | 久久黄色 | 亚洲欧美在线观看 | 亚洲一区 日韩精品 中文字幕 | 亚洲精品久久久久久下一站 | 欧美一区二区三区在线 | 夜久久 | 亚洲欧美影院 | 亚洲另类视频 | 偷拍自拍网| 草草精品视频 | 亚洲精美视频 | 国产电影一区二区 | 亚洲精品一区二区三区樱花 | 四虎av成人 | 日本在线免费观看视频 | 网站黄色在线 | 日韩免费在线视频 | 黄色av免费 | 日本视频在线 | 亚洲成年人网站在线观看 | 成人久久久久久久久 | 激情欧美一区二区三区中文字幕 | 91国内外精品自在线播放 | 国产精品去看片 | av在线免费播放 | 国产在线观看一区二区三区 | 亚洲欧美一级 | 精品网| 免费簧片 | 亚洲狼人 | 亚洲第一视频 | 成人av电影网 | 欧美不卡视频 | 欧美一级在线 | 欧美中文字幕一区 |