JPA SQL 的复杂查询createNamedQuery
@NamedNativeQueries({
@NamedNativeQuery(
name = "getNativeNutShellInfo", //需要调用的name
query = "{call cp_syslogin_web(?,?,?)}", //存储过程,返回的List字段对应的值@EntityResult此处填充实体的属性值
resultSetMapping = "ReturnColumnEntityList"
)
}) @SqlResultSetMappings({
@SqlResultSetMapping(
name = "ReturnColumnEntityList",
entities = {
@EntityResult(entityClass = NutShellInfo.class,fields={ //返回的List字段对应的值此处填充实体的属性值
@FieldResult(name = "aguid", column = "aguid"),
@FieldResult(name = "user", column = "htkusr"),
@FieldResult(name = "qty", column = "qtynow"),
@FieldResult(name = "registered", column = "breg"),
@FieldResult(name = "hint", column = "hint"),
@FieldResult(name = "ver", column = "ver"),
@FieldResult(name = "overQty", column = "boverqty"),
@FieldResult(name = "timeout", column = "btimeout"),
@FieldResult(name = "host", column = "htkwww")
}) } )
}) @Entity
public class NutShellInfo {
@Id
private String aguid;
@Column(name = "htkusr")
private String user;
@Column(name = "qtynow")
private double qty;
@Column(name = "breg")
private boolean registered;
private String hint;
private String ver;
@Column(name = "boverqty")
private boolean overQty;
@Column(name = "btimeout")
private boolean timeout;
@Column(name = "htkwww")
private String host;
@Transient
private String password;//这个字段不对数据库操作 public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
} public double getQty() {
return qty;
} public void setQty(double qty) {
this.qty = qty;
} public boolean isRegistered() {
return registered;
} public void setRegistered(boolean registered) {
this.registered = registered;
} public String getHint() {
return hint;
} public void setHint(String hint) {
this.hint = hint;
} public String getVer() {
return ver;
} public void setVer(String ver) {
this.ver = ver;
} public boolean isOverQty() {
return overQty;
} public void setOverQty(boolean overQty) {
this.overQty = overQty;
} public boolean isTimeout() {
return timeout;
} public void setTimeout(boolean timeout) {
this.timeout = timeout;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} =========================dao=========================
@Service
public class PubBaseDaoImpl<T> implements PubBaseDao<T> {
private EntityManager em = null; @PersistenceContext
public void setEm(EntityManager em) {
this.em = em;
} @Override
public List<T> qryNamedQuery(String nativeName,Object[] params) {
Query q = em.createNamedQuery(nativeName);
setQueryParams(q, params);
return q.getResultList();
} public void setQueryParams(Query query, Object[] params) {
try {
if (params != null && params.length > ) {
for (int i = ; i < params.length; i++) {
query.setParameter(i + , params[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} =================services===================================
@Override
public void qryInfo(String userId, String machineCode) {
List<NutShellInfo> list = baseDao.qryNamedQuery("getNativeNutShellInfo",new Object[] { userId, machineCode, }); } JPA 原生态SQL 的复杂查询之createNamedQuery,select * from查询,返回的List字段对应的填充实体
============实体类==========================
@NamedNativeQueries({
@NamedNativeQuery(
name = "getNativeNutShellInfo", //需要调用的name
query = "select * from NutShellInfo where aguid=? and htkusr=? and btimeout=? ", //存储过程,返回的List字段对应的值@EntityResult此处填充实体的属性值
resultSetMapping = "ReturnColumnEntityList"
)
}) @SqlResultSetMappings({
@SqlResultSetMapping(
name = "ReturnColumnEntityList",
entities = {
@EntityResult(entityClass = NutShellInfo.class,fields={ //返回的List字段对应的值此处填充实体的属性值
@FieldResult(name = "aguid", column = "id"),
@FieldResult(name = "user", column = "htkusr"),
@FieldResult(name = "qty", column = "qtynow"),
@FieldResult(name = "registered", column = "breg"),
@FieldResult(name = "hint", column = "hint"),
@FieldResult(name = "ver", column = "ver"),
@FieldResult(name = "overQty", column = "boverqty"),
@FieldResult(name = "timeout", column = "btimeout"),
@FieldResult(name = "host", column = "htkwww")
}) } )
}) @Entity
public class NutShellInfo {
@Id
private String aguid;
@Column(name = "htkusr")
private String user;
@Column(name = "qtynow")
private double qty;
@Column(name = "breg")
private boolean registered;
private String hint;
private String ver;
@Column(name = "boverqty")
private boolean overQty;
@Column(name = "btimeout")
private boolean timeout;
@Column(name = "htkwww")
private String host;
@Transient
private String password;//这个字段不对数据库操作 public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
} public double getQty() {
return qty;
} public void setQty(double qty) {
this.qty = qty;
} public boolean isRegistered() {
return registered;
} public void setRegistered(boolean registered) {
this.registered = registered;
} public String getHint() {
return hint;
} public void setHint(String hint) {
this.hint = hint;
} public String getVer() {
return ver;
} public void setVer(String ver) {
this.ver = ver;
} public boolean isOverQty() {
return overQty;
} public void setOverQty(boolean overQty) {
this.overQty = overQty;
} public boolean isTimeout() {
return timeout;
} public void setTimeout(boolean timeout) {
this.timeout = timeout;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} =========================dao=========================
@Service
public class PubBaseDaoImpl<T> implements PubBaseDao<T> {
private EntityManager em = null; @PersistenceContext
public void setEm(EntityManager em) {
this.em = em;
} @Override
public List<T> qryNamedQuery(String nativeName,Object[] params) {
Query q = em.createNamedQuery(nativeName);
setQueryParams(q, params);
return q.getResultList();
} public void setQueryParams(Query query, Object[] params) {
try {
if (params != null && params.length > ) {
for (int i = ; i < params.length; i++) {
query.setParameter(i + , params[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} =================services===================================
@Override
public void qryInfo(String userId, String machineCode) {
List<NutShellInfo> list = baseDao.qryNamedQuery("getNativeNutShellInfo",new Object[] { userId, machineCode, }); } JPA 原生态SQL 的复杂查询之createNamedQuery,select xxx,xxx from查询,返回的List字段对应的填充实体
============实体类==========================
@NamedNativeQueries({
@NamedNativeQuery(
name = "getNativeNutShellInfo", //需要调用的name
query = "select aguid,htkusr from NutShellInfo where aguid=? and htkusr=? and btimeout=? ", //存储过程,返回的List字段对应的值@EntityResult此处填充实体的属性值
resultSetMapping = "ReturnColumnEntityList"
)
}) @SqlResultSetMappings({
@SqlResultSetMapping(
name = "ReturnColumnEntityList",
entities = {}
columns = {
@ColumnResult(name = "aguid"),
@ColumnResult(name = "htkusr"),
}
)
}) @Entity
public class NutShellInfo {
@Id
private String aguid;
@Column(name = "htkusr")
private String user;
@Column(name = "qtynow")
private double qty;
@Column(name = "breg")
private boolean registered;
private String hint;
private String ver;
@Column(name = "boverqty")
private boolean overQty;
@Column(name = "btimeout")
private boolean timeout;
@Column(name = "htkwww")
private String host;
@Transient
private String password;//这个字段不对数据库操作 public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
} public double getQty() {
return qty;
} public void setQty(double qty) {
this.qty = qty;
} public boolean isRegistered() {
return registered;
} public void setRegistered(boolean registered) {
this.registered = registered;
} public String getHint() {
return hint;
} public void setHint(String hint) {
this.hint = hint;
} public String getVer() {
return ver;
} public void setVer(String ver) {
this.ver = ver;
} public boolean isOverQty() {
return overQty;
} public void setOverQty(boolean overQty) {
this.overQty = overQty;
} public boolean isTimeout() {
return timeout;
} public void setTimeout(boolean timeout) {
this.timeout = timeout;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} =========================dao=========================
@Service
public class PubBaseDaoImpl<T> implements PubBaseDao<T> {
private EntityManager em = null; @PersistenceContext
public void setEm(EntityManager em) {
this.em = em;
} @Override
public List<T> qryNamedQuery(String nativeName,Object[] params) {
Query q = em.createNamedQuery(nativeName);
setQueryParams(q, params);
return q.getResultList();
} public void setQueryParams(Query query, Object[] params) {
try {
if (params != null && params.length > ) {
for (int i = ; i < params.length; i++) {
query.setParameter(i + , params[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} =================services===================================
@Override
public void qryInfo(String userId, String machineCode) {
List<NutShellInfo> list = baseDao.qryNamedQuery("getNativeNutShellInfo",new Object[] { userId, machineCode, }); }
JPA SQL 的复杂查询createNamedQuery的更多相关文章
- JPA 原生态SQL 的复杂查询之createNamedQuery
JPA 原生态SQL 的复杂查询之createNamedQuery调用存储过程,返回的List字段对应的填充实体============实体类,调用存储过程====================== ...
- spring boot jpa 多条件组合查询带分页的案例
spring data jpa 是一个封装了hebernate的dao框架,用于单表操作特别的方便,当然也支持多表,只不过要写sql.对于单表操作,jpake可以通过各种api进行搞定,下面是一个对一 ...
- JPA 使用 Specification 复杂查询和 Criteria 查询
转自:https://blog.wuwii.com/jpa-specification.html 前言 JPA 给我们提供了基础的 CURD 的功能,并且用起来也是特别的方便,基本都是一行代码完成各种 ...
- Spring Data Jpa的四种查询方式
一.调用接口的方式 1.基本介绍 通过调用接口里的方法查询,需要我们自定义的接口继承Spring Data Jpa规定的接口 public interface UserDao extends JpaR ...
- Spring Data JPA 自定义对象接收查询结果集
Spring Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和 ...
- jpa jpql @query 动态查询
需求/背景 假设有一个用户表, 对应的用户实体: public class User { @Id Long id; //姓名 String name; //性别,男0女1 String sex; // ...
- Sql Server中查询今天、昨天、本周、上周、本月、上月数据
Sql Server中查询今天.昨天.本周.上周.本月.上月数据 在做Sql Server开发的时候有时需要获取表中今天.昨天.本周.上周.本月.上月等数据,这时候就需要使用DATEDIFF()函数及 ...
- sql跨库查询
---------------------------------------------------------------------------------- --1. 创建链接服务器 --1. ...
- SQL 数据分页查询
最近学习了一下SQL的分页查询,总结了以下几种方法. 首先建立了一个表,随意插入的一些测试数据,表结构和数据如下图: 现在假设我们要做的是每页5条数据,而现在我们要取第三页的数据.(数据太少,就每页5 ...
随机推荐
- 常见hash算法的原理
散列表,它是基于快速存取的角度设计的,也是一种典型的“空间换时间”的做法.顾名思义,该数据结构可以理解为一个线性表,但是其中的元素不是紧密排列的,而是可能存在空隙. 散列表(Hash table,也叫 ...
- 重构20-Extract Subclass(提取父类)
当一个类中的某些方法并不是面向所有的类时,可以使用该重构将其迁移到子类中.我这里举的例子十分简单,它包含一个Registration类,该类处理与学生注册课程相关的所有信息. public class ...
- Oracle基础 (十二)数学函数
数学函数: ABS(n):求绝对值 ),) FROM DUAL; --获取15的绝对值 结果:, BITAND(X,Y):返回X,Y进行位与(AND)的运算结果 ,), BITAND(,), BITA ...
- xe5 android sample 中的 SimpleList 是怎样绑定的
C:\Users\Public\Documents\RAD Studio\12.0\Samples\FireMonkeyMobile 例子中的绑定方式如下图: 1.拖拽一个listview到界面上,然 ...
- sql 游标例子 根据一表的数据去筛选另一表的数据
sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...
- OpenShare常见问题及解答
OpenShare常见问题及回答: Q:OpenShare可以整合SAP么? A:当然可以,OpenShare是真正完全开放的产品,但要进行二次开发,事实上我们帮我们大部分的客户都整合了SAP,包括数 ...
- Meta也很强
<!--http-equiv 必要属性--> <meta http-equiv="Content-Type" content="text/html; c ...
- 关于类似(i++)+(++i)
这是一个“然并卵”的问题,因为没有人愿意在代码中给自己找这种麻烦,看到书上讲到这个问题忍不住想顺势总结下,就从表达式说起吧. 在js中,同一般的语言一样,表达式分很多种. 对象和数组的初始化表达式:即 ...
- AngularJS简单用法
一.数据绑定 AngularJS的双向数据绑定,意味着你可以在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.即:一方面可以做到model变化驱动了DOM中元素变化,另一 ...
- 瀑布流布局--原生JavaScript
HTML(注意包裹关系,方便js调用) <body> <div id="main"> <div class="box"> & ...