JPA樂觀鎖&悲觀鎖

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
public void testQuery13()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o.name,d.name from Employee o join o.department d";
Query query = entityManager.createQuery(spql);
List<Object[]> resultList = query.getResultList();
for (Object[] o : resultList) {
System.out.println(Arrays.toString(o));
}
}
//2.5.2.查詢出市場部員工信息及電話
@Test
public void testQuery14()throws Exception{
EntityManager entityManager = Util.getentityManager();
String spql = "select o.employee.name,o.number from Phone o ";
Query query = entityManager.createQuery(spql);
List<Object[]> resultList = query.getResultList();
for (Object[] o : resultList) {
System.out.println(Arrays.toString(o));
}
}
//2.6.1.查詢出各個部門員工的平均工資和每一個部門的最高工資【使用匯集函數】
@Test
public void testQuery15()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select avg (o.salary),max(o.salary)from Employee o group by o.department";
Query query = entityManager.createQuery(sqpl);
List <Object[]>resultList = query.getResultList();
for (Object[] s : resultList) {
System.out.println(Arrays.toString(s));
}
//[工業製造ERP, 2]
//[服裝ERP, 3]
//[藍源EDP, 0]
//[藍源OA, 2]
//[藍源SAAS平臺, 0]
//[裝飾ERP, 3]
//[通用CRM, 4]

}
//2.6.2.查詢出各個項目參與人數報表
@Test
public void testQuery16()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select o.name,o.employees.size from Project o group by o.name";
/* String sqpl = "select o.name,o.employees.size from Project o where o.employees.size>0";*/
Query query = entityManager.createQuery(sqpl);
List <Object[]>resultList = query.getResultList();
for (Object[] s : resultList) {
System.out.println(Arrays.toString(s));
}
}
//2.7.查詢出大於平均工資的員工信息
@Test
public void testQuery17()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select o from Employee o where salary>(select avg(o.salary) from Employee o) ";
Query query = entityManager.createQuery(sqpl);
List <Object>resultList = query.getResultList();
for (Object s : resultList) {
System.out.println(s);
}

}
//分頁查詢
@Test
public void testQuery18()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select o from Employee o";
Query query = entityManager.createQuery(sqpl);
query.setFirstResult(1).setMaxResults(5);
List<Employee> resultList = query.getResultList();
for (Employee employee : resultList) {
System.out.println(employee);
}
}
//3.3.獲取記錄總數,返回類型是Long
@Test
public void testQuery19()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select count(o) from Employee o";
Query query = entityManager.createQuery(sqpl);
Long result = (Long) query.getSingleResult();
System.out.println(result);

}
//原生sql
@Test
public void testQuery20()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select * from employee";
Query nativeQuery = entityManager.createNativeQuery(sqpl);
List <Object[]>resultList = nativeQuery.getResultList();
for (Object[] o : resultList) {
System.out.println(Arrays.toString(o));
}
}
@Test
//告訴createNativeQuery把查詢出來的字段轉換成Employee對象
public void testQuery21()throws Exception{
EntityManager entityManager = Util.getentityManager();
String sqpl = "select * from employee";
Query nativeQuery = entityManager.createNativeQuery(sqpl,Employee.class);
List <Employee>resultList = nativeQuery.getResultList();
for (Employee o : resultList) {
System.out.println(o);
}
}

 distinct去重size操做集合spa

jion  :鏈接
內鏈接  外鏈接    左外鏈接   右外鏈接

笛卡爾積:所謂笛卡爾積,通俗點說就是指包含兩個集合中任意取出兩個元素構成的組合的集合.
對象

事務併發:blog

    什麼是事務:事務是程序中一系列嚴密的操做,全部操做執行必須成功完成,不然在每一個操做所作的更改將會被撤銷,這也是事務的原子性(要麼成功,要麼失敗)

      事務的特性:原子性(Atomicity) :事務單元是最小的單元 不能分割

           一致性(Consistency):事務執行的結果必須是使數據庫數據從一個一致性狀態變到另一種一致性狀態。當事務執行成功後就說數據庫處於一致性狀態
           隔離性(Isolation):一個事務的執行過程當中不能影響到其餘事務的執行,即一個事務內部的操做及使用的數據對其餘事務是隔離的,併發執行各個事務之間無不干擾。   

           持續性(Durability):即一個事務執一旦提交,它對數據庫數據的改變是永久性的。以後的其它操做不該該對其執行結果有任何影響。

併發:一般爲了得到更好的運行性能,各類數據庫都容許多個事務同時運行,這就是事務併發

 

 事務併發帶來的問題:

 

        第一類丟失更新 :  秒殺場景會出現問題
        第二類數據更新:  多個事務同時讀取相同數據,並完成各自的事務提交,致使最後一個事務提交會覆蓋前面全部事務對數據的改變
        髒讀:一個更改的數據未提交 一我的查詢更改事後並未提交的數據 頭一我的 放棄或者撤銷提交的事務  可是第二我的查詢到的和真實的不一致
        虛度:A查詢獲得1條數據 B這時候添加了一條數據  A再次查詢就變成了2條
        不可重複讀:在一個事務中先後兩次讀取的結果並不致,致使了不可重複讀。

 

悲觀鎖:JPQL語句加了一把鎖  同事只能一我的進行操做  若是爲放棄操做權 那麼後面想要操做的人就得排隊  效率低 可是很安全

樂觀鎖:給數據庫加一個版本 每一操做數據就更新版本 就不會出現事務併發的狀況

相關文章
相關標籤/搜索