基于Spring Boot,使用JPA动态调用Sql查询数据
在《基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD》,《基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合》完成了CRUD,调用存储过程查询数据。
很多复杂的情况下,会存在要直接执行SQL来获取数据。
通过“EntityManager”创建NativeQuery方法来执行动态SQL。
1.查询结果集映射
在包“com.kxh.example.demo.domain”下的“Contact”实体上编写命名的结果集映射,因为可以写很多映射。
@SqlResultSetMapping注解即为映射。
name参数,可以为结果集映射取个名字。
entities参数,用来说明把Entity和查询的结果字段进行关联说明。
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
@SqlResultSetMapping(
name = "conatctMapping",
entities = @EntityResult(
entityClass = Contact.class,
fields = {
@FieldResult(name = "name", column = "name"),
@FieldResult(name = "phone", column = "phone"),
@FieldResult(name = "mail", column = "mail")})
)
@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"创建NativeQuery函数,第一参数是Sql,第二个参数就是上面定义的结果集映射名。
然后传入查询条件参数,设置最大返回结果记录数,获取查询结果集。
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();
} @SuppressWarnings("unchecked")
public List<Contact> findAllByViaQuery(String name) {
List<Contact> contacts = this.entityManager
.createNativeQuery("select name, phone, mail from contact where name like :name", "conatctMapping")
.setParameter("name", name)
.setMaxResults(5)
.getResultList(); return contacts;
}
}
4.通过RestController向外提供服务
增加一个新的访问路径映射,在处理方法中调用contactsService.findAllByViaQuery(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;//省略
//通过动态sql查
@RequestMapping(value="/query/viadnq/likename", method=RequestMethod.GET)
public List<Contact> findContactsUseDyanamicQueryLikeName(String name) {
System.out.println("kxh1");
String nameWhere = org.apache.commons.lang.StringUtils.join(new String[]{"%", name, "%"}, "");
List<Contact> contacts = contactsService.findAllByViaQuery(nameWhere);
if(contacts == null) {
System.out.println("kxh4");
return new ArrayList<Contact>();
} else {
System.out.println("kxh5");
return contacts;
}
}
}
End
基于Spring Boot,使用JPA动态调用Sql查询数据的更多相关文章
- 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 基于Spring Boot+Cloud构建微云架构
前言 首先,最想说的是,当你要学习一套最新的技术时,官网的英文文档是学习的最佳渠道.因为网上流传的多数资料是官网翻译而来,很多描述的重点也都偏向于作者自身碰到的问题,这样就很容易让你理解和操作出现偏差 ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - JPA整合
文章目录 1. 环境依赖 2. 数据源 3. 脚本初始化 4. JPA 整合方案一 通过继承 JpaRepository 接口 4.1. 实体对象 4.2. DAO相关 4.3. Service相关 ...
- Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用
Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...
- 基于Spring Boot和Spring Cloud实现微服务架构
官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...
随机推荐
- [国嵌攻略][157][SPI总线介绍]
SPI总线架构 SPI(serial peripheral interface)串行外设接口,是一种高速,全双工,同步的通信总线.采用主从模式(master slave)架构,支持多个slave,一般 ...
- [国嵌攻略][098][Linux内核简介]
Linux系统架构 1.用户空间:应用程序.C函数库 2.内核空间:系统调用接口.内核.体系结构相关代码 Linux系统利用处理器不同的工作模式,使用其中的两个级别分别来运行Linux内核与应用程序, ...
- mysql按照天统计报表,当天没有数据,填0
1.问题复现: 按照天数统计每天的总数,如果其中有几天没有数据,那么group by 返回会忽略那几天,如何填充0?如下图,统计的10-3~10-10 7天的数据,其中只有8号和10号有数据,这样返回 ...
- JavaScript变量声明与提升
一直以来对变量提升都是比较模糊的,今天特地看了一下这个知识点,总结一下. 1.举个最简单的例子来说一下什么是变量提升吧. function foo(){ console.log(x); // unde ...
- [最直白版]一步一步教你用VMware Workstation12安装Ubuntu 16.04和VMware Tools的教程
[最直白版]Win10下一步一步教你用 VMware Workstation12安装Ubuntu 16.04和VMware Tools的教程 安装过程中使用的软件(要保证电脑里面有下列三个东西): 1 ...
- logback的使用和logback.xml详解
一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它两 ...
- boostrap ajax表单验证提交
=============================================================================== 1. 1 <link href=& ...
- iOS学习之Map,定位,标记位置的使用
iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可.这次要实现的效果如下: 有标注(大头针),定位,地图. 1.添加地图 1.1 新一个Single V ...
- Java之IO流学习总结【下】
2.字节流 |-- InputStream(读) |-- OutputStream(写) 由于字节是二进制数据,所以字节流可以操作任何类型的数据,值得注意的是字符流使用的是字符数组char[]而字节流 ...
- [one day one question] Vue数组变更不能触发刷新
问题描述:Vue数组变更不能触发刷新,特别是数组的每个元素都是对象的时候,对象中某个属性的值发生变化,根本无法触发Vue的dom刷新,这怎么破? 解决方案:this.$set(array, index ...