基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
在上一篇《基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD》中完成了使用JPA对实体数据的CRUD操作。
那么,有些情况,会把一些查询语句写在存储过程中,由存储过程来返回记录集。
在这里就先通过EntityManager创建命名存储过程的方法完成调用。
1.创建SQL存储过程
存储过程返回所有的联系人。
USE [demodb]
GO SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <bobenut>
-- Create date: <2017/9/14>
-- Description: <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,是对存储过程输入或输出参数的映射定义。
package com.kxh.example.demo.domain; import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedStoredProcedureQueries;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.SqlResultSetMapping;
import javax.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 class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private String name; private String phone; private String mail; public Contact() {
super();
} public Contact(String name, String phone, String mail) {
super(); this.name = name;
this.phone = phone;
this.mail = mail;
} public long getId() {
return this.id;
} public void setId(long value) {
this.id = value;
} public String getName() {
return this.name;
} public void setName(String value) {
this.name = value;
} public String getPhone() {
return phone;
} public void setPhone(String value) {
this.phone = value;
} public String getMail() {
return this.mail;
} public void setMail(String value) {
this.mail = value;
}
}
3.通过业务对象调用
在包“com.kxh.example.demo.service”下创建类“ContactsService”。
在类内,引入“EntityManager”,加上@Autowired注解由框架实例化。
通过"EntityManager"创建命名的存储过程函数,并传入上面定义的映射名进行指定调用。
然后为存储过程设置输入参数,执行并返回结果。
package com.kxh.example.demo.service; import java.util.List; import javax.persistence.EntityManager;
import javax.persistence.StoredProcedureQuery; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.kxh.example.demo.domain.Contact; @Component
public class ContactsService {
@Autowired
private EntityManager entityManager; @SuppressWarnings("unchecked")
public List<Contact> findAllViaProc(String name) {
StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("getContactsLikeName");
storedProcedureQuery.setParameter("name", name);
storedProcedureQuery.execute();
return storedProcedureQuery.getResultList();
}
}
4.通过RestController向外提供服务
引入“ContactService”作为成员变量,并Autowired。
增加一个新的访问路径映射,在处理方法中调用contactsService.findAllViaProc(nameWhere)获取查询结果集。
package com.kxh.example.demo.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import com.kxh.example.demo.dao.ContactsRepository;
import com.kxh.example.demo.domain.Contact;
import com.kxh.example.demo.service.ContactsService; @RestController
@RequestMapping("/contacts")
public class ContactsController { @Autowired
ContactsService contactsService;
//省略//通过存储过程查
@RequestMapping(value="/query/viaproc/likename", method=RequestMethod.GET)
public List<Contact> findContactsUseProcLikeName(String name) {
System.out.println("kxh1");
String nameWhere = org.apache.commons.lang.StringUtils.join(new String[]{"%", name, "%"}, "");
List<Contact> contacts = contactsService.findAllViaProc(nameWhere);
if(contacts == null) {
return new ArrayList<Contact>();
} else {
return contacts;
}
} //省略
}
End
基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合的更多相关文章
- SQL Server数据库多种方式查找重复记录
摘要:SQL Server是一个关系数据库管理系统,SQL Server数据库的应用是很多的,SQL Server数据库赢得了广大用户的青睐,本文将主要为大家介绍关于SQL Server数据库中查找重 ...
- 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD
完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中. 1.使用STS创建工程. 使用STS创建RESTful工程,可以参考: <用S ...
- Spring Boot 集成 MyBatis和 SQL Server实践
概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...
- Sql Server数据库之存储过程
阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程 简单来说,存储过程就是一条或 ...
- C#调用ODBC连接SQL Server数据库的存储过程
OdbcConnection con = new OdbcConnection("Driver={SQL Server};server=PC-200201070359;uid=sa;pwd= ...
- SQL Server数据库的存储过程中定义的临时表,真的有必要显式删除临时表(drop table #tableName)吗?
本文出处:http://www.cnblogs.com/wy123/p/6704619.html 问题背景 在写SQL Server存储过程中,如果存储过程中定义了临时表,有些人习惯在存储过程结束的时 ...
- sql server数据库查询取出重复数据记录
问题:博主在2011年6月,广东技术师范大学大四的时候,从学校计算机科学学院网站看到招聘信息并到广东中原地产IT部面试,很清楚记得当时的面试题目:怎么从数据库里面查询重复记录. 解决方案:在sql s ...
- Sql Server 数据库分页存储过程书写
create proc 存储过程名称( @page int, //pageindex @rows int, //pagesize @rowCount int out)as begin--定义字符串变量 ...
- 基于Spring Boot,使用JPA动态调用Sql查询数据
在<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>,<基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合 ...
随机推荐
- JavaScript八张思维导图—基本语句
JS基本概念 JS操作符 JS基本语句 JS数组用法 Date用法 JS字符串用法 JS编程风格 JS编程实践 不知不觉做前端已经五年多了,无论是从最初的jQuery还是现在火热的Angular,Vu ...
- ThinkPHP3.2基础知识(三)
1.如何开启调试模式,开启调试模式有什么用处? // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false define('APP_DEBUG',True); 开启调试模式的用处:方便及时发 ...
- 【深度学习系列】PaddlePaddle可视化之VisualDL
上篇文章我们讲了如何对模型进行可视化,用的keras手动绘图输出CNN训练的中途结果,本篇文章将讲述如何用PaddlePaddle新开源的VisualDL来进行可视化.在讲VisualDL之前,我们先 ...
- vue源码入口文件分析
开发vue项目有段时间了, 之前用angularjs 后来用 reactjs 但是那时候一直没有时间把自己看源码的思考记录下来,现在我不想再浪费这 来之不易的思考, 我要坚持!! 看源码我个人感觉非常 ...
- wigs的理解和应用
1. 首先了解下,Web应用的本质,大体如下: 1.浏览器发送一个HTTP请求: 2.服务器收到请求,生成一个HTML文档: 3.服务器把HTML文档作为HTTP响应的Body发送给浏览器: 4.浏览 ...
- 安装新的int 9中断例程2
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- scrapy_Response and Request
scrapy中重要的两个类是什么? Requests.Response 什么是Requests? 网页下载 有哪些参数? url callback headers # 头部信息 cookie ...
- python_如何读写csv数据
案例: 通过股票网站,我们获取了中国股市数据集,它以csv数据格式存储 Data,Open,High,Low,Close,Volume,Adj Close 2016-06-28,8.63,8.47,8 ...
- SSM与jsp传递实体类
jsp传controller Controller: @RequestMapping({"/user"}) public void registerUser(User uu) th ...
- awk匹配以aaa开头,以bbb结尾的内容,同时aaa和bbb之间还包含ccc
如果是匹配以A开头,以B结尾的内容,同时A和B之间还包含C的这种怎么做?比如 [root@localhost ~]#cat file aaa grge ddd bbb aaa gege ccc bbb ...