在 SQL 语句中,支持通过 where 语句实现各种简单、复杂的条件查询。JPA 中的 JPQL 语句也允许定义各种查询条件,根据条件筛选数据。
(1)配置 persistence.xml,如下:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="openJPA2" transaction-type="RESOURCE_LOCAL"> <!-- JPA提供者 --> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <!-- 声明实体类 --> <class>com.hxstrive.openjpa.entity2.User</class> <!-- 配置JPA数据库属性 --> <properties> <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/openjpa_learn?useSSL=false& serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"/> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="openjpa.ConnectionUserName" value="root"/> <property name="openjpa.ConnectionPassword" value="aaaaaa"/> <property name="openjpa.Log" value="SQL=TRACE"/> <!-- 自动生成表 --> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> <!-- 不使用加载时强化和编译时强化,使用运行时Unenhanced(不能发挥OpenJPA的最大效能,所以也不推荐) --> <property name="openjpa.ClassLoadEnhancement" value="false"/> <property name="openjpa.DynamicEnhancementAgent" value="false"/> <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> </properties> </persistence-unit> </persistence>
(2)用户表实体映射,代码如下:
@Data @Entity @Table public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column private String name; @Column private Integer age; @Column private Float salary; }
在介绍过滤器之前,我们介绍一下执行 JPQL 语句的模版方法。代码如下:
public class JPQLDemo5 { /** 持久化单元名称 */ private static final String PERSISTENCE_NAME = "openJPA2"; private static void query(EntityManager em, String sql) { TypedQuery<User> query = em.createQuery(sql, User.class); List<User> userList = query.getResultList(); for (User user : userList) { System.out.println(user); } } public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); // 大于、小于等于 // query(em, "你的JPQL语句"); em.close(); factory.close(); System.out.println("finished."); } }
检索 age 等于 80 的用户信息,代码如下:
query(em, "select t from User t where t.age=80");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age = ?) [params=?] User{id=2, name='用户-1', age=80, salary=7253.2427} User{id=8, name='用户-7', age=80, salary=3047.556}
检索 age 不等于 80 的用户信息,代码如下:
query(em, "select t from User t where t.age <> 80");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age <> ?) [params=?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=3, name='用户-2', age=44, salary=5855.36} User{id=4, name='用户-3', age=10, salary=4105.1895}
检索 age 大于 20 ,且小于等于 60 的用户信息,代码如下:
query(em, "select t from User t where t.age > 20 and t.age <= 60");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age > ? AND t0.age <= ?) [params=?, ?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=3, name='用户-2', age=44, salary=5855.36} User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=10, name='用户-9', age=26, salary=6327.472}
检索 age 大于 20 ,且小于等于 60 的用户信息,代码如下:
query(em, "select t from User t where t.age between 20 and 60");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age >= ? AND t0.age <= ?) [params=?, ?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=3, name='用户-2', age=44, salary=5855.36} User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=10, name='用户-9', age=26, salary=6327.472}
检索 name 包含 "8" 字符串的用户信息,代码如下:
query(em, "select t from User t where t.name like '%8%'");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.name LIKE ? ESCAPE '\\') [params=?] User{id=9, name='用户-8', age=79, salary=7199.551}
检索 id 等于 1、2 或 3 的用户信息,代码如下:
query(em, "select t from User t where t.id in(1,2,3)");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE ((t0.id = ? OR t0.id = ? OR t0.id = ?) AND t0.id IS NOT NULL) [params=?, ?, ?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=2, name='用户-1', age=80, salary=7253.2427} User{id=3, name='用户-2', age=44, salary=5855.36}
检索 id 不等于 1、2 和 3 的用户信息,代码如下:
query(em, "select t from User t where t.id not in(1,2,3)");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (NOT (t0.id = ? OR t0.id = ? OR t0.id = ?) AND t0.id IS NOT NULL) [params=?, ?, ?] User{id=4, name='用户-3', age=10, salary=4105.1895} User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=6, name='用户-5', age=73, salary=8892.097} User{id=7, name='用户-6', age=77, salary=5494.797}
AND运算符只有当满足所有给出的条件时,才能够成功获取到相应数据。
例如:检索 age 大于 50,且小于 80 的用户数据
query(em, "select t from User t where t.age>50 and t.age<80");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age > ? AND t0.age < ?) [params=?, ?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=6, name='用户-5', age=73, salary=8892.097} User{id=7, name='用户-6', age=77, salary=5494.797} User{id=9, name='用户-8', age=79, salary=7199.551}
OR 运算符使用方法其实和 AND 运算符很像,不同的是,OR 运算符只需要满足多个条件中的任意一个条件,即可成功获取到相应数据。
例如:检索 age 小于 50,或 age 大于80 的用户数据
query(em, "select t from User t where t.age>80 or t.age<50");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age > ? OR t0.age < ?) [params=?, ?] User{id=3, name='用户-2', age=44, salary=5855.36} User{id=4, name='用户-3', age=10, salary=4105.1895} User{id=10, name='用户-9', age=26, salary=6327.472}
下面将演示 and 和 or 操作符的用法,代码如下:
query(em, "select t from User t where t.age>50 and (t.salary > 8000 or t.salary < 3000)");
输出如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 WHERE (t0.age > ? AND (t0.salary > ? OR t0.salary < ?)) [params=?, ?, ?] User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=6, name='用户-5', age=73, salary=8892.097}