激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Springboot+hibernate實現簡單的增刪改查示例

Springboot+hibernate實現簡單的增刪改查示例

2021-05-26 13:12*眉間緣* Java教程

今天小編就為大家分享一篇Springboot+hibernate實現簡單的增刪改查示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1、創建好項目之后在Springboot+hibernate實現簡單的增刪改查示例配置端口號(也可以不用配置,默認端口8080)

?
1
2
3
#server
server.port=8080
server.tomcat.uri-encoding=utf-8

2、配置mysql

?
1
2
3
4
5
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterencoding=utf8
spring.datasource.username=*****
spring.datasource.password=*****

3、配置jpa以及視圖層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#spring data jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.mysql5dialect
 
#視圖層控制
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**

4、在pom中加入springboot需要的依賴

?
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
<dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.39</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-thymeleaf</artifactid>
      <version>1.4.0.release</version>
    </dependency>
 
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<!--    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-jdbc</artifactid>
      <version>1.4.3.release</version>
    </dependency>-->
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-jpa</artifactid>
      <version>1.5.1.release</version>
    </dependency>
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>fastjson</artifactid>
      <version>1.2.46</version>
    </dependency>
 
 
  </dependencies>

整個包結構

Springboot+hibernate實現簡單的增刪改查示例

controller

?
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
68
69
70
71
72
73
package com.song.configuration.controller;
 
import com.alibaba.fastjson.jsonobject;
import com.song.configuration.entity.user;
import com.song.configuration.service.userservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;
 
import java.util.list;
 
 
/**
 *
 * user控制層
 */
@controller
@requestmapping(value = "/user")
public class usercontroller {
  @autowired
  private userservice userservice;
 
  @requestmapping(value = "/index")
  public string index(){
    return "user/index";
  }
 
  @requestmapping(value = "/show",method = requestmethod.get)
  @responsebody
  public string show(@requestparam(value = "name")string name){
    user user = userservice.finduserbyname(name);
    if(null != user)
      return user.getid()+"/"+user.getname()+"/"+user.getpassword();
    else return "null";
  }
 
  @requestmapping("/showlist")
  @responsebody
  public jsonobject showlist(){
    list<user> list = userservice.find();
    jsonobject jo = new jsonobject();
    if(list!=null){
 
      jo.put("code",0);
      jo.put("msg",true);
      jo.put("count",list.size());
      jo.put("data",list);
    }
    return jo;
  }
 
  @requestmapping("/delete")
  @responsebody
  public string deleteuserbyid(@requestparam(value = "id")integer id){
    return userservice.deleteuserbyid(id);
  }
 
  @requestmapping("/update")
  @responsebody
  public string queryuserbyid(@requestparam(value = "id")integer id,@requestparam(value = "name")string name){
 
    return userservice.queryuserbyid(id,name);
  }
 
  @requestmapping("/add")
  @responsebody
  public string countuserby(@requestparam(value = "id")integer id,@requestparam(value = "name")string name,@requestparam(value = "password")string password){
    return userservice.countuserby(id,name,password);
  }
}

service

?
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
package com.song.configuration.service;
 
import com.song.configuration.entity.user;
import com.song.configuration.repository.userrepositoty;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
 
import java.util.list;
 
/**
 *
 * user業務邏輯
 */
@service
public class userservice {
  @autowired
  private userrepositoty userrepositoty;
 
  public user finduserbyname(string name) {
    user user = null;
    try {
      user = userrepositoty.findbyusername(name);
    } catch (exception e) {
    }
    return user;
  }
 
  public list<user> find() {
    list<user> list = null;
    try {
      list = userrepositoty.find();
    } catch (exception e) {
    }
    return list;
  }
 
  public string deleteuserbyid(integer id){
    int a = userrepositoty.deleteuserbyid(id);
    return "chenggong";
  }
 
  public string queryuserbyid(integer id ,string name){
    int a = userrepositoty.queryuserbyid(id,name);
    return "成功";
  }
 
  public string countuserby(integer id ,string name ,string password){
    int a = userrepositoty.countuserby(id,name,password);
    return "成功";
  }
}

repository

?
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
package com.song.configuration.repository;
 
import com.song.configuration.entity.user;
import org.springframework.data.jpa.repository.jparepository;
import org.springframework.data.jpa.repository.modifying;
import org.springframework.data.jpa.repository.query;
import org.springframework.data.repository.query.param;
import org.springframework.stereotype.repository;
import org.springframework.transaction.annotation.transactional;
 
import java.util.list;
 
/**
 * created by song on 2017/2/15.
 * user表操作接口
 */
@repository
public interface userrepositoty extends jparepository<user,long>{
  /*
  * 根據用戶名查詢
  * */
  @query("select t from user t where t.name = :name")
  user findbyusername(@param("name") string name);
 
  /*
  * 查詢全部
  * */
  @query("select t from user t")
  list<user> find();
 
  /*
  * 刪除 必須加入@modifying和@transactional
  * */
  @modifying
  @transactional
  @query("delete from user u where u.id=:id")
  public int deleteuserbyid(@param("id") integer id);
 
 
  @modifying
  @transactional
  @query("update user u set u.name = :name where u.id=:id")
  public int queryuserbyid(@param("id") integer id,@param("name") string name);
    
  @query(value = "insert into user value(?,?,?)", nativequery = true)
  @transactional
  @modifying
  public int countuserby(@param("id")integer id,@param("name") string name,@param("password") string password);
}

@modifying:

(1)可以通過自定義的 jpql 完成 update 和 delete 操作。注意: jpql 不支持使用 insert;

(2)在 @query 注解中編寫 jpql 語句, 但必須使用 @modifying 進行修飾. 以通知   springdata, 這是一個 update 或 delete 操作

(3)update 或 delete 操作需要使用事務,此時需要定義 service 層,在 service 層的方法上添加事務操作;

(4)默認情況下, springdata 的每個方法上有事務, 但都是一個只讀事務。

@transactional:

a. 一個功能是否要事務,必須納入設計、編碼考慮。不能僅僅完成了基本功能就ok。

b. 如果加了事務,必須做好開發環境測試(測試環境也盡量觸發異常、測試回滾),確保事務生效。

c. 以下列了事務使用過程的注意事項,請大家留意。

1. 不要在接口上聲明@transactional ,而要在具體類的方法上使用 @transactional 注解,否則注解可能無效。

2.不要圖省事,將@transactional放置在類級的聲明中,放在類聲明,會使得所有方法都有事務。故@transactional應該放在方法級別,不需要使用事務的方法,就不要放置事務,比如查詢方法。否則對性能是有影響的。

3.使用了@transactional的方法,對同一個類里面的方法調用,

@transactional無效。比如有一個類test,它的一個方法a,a再調用test本類的方法b(不管b是否public還是private),但a沒有聲明注解事務,而b有。則外部調用a之后,b的事務是不會起作用的。(經常在這里出錯)

4.使用了@transactional的方法,只能是public,@transactional注解的方法都是被外部其他類調用才有效,故只能是public。道理和上面的有關聯。故在

protected、private 或者 package-visible 的方法上使用 @transactional

注解,它也不會報錯,但事務無效。

5.經過在icore-claim中測試,效果如下:

a.拋出受查異常xxxexception,事務會回滾。

b.拋出運行時異常nullpointerexception,事務會回滾。

c.quartz中,execute直接調用加了@transactional方法,可以回滾;間接調用,不會回滾。(即上文3點提到的)

d.異步任務中,execute直接調用加了@transactional方法,可以回滾;間接調用,不會回滾。(即上文3點提到的)

e.在action中加上@transactional,不會回滾。切記不要在action中加上事務。

f.在service中加上@transactional,如果是action直接調該方法,會回滾,如果是間接調,不會回滾。(即上文3提到的)

g.在service中的private加上@transactional,事務不會回滾。

application:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.song.configuration;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.componentscan;
 
/**
 *
 * 項目啟動入口,配置包根路徑
 */
@springbootapplication
@componentscan(basepackages = "com.song.configuration")
public class entry {
  public static void main(string[] args) throws exception {
    springapplication.run(entry.class, args);
  }
}

jpaconfiguration:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.song.configuration;
 
import org.springframework.boot.autoconfigure.domain.entityscan;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.ordered;
import org.springframework.core.annotation.order;
import org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor;
import org.springframework.data.jpa.repository.config.enablejparepositories;
import org.springframework.transaction.annotation.enabletransactionmanagement;
 
 
@order(ordered.highest_precedence)
@configuration
@enabletransactionmanagement(proxytargetclass = true)
@enablejparepositories(basepackages = "com.song.configuration.repository")
@entityscan(basepackages = "com.song.configuration.entity")
public class jpaconfiguration {
  @bean
  persistenceexceptiontranslationpostprocessor persistenceexceptiontranslationpostprocessor(){
    return new persistenceexceptiontranslationpostprocessor();
  }
}

其他包要在jpaconfiguration所在包下面,不然找不到路徑

以上這篇springboot+hibernate實現簡單的增刪改查示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/NCL--/p/8539288.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级一级一级一级毛片 | 一本色道久久综合亚洲精品小说 | 91av在线免费观看 | 久久久久久中文字幕 | 日韩色视频 | av电影免费在线 | 国产精品午夜性视频 | 欧美片一区二区 | 亚洲视频黄 | www.91sp | 国产亚洲美女精品久久久2020 | 免费的性生活视频 | 国产日韩成人 | 羞羞视频免费网站含羞草 | 中文字幕在线观看精品 | 成人在线视频免费看 | 黄色免费大片 | 欧美精品v国产精品v日韩精品 | 成人免费一区二区三区在线观看 | 91成人精品 | 91久久免费| 亚洲国产精品久久久久久久久久久 | 视频一区二区国产 | 成人三级电影网 | 久久精品23 | 久在线观看 | 亚洲性生活视频 | 又黄又爽免费无遮挡在线观看 | 成人在线观看免费观看 | 国产精品久久久久久久久久久久久久久久 | 亚洲性生活免费视频 | 国产精品一品二区三区四区18 | 久啪视频| 九色免费视频 | 免费看欧美黑人毛片 | 国产一区毛片 | 日韩视频在线观看免费 | 毛片一区二区三区四区 | 欧美一级黑人 | 国产一级毛片高清视频 | 精品国产91久久久久久 |