本章节将介绍怎样利用 JPQL 实现批量操作。
下面实例将使用 user 表作为操作对象。
(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; }
通过 createQuery() 方法执行 select 语句,查询 user 表中所有数据。代码如下:
private void select() { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); TypedQuery<User> query = em.createQuery("select u from User u", User.class); List<User> userList = query.getResultList(); for (User user : userList) { System.out.println(user); } em.getTransaction().commit(); em.close(); factory.close(); System.out.println("finished."); }
运行上面代码输出 SQL 语句如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 User{id=1, name='用户-0', age=56, salary=7251.132} User{id=2, name='用户-1', age=93, salary=7253.2427} User{id=3, name='用户-2', age=44, salary=5855.36} 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} User{id=8, name='用户-7', age=91, salary=3047.556} User{id=9, name='用户-8', age=79, salary=7199.551} User{id=10, name='用户-9', age=26, salary=6327.472}
通过 createQuery() 方法执行 update 语句,将所有年龄大于80岁的用户的年龄设置为80岁。代码如下:
private void update() { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Query query = em.createQuery("update User u set u.age=80 where u.age > 80"); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); System.out.println("finished."); }
运行上面代码输出 SQL 语句如下:
UPDATE User t0 SET t0.age = ? WHERE (t0.age > ?) [params=?, ?]
通过 createQuery() 方法执行 delete 语句,将所有年龄大于80岁的用户删除。代码如下:
private void delete() { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Query query = em.createQuery("delete from User u where u.age > 80"); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); System.out.println("finished."); }
运行上面代码输出 SQL 语句如下:
DELETE t0 FROM User t0 WHERE (t0.age > ?) [params=?]