JPQL
JPQL语言,即 Java Persistence Query Language 的简称。JPQL 是一种和 SQL 非常类似的中间性和对象化查询语言,它最终会被编译成针对不同底层数据库的 SQL 查询,从而屏蔽不同数据库的差异。
JPQL语言的语句可以是 select 语句、update 语句或delete语句,它们都通过 Query 接口封装执行
1.1. 查询所有
select o from Order o
public void findAll(){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="from MyComputer";
TypedQuery
List
for (MyComputer myComputer : resultList) {
System.out.println(myComputer.getComName());
}
entityManager.close();
}
注意导包:javax.persistence.Query
1.2. 参数查询
命名占位
select o from Orders o where o.id = :myId
query.setParameter(name, value)
注意:参数名前必须冠以冒号(:)
public void findByID(Long id){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="select com from MyComputer com where com.comId=:cid";
TypedQuery
query.setParameter("cid",id);
MyComputer singleResult = query.getSingleResult();
System.out.println(singleResult.getComName());
entityManager.close();
}
索引占位
select o from Order o where o.id = ?1 and o.customer = ?2
其中 ?1 代表第一个参数query.setParameter( 1, 2 );
?2 代表第二个参数。 query.setParameter( 2, "John" );
这里和HQL的方式不一样。必须在问号后面指定数字索引。
代码:
public void findByID(Long id){
EntityManager entityManager = JPAUtils.getEntityManager();
// String jpql ="select com from MyComputer com where com.comId=:cid";
String jpql ="select com from MyComputer com where com.comId=?1";
TypedQuery
// query.setParameter("cid",id);
query.setParameter(1,id);
MyComputer singleResult = query.getSingleResult();
System.out.println(singleResult.getComName());
entityManager.close();
}
1.3. 模糊查询
select c from Customers c where c.phone like '139%’
代码:
public void findBylike(){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="select com from MyComputer com where com.comName like :cn";
TypedQuery
query.setParameter("cn","%te%");
List
for (MyComputer myComputer : resultList) {
System.out.println(myComputer.getComName());
}
entityManager.close();
}
1.4. 投影查询(查询部分字段)
select o.id, o.customerName from Order
返回Object[]
代码:
public void findDepart(){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="select com.comName,com.comDesc from MyComputer com";
TypedQuery
1.5. 更新
update MyUser set username=:username where id=:id
query.executeUpdate();
代码:
public void updateCom(){
EntityManager entityManager = JPAUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String jpql ="update MyComputer set comDesc=:cdesc where comId=:cid";
Query query = entityManager.createQuery(jpql);
query.setParameter("cdesc","最后一次机会超越自己");
query.setParameter("cid",5L);
query.executeUpdate();
transaction.commit();
entityManager.close();
}
1.6. 删除
delete from MyUser where id=:id
代码:
public void delCom(){
EntityManager entityManager = JPAUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String jpql ="delete from MyComputer where comId=:cid";
Query query = entityManager.createQuery(jpql);
query.setParameter("cid",4L);
query.executeUpdate();
transaction.commit();
entityManager.close();
}
1.7. 分页
query.setFirstResult(startPosition);
query.setMaxResults(maxResult);
代码:
public void findAll(){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="select com from MyComputer com";
TypedQuery
query.setFirstResult(0);//起始位置
query.setMaxResults(2);//每页大小
List
for (MyComputer myComputer : resultList) {
System.out.println(myComputer.getComName());
}
entityManager.close();
}
1.8. 函数查询
select count(id) from MyUser
query.getSingleResult()
代码:
public void findCount(){
EntityManager entityManager = JPAUtils.getEntityManager();
String jpql ="select count(comId) from MyComputer ";
TypedQuery
Long singleResult = query.getSingleResult();
System.out.println(singleResult);
entityManager.close();
}
计算总页数。
对比HQL:总结发现,就是使用索引占位的时候,JPQL需要在问号后面指定索引编号,其他基本一致。