300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > jpa执行mysql存储过程_基于Spring Boot 使用JPA调用Sql Server数据库的存储过程并返回记录集合...

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

时间:2022-09-25 02:12: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: </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.ponent;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= mons.lang.StringUtils.join(new String[]{"%", name, "%"}, "");

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

}else{returncontacts;

}

}//省略}

End

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。