基于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数据库的存储过程并返回记录集合 ...
随机推荐
- Flume环境搭建_五种案例
Flume环境搭建_五种案例 http://flume.apache.org/FlumeUserGuide.html A simple example Here, we give an example ...
- 如何开发由Create-React-App 引导的应用(三)
此文章是翻译How to develop apps bootstrapped with Create React App 官方文档 系列文章 如何开发由Create-React-App 引导的应用 如 ...
- linux CentOS部署【minimal 】
1.为什么选择CentOS不选择其他版本:http://www.cnblogs.com/TeemoHQ/p/6377260.html 2.准备的资源:VMware[官网下载],CentOS镜像 [阿里 ...
- 一个简洁的PHP可逆加密函数(分享)
http://www.jb51.net/article/38018.htm 本篇文章是对一个简洁的PHP可逆加密函数进行了详细的分析介绍,需要的朋友参考下 很多时候我们需要对数据进行加密解密,比如 ...
- tp5 点击刷新验证码
<form action="<{:url('index/index/login')}>" method="post" name="f ...
- Vue.js的坑
参考网址:http://cn.vuejs.org/v2/guide/components.html 1.camelCase vs. kebab-case HTML 特性不区分大小写.当使用非字符串模版 ...
- java中的nextLine
package scanner; import java.util.Scanner; public class NextLine { public static void main(String[] ...
- SSH key introduction
Preface At the first time, we take the connection with GitLab remote server. You need to type userna ...
- mysql-冗余和重复索引
mysql允许在相同列上创建多个索引,无论是有意还是无意,mysql需要单独维护重复的索引,并且优化器在优化查询的时候也需要逐个地进行考虑,这会影响性能. 重复索引是指的在相同的列上按照相同的顺序创建 ...
- python_11_字符编码
什么是字符编码? --世界上有很多国家,每个国家都有自己独特的语言,所以在计算机普及的当今世界, 每个国家都有自己的字符编码,本国的软件运行在其他国家的机器上,会出现乱码, 有utf-8,gbk等各种 ...