日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

jpa执行mysql存储过程_基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合...

發布時間:2023/12/19 数据库 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jpa执行mysql存储过程_基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

那么,有些情況,會把一些查詢語句寫在存儲過程中,由存儲過程來返回記錄集。

在這里就先通過EntityManager創建命名存儲過程的方法完成調用。

1.創建SQL存儲過程

存儲過程返回所有的聯系人。

USE [demodb]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

--=============================================--Author: --Create date: <2017/9/14>--Description: --=============================================

ALTER PROCEDURE [dbo].[proc_get_contacts_like_name]

@name varchar(50)AS

BEGIN

SET NOCOUNT ON;SELECT * from contact where name like @name;END

2.定義命名的存儲過程。

在包“com.kxh.example.demo.domain”下的“Contact”實體上編寫存儲過程的映射。

@NamedStoredProcedureQueries注解表示可以包含多個存儲過程的映射。

@NamedStoredProcedureQuery注解就是對一個存儲過程的映射。

參數name,給這次映射取一個名字,后續調用時使用。

參數procedureName,是數據庫中真實的存儲過程的名字。

參數parameters,是對存儲過程輸入或輸出參數的映射定義。

packagecom.kxh.example.demo.domain;importjavax.persistence.Entity;importjavax.persistence.EntityResult;importjavax.persistence.FieldResult;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.NamedStoredProcedureQueries;importjavax.persistence.NamedStoredProcedureQuery;importjavax.persistence.ParameterMode;importjavax.persistence.SqlResultSetMapping;importjavax.persistence.StoredProcedureParameter;

@Entity@NamedStoredProcedureQueries({

@NamedStoredProcedureQuery(

name= "getContactsLikeName",

procedureName= "proc_get_contacts_like_name",

resultClasses= { Contact.class},

parameters={

@StoredProcedureParameter(

mode=ParameterMode.IN,

name= "name",

type= String.class)

}

)

})public classContact {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)private longid;privateString name;privateString phone;privateString mail;publicContact() {super();

}publicContact(String name, String phone, String mail) {super();this.name =name;this.phone =phone;this.mail =mail;

}public longgetId() {return this.id;

}public void setId(longvalue) {this.id =value;

}publicString getName() {return this.name;

}public voidsetName(String value) {this.name =value;

}publicString getPhone() {returnphone;

}public voidsetPhone(String value) {this.phone =value;

}publicString getMail() {return this.mail;

}public voidsetMail(String value) {this.mail =value;

}

}

3.通過業務對象調用

在包“com.kxh.example.demo.service”下創建類“ContactsService”。

在類內,引入“EntityManager”,加上@Autowired注解由框架實例化。

通過"EntityManager"創建命名的存儲過程函數,并傳入上面定義的映射名進行指定調用。

然后為存儲過程設置輸入參數,執行并返回結果。

packagecom.kxh.example.demo.service;importjava.util.List;importjavax.persistence.EntityManager;importjavax.persistence.StoredProcedureQuery;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importcom.kxh.example.demo.domain.Contact;

@Componentpublic classContactsService {

@AutowiredprivateEntityManager entityManager;

@SuppressWarnings("unchecked")public ListfindAllViaProc(String name) {

StoredProcedureQuery storedProcedureQuery= this.entityManager.createNamedStoredProcedureQuery("getContactsLikeName");

storedProcedureQuery.setParameter("name", name);

storedProcedureQuery.execute();returnstoredProcedureQuery.getResultList();

}}

4.通過RestController向外提供服務

引入“ContactService”作為成員變量,并Autowired。

增加一個新的訪問路徑映射,在處理方法中調用contactsService.findAllViaProc(nameWhere)獲取查詢結果集。

packagecom.kxh.example.demo.controller;importjava.util.ArrayList;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importcom.kxh.example.demo.dao.ContactsRepository;importcom.kxh.example.demo.domain.Contact;importcom.kxh.example.demo.service.ContactsService;

@RestController

@RequestMapping("/contacts")public classContactsController {

@Autowired

ContactsService contactsService;//省略//通過存儲過程查

@RequestMapping(value="/query/viaproc/likename", method=RequestMethod.GET)public ListfindContactsUseProcLikeName(String name) {

System.out.println("kxh1");

String nameWhere= org.apache.commons.lang.StringUtils.join(new String[]{"%", name, "%"}, "");

List contacts =contactsService.findAllViaProc(nameWhere);if(contacts == null) {return new ArrayList();

}else{returncontacts;

}

}//省略}

End

總結

以上是生活随笔為你收集整理的jpa执行mysql存储过程_基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。