JPA-jpql查询语句的书写
(1)实体类
1 @NamedQueries({ 2 @NamedQuery(name = "getUserByAccount",query ="from User where account=?0"), 3 @NamedQuery(name = "getUsers",query ="from User") 4 }) 5 @Entity 6 @Table(name = "erp_user") 7 public class User { 8 @Id 9 @GeneratedValue(strategy = GenerationType.IDENTITY) 10 private Integer id; 11 private String account; 12 private String password; 13 @ManyToOne(cascade = CascadeType.PERSIST) 14 @JoinColumn(name = "roleid") 15 private Role role; 16 //省略get和set方法 17 }
(2)测试类
1 public class QueryTest { 2 private static EntityManagerFactory emf; 3 private EntityManager em; 4 private EntityTransaction tx; 5 6 @BeforeClass 7 public static void init() { 8 emf = Persistence.createEntityManagerFactory("jpa"); 9 } 10 11 @AfterClass 12 public static void destory() { 13 emf = null; 14 } 15 16 @Before 17 public void setUp() { 18 em = emf.createEntityManager(); 19 tx = em.getTransaction(); 20 tx.begin(); 21 } 22 23 @After 24 public void tearDown() { 25 tx.commit(); 26 em.close(); 27 } 28 29 @Test 30 public void test1() { 31 String jpql = "from User"; 32 TypedQuery<User> query = em.createQuery(jpql, User.class); 33 List<User> users = query.getResultList(); 34 for (User user : users) { 35 System.out.println(user.getAccount()); 36 } 37 } 38 39 @Test 40 public void test2() { 41 String jpql = "select u from User u"; 42 TypedQuery<User> query = em.createQuery(jpql, User.class); 43 List<User> users = query.getResultList(); 44 for (User user : users) { 45 System.out.println(user.getAccount()); 46 } 47 } 48 49 @Test 50 public void test3() { 51 String jpql = "select account from User"; 52 TypedQuery<String> query = em.createQuery(jpql, String.class); 53 List<String> accounts = query.getResultList(); 54 for (String account : accounts) { 55 System.out.println(account); 56 } 57 } 58 59 @Test 60 public void test4() { 61 String jpql = "select account,password from User"; 62 TypedQuery<Object[]> query = em.createQuery(jpql, Object[].class); 63 List<Object[]> objects = query.getResultList(); 64 for (Object[] object : objects) { 65 System.out.println(object[0] + "--" + object[1]); 66 } 67 } 68 69 @Test 70 public void test5() { 71 String jpql = "select u from User u where u.account = 'admin'"; 72 TypedQuery<User> query = em.createQuery(jpql, User.class); 73 List<User> users = query.getResultList(); 74 for (User user : users) { 75 System.out.println(user.getAccount()); 76 } 77 } 78 79 @Test 80 public void test6() { 81 String jpql = "select u from User u where u.account = ?0"; 82 TypedQuery<User> query = em.createQuery(jpql, User.class); 83 query.setParameter(0, "admin"); 84 List<User> users = query.getResultList(); 85 for (User user : users) { 86 System.out.println(user.getAccount()); 87 } 88 } 89 90 @Test 91 public void test7() { 92 String jpql = "select u from User u where u.account = :account"; 93 TypedQuery<User> query = em.createQuery(jpql, User.class); 94 query.setParameter("account", "admin"); 95 List<User> users = query.getResultList(); 96 for (User user : users) { 97 System.out.println(user.getAccount()); 98 } 99 } 100 101 @Test 102 public void test8() { 103 String jpql = "from User"; 104 TypedQuery<User> query = em.createQuery(jpql, User.class); 105 query.setFirstResult(0); 106 query.setMaxResults(3); 107 List<User> users = query.getResultList(); 108 for (User user : users) { 109 System.out.println(user.getAccount()); 110 } 111 } 112 113 @Test 114 public void test9() { 115 String jpql = "select count(*) from User"; 116 TypedQuery<Long> query = em.createQuery(jpql, Long.class); 117 Long num = query.getSingleResult(); 118 System.out.println(num); 119 } 120 @Test 121 public void test10() { 122 TypedQuery<User> query = em.createNamedQuery("getUserByAccount",User.class); 123 query.setParameter(0,"admin"); 124 User user = query.getSingleResult(); 125 System.out.println(user.getAccount()); 126 } 127 @Test 128 public void test11() { 129 TypedQuery<User> query = em.createNamedQuery("getUsers", User.class); 130 List<User> users = query.getResultList(); 131 for (User user : users) { 132 System.out.println(user.getAccount()); 133 } 134 } 135 136 }