Qpql書寫規則sql
(1)在jpql裏面不能剛出現*和表名 只能出現對象別名或者 類名 關鍵字和sql裏面同樣
(2).類名和屬性名要區分大小寫數據庫
(3)簡單的jpql語句 String jpql = "select o from 類名 o";安全
一些簡單的數據查詢併發
定義實體類函數
創建關聯性能
測試代碼測試
//2.2.1.查詢全部員工【查詢實體類型】
@Test
public void testQuery1()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o";
Query query = entityManager.createQuery(spql);
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.2.查詢全部員工的姓名和所屬部門名稱【查詢特定屬性】
@Test
public void testQuery2()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o.name,o.department.name from Employee o";
Query query = entityManager.createQuery(spql);
List<Object[]> resultList = query.getResultList();
for (Object[] o : resultList) {
System.out.println(Arrays.toString(o));
}
}
//2.2.3.查詢出全部在成都和廣州工做的員工【查詢結果過濾】
@Test
public void testQuery3()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o where o.department.city=? or o.department.city=?";
Query query = entityManager.createQuery(spql);
query.setParameter(1, "成都");
query.setParameter(2,"廣州" );
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.4.查詢出全部員工信息,按照月薪排序【查詢排序】
//oder by 須要排序的內容 排序的方式DESC asc
@Test
public void testQuery4()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o order by o.salary DESC ";
Query query = entityManager.createQuery(spql);
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.5.查詢出全部員工信息,按照部門編號排序【使用關聯對象屬性排序】
@Test
public void testQuery5()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o order by o.department.sn asc ";
Query query = entityManager.createQuery(spql);
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.6.查詢出在恩寧路和八寶街上班的員工信息【使用IN】
@Test
public void testQuery6 ()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o where o.department.street in(?,?)";
Query query = entityManager.createQuery(spql);
query.setParameter(1, "恩寧路" ).setParameter(2, "八寶街" );
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.7.查詢出工資在5000-6000的員工【使用BETWEEN..AND..】
@Test
public void testQuery7()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o where o.salary between 5000 and 6000";
Query query = entityManager.createQuery(spql);
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
//2.2.8.查詢出姓名包含er或者en的員工【使用LIKE】
@Test
public void testQuery8()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Employee o where o.name like ? or o.name like ?";
Query query = entityManager.createQuery(spql);
query.setParameter(1, "%er%" ).setParameter(2, "%en%" );
List<Employee> resultList = query.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}
// 查詢出有員工的部門【size】
@Test
public void testQuery9()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select o.name from Department o where o.employees.size>0";
Query query = entityManager.createQuery(spql);
List<String> resultList = query.getResultList();
for (String o : resultList) {
System.out.println(o);
}
}
//2.3.1.查詢出有員工的部門【distinct】
@Test
public void testQuery10()throws Exception {
EntityManager entityManager = Util.getentityManager();
String spql = "select distinct o.department.name from Employee o";
Query query = entityManager.createQuery(spql);
List<String> resultList = query.getResultList();
for (String o : resultList) {
System.out.println(o);
}
}
//2.4.2.查詢出部門信息,按照部門的員工人數排序
@Test
public void testQuery11()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o from Department o order by o.employees.size asc ";
Query query = entityManager.createQuery(spql);
List<Department> resultList = query.getResultList();
for (Department o : resultList) {
System.out.println(o);
}
}
//2.4.3.查詢出沒有員工參與的項目【對集合使用size】
@Test
public void testQuery12()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o.name from Project o where o.employees.size=0";
Query query = entityManager.createQuery(spql);
List<String> resultList = query.getResultList();
for (String o : resultList) {
System.out.println(o);
}
}
//2.5.1.查詢出全部員工及部門名稱【JOIN/LEFT JOIN】
@Test