ssh框架整合案例(字典表,no-session,hebiernate|模板的api,懶加載,級聯刪除,ajax的遞歸錯誤)

一:字典表javascript

字典信息:在項目中可能會使用到,已經存在的一些信息。
    例如,客戶的級別:普通用戶,vip用戶...
    客戶的來源:網絡營銷,電話營銷...
    客戶所屬行業:電子商務,房地產...
    客戶的性別:男,女
    在保存用戶的時候,這些信息都是已經存在的,不該該讓用戶讓用戶來任意填寫,
    而是經過下拉列表來讓用戶選擇。
    這些已經存在的信息稱之爲字典信息,將字典信息保存在字典表中。

二:表的設計前端

客戶表和級別表,來源表和所屬行業表的關係java

圖片描述

客戶和級別表,行業表,來源表都屬於多對一的關係
爲了簡化開發,能夠將三張字典數據合成一張字典表

字典表中的內容
圖片描述mysql


三:實體之間的設計jquery

customer表中的cust_level,cust_source,cust_industry字段屬於外鍵
對應着字典表basedict中的dict_id主鍵
在多方(客戶實體)中存在一方對象(字典實體)的引用
Customer實體設計
public class Customer implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long cust_id;
    private String cust_name;
    private String cust_phone;
    private String cust_mobile;
    //配置主外鍵關係    referencedColumnName外鍵所指向的主鍵    name:外鍵名稱
    @ManyToOne(targetEntity=BaseDict.class)
    @JoinColumn(name="cust_level",referencedColumnName="dict_id")
    private BaseDict level;    客戶級別
    @ManyToOne(targetEntity=BaseDict.class)
    @JoinColumn(name="cust_source",referencedColumnName="dict_id")
    private BaseDict source;   客戶來源
    @ManyToOne(targetEntity=BaseDict.class)
    @JoinColumn(name="cust_industry",referencedColumnName="dict_id")
    private BaseDict industry; 客戶所屬行業

字典實體(BaseDict)web

@Entity
@Table(name="base_dict")
public class BaseDict implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long dict_id;            字典表id
    private String dict_type_code;   類別(該條記錄是來源,行業仍是級別的標識)
    private String dict_type_name;   類別的名稱(客戶來源,客戶行業,客戶級別)
    private String dict_item_name;   來源,級別,行業中的具體項(vip,房地產,網絡營銷)
    private String dict_item_code;    
    private Integer dict_sort;       排序使用 
    private Character dict_enable;
    private String dict_memo;

知識回顧:Hibernate中查詢的apiajax

①:oid 經過id查詢 get load方法
②hql:在HQL語句中不可能出現於數據庫相關的信息,由於它是面向對象來操做的,
    只會出現實體類中的屬性或對象若是出現了表名或者表中的字段,
    那麼確定是你的表名和類名相同,表中的字段和類中的屬性相同
    查詢的api
        Query query = session.createQuery(hql語句);
        query.list()查詢全部
        query.uniqueResult()返回單一值
    
    基本查詢:from 類名 查詢該實體類映射的表中全部的記錄封裝爲List<Object>
    條件查詢:
        from 類名 where 屬性名 like ?         佔位符
        from 類名 where 屬性名 = ? 
        from 類名 where 屬性名 like :name;    名稱佔位符
        設置查詢條件:
            query.setParameter(0, "%o%");//設置?佔位符的參數值   
            query.setParameter(name, "%o%");//設置命名佔位符的參數值
     分頁查詢
         String sql = "from Customer";
         Query query = session.createQuery(sql);
         query.setFirstResult(0);//至關於設置limit的第一個參數,起始索引
         query.setMaxResults(3);//至關於設置limit的第二個參數,一頁顯示多少條記錄

hql查詢續:
    排序
         String sql = "from Customer order by 屬性名 desc";  
        //desc降序  默認爲升序 
    聚合函數  count|sum|max|min|avg
        select count(cust_id) from Customer
        count(*)也ok
    投影查詢 查詢部分列
        要求實體類必需要有select後面的構造函數
        String hql = "select new Customer(c.cust_id,c.cust_name) from Customer c";

③:qbc查詢
    session.createCriteria(持久化類的字節碼對象)
    分頁:
        setFirstResult(int 開啓的索引)
        setMaxResults(int 每頁顯示的條數)
    排序:
        addOrder(Order.desc|asc(屬性名));
    統計:
        setProjection(Projections.count|sum|min|max|avg(屬性名))
        setProjection(Projections.rowCount())
    條件:
        add(Resitrctions.eq|like|gt|lt(屬性名稱,參數..));

離線查詢對象:
    DetachedCriteria:api和Criteria中徹底相同
    DetachedCriteria dc = DetachedCriteria(持久化類的字節碼對象);
    在web層進行使用,在條件查詢的時候對查詢的條件進行封裝,在調用service層
    方法得時候,只須要傳遞dc對象就ok。
HibernateTemplate(Hibernate模板操做數據庫)
    save(obj)
    update(obj)
    delete(obj)
    get(class,id)
    load(class,id)
    
    findByCriteria(DetachedCriteria dc):qbc查詢
    findByCriteria(DetachedCriteria dc,int firstResult,int maxResults):qbc分頁
    find(String hql,Object... args):hql

需求一:添加客戶到數據庫中spring

添加客戶的頁面:

圖片描述

在添加用戶的時候,要先從數據庫中查詢出客戶級別,信息來源,所屬行業
這些字典信息來讓用戶進行選擇
由兩種方式:
①:同步方式    先跳轉到action,將查詢到的字典內容放置到值棧中,而後再請發到add.jsp
②:異步方式    直接到add.jsp頁面,當頁面加載完成的時候發送ajax請求

先使用第一種方式:sql

//查詢字典數據
    public void getDictValue(){
        經過類別能夠查詢到來源,級別,所屬行業下的全部數據
        levelList = customerService.findBaseDictByTypeCode("006");
        sourceList = customerService.findBaseDictByTypeCode("002");
        industryList = customerService.findBaseDictByTypeCode("001");
    }
字典數據的集合設置爲action的成員屬性,提供get方法就能夠在jsp頁面中獲取

add.jsp頁面數據庫

使用ognl+struts2標籤獲取致函中的數據(獲取客戶來源,所屬行業相似)
    <td>客戶級別 :</td>
    <td>
        <select name="level.dict_id" style="WIDTH: 180px">
            <s:iterator value="levelList" var="basedict">
                <option value="<s:property value="#basedict.dict_id"/>"><s:property value="#basedict.dict_item_name"/></option>
            </s:iterator>
        </select>
    </td>
customer表中的cust_level,cust_source,cust_industry字段屬於外鍵
對應着字典表basedict中的dict_id主鍵
表單中
級別的name屬性爲:level.dict_id
來源的name屬性爲: source.dict_id
會使用屬性封裝的方式封裝到BaseDict level中的dict_id
在保存Customer的時候會將對應的外鍵(cust_level,cust_source,cust_industry)保存

需求2:查詢客戶列表信息(分頁+條件查詢)

![圖片描述

分頁使用的ObjectVlaue(PageBean)部分代碼
mysql分頁    limit ?,?
    後臺須要的條件:起始索引,一頁顯示的記錄數
    前臺須要的數據:當前頁顯示的數據,總頁數,總的記錄數
    前臺須要傳遞的數據:當前頁(沒有傳遞默認爲1)一頁顯示的記錄數(沒有傳遞,給出默認值)
    
    起始索引:(當前頁-1)*一頁顯示的記錄數
    總記錄數:從數據庫中查詢而來
    總頁數:Math.ceil(1.0*總記錄數/總頁數)
    當前頁顯示的數據:數據庫中查詢
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer startIndex = 0;        //起始索引
    private Integer pageSize = 3;        //一頁顯示的記錄數
    private Integer pageNumber = 1;        //當前頁,由前端用戶傳遞,若是用戶沒有傳遞默認顯示第一頁的數據
    private List<T> result;    //封裝查詢出來的某一頁的數據
    private Long totalRecord;        //總記錄數        從數據庫中查詢而來
    //計算而來    總記錄數%一頁顯示的記錄==0?總記錄數/一頁顯示的記錄:總記錄數/一頁顯示的記錄+1
    private Integer totalPage;            //總頁數        
    public Integer getTotalPage() {
        totalPage = (int) Math.ceil(1.0*totalRecord/pageSize);
        return totalPage;
    }
    
    //外界來獲取起始索引   在內部進行計算
    public Integer getStartIndex() {
        startIndex = (pageNumber - 1) * pageSize;
        return startIndex;
    }
    public void setPageSize(Integer pageSize) {
        if(pageSize != null){
            若是前臺沒有傳遞,使用默認值
            this.pageSize = pageSize; 
        }else{
            this.pageSize = 3;
        }
    }
    public void setPageNumber(Integer pageNumber) {
        if(pageNumber != null){
            this.pageNumber = pageNumber; 
        }else{
            若是前臺沒有傳遞,使用默認值
            this.pageNumber = 1;
        }
    }
}

條件查詢:在web層使用DetachedCriteria來封裝查詢的條件
web層action中的代碼
使用DetachedCriteria來封裝查詢的條件,使用pageBean來封裝當前頁和一頁顯示的數據
調用service層方法的時候傳遞DetachedCriteria對象和pageBean對象
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
 /*
    * dc.add(Restrictions(propertyName, value))查詢提交 
    propertyName:是屬性字段 value是條件對應的值
    * 而後再使用對象導航查詢去字典表中查詢數據
*/
 if(customer.getCust_name() != null &&!customer.getCust_name().trim().equals("")){
    dc.add(Restrictions.like("cust_name","%"+customer.getCust_name().trim()+"%"));
 }
 /*
    * 對查詢的條件進行判斷和封裝 不須要對對象進行非空判斷,
    由於使用模型封裝實體的時候,實體必須手動建立
    * 若是下拉列表沒有沒有被選擇,select傳遞的value爲-1
 */
 if(customer.getLevel() != null && customer.getLevel().getDict_id() != -1){
    dc.add(Restrictions.eq("level", customer.getLevel()));
  }
 if(customer.getSource() != null && customer.getSource().getDict_id() != -1){
    dc.add(Restrictions.eq("source", customer.getSource()));
 }
 if(customer.getIndustry() != null && customer.getIndustry().getDict_id() != -1){
    dc.add(Restrictions.eq("industry", customer.getIndustry()));
 }
 //傳遞的參數爲離線查詢對象(封裝條件)和分頁須要的pageBean
 pageBean = customerService.findList(dc,pageBean);

service層的代碼

public PageBean findList(DetachedCriteria dc,PageBean pageBean) {
        /*
         * 須要填充的數據爲當前頁顯示的數據
         * 總的記錄數
         */
        //查詢總的記錄數(須要傳遞dc離線查詢對象,由於不止是分頁,是條件+分頁)
        Long totalRecord = customerDao.searchTotalRecord(dc);
        pageBean.setTotalRecord(totalRecord);
        //查詢當前頁顯示的記錄
        List<Customer> result = customerDao.searchCustomerList(dc,pageBean);
        pageBean.setResult(result);
        return pageBean;
    }

dao層的代碼:
@Override
    public Long searchTotalRecord(DetachedCriteria dc) {
        //設置投影(聚合)
        dc.setProjection(Projections.rowCount());
        /*
         * 查詢語句相似於 select count(*) from Customer 
         * 若是有條件的分頁就是select count(*) from Customer where ......
         */
        List<Long> record = (List<Long>) hibernateTemplate.findByCriteria(dc);
        /*
         * 查詢完成以後須要去除投影,由於查詢完成總記錄數以後
         * 還須要查詢當前頁顯示的數據  使用的是一個離線查詢對象
         */
        dc.setProjection(null);
        return record.get(0);
    }
    //分頁+條件查詢    查詢當前頁顯示的數據
    @Override
    public List<Customer> searchCustomerList(DetachedCriteria dc, PageBean pageBean) {
        List<Customer> customerList = (List<Customer>) hibernateTemplate.findByCriteria(dc, pageBean.getStartIndex(), pageBean.getPageSize()); 
        return customerList;
    }
要注意的就是在進行總記錄數查詢的時候dc.setProjection(Projections.rowCount());
當查詢完成的時候去去除投影,由於接下來查詢當前頁顯示的記錄數使用的也是同一個
離線查詢對象。

查詢條件的回顯

兩種方式:
方式一:使用el表達式進行判斷
    <option value="${basedict.dict_id }" <c:if test='${basedict.dict_id==industry.dict_id}'>selected='selected'</c:if>>${basedict.dict_item_name}</option>
方式二:使用jqery屬性選擇器
使用jquery的屬性選擇器來進行判斷

當下拉列表中option中的值與以前選擇的值相同時,讓匹配的option選中

<script type="text/javascript">
    $(function(){
        //當頁面加載完成的時候使用jquery的屬性選擇器
        $("#level option[value='${level.dict_id}']").prop("selected",true);
        $("#source option[value='${source.dict_id}']").prop("selected",true);
    })
</script>

前臺頁面:
    當點擊下一頁或者索引頁的時候,使用的是超連接。
    可是分頁+條件查詢,查詢的條件在表單中
    解決:當點擊索引頁。上一頁下一頁以後。
        爲超連接提供點擊事件:
        將當前點擊的頁數動態的放置在表單輸入項中
        而後提交表單,這樣會將查詢的條件和當前頁一塊兒提交到後臺頁面
當前頁的表單
    <input id="pageNumberId" type="text" name="pageBean.pageNumber" value="">
    <A href="javascript:toPage('${pageBean.pageNumber+1 }')">後一頁</A>
    function toPage(pageNumber){
            //獲取到提交當前頁的輸入框
            $("#pageNumberId").val(pageNumber);
            //提交表單
            $("#customerForm").submit();
    }

需求三:修改客戶信息:
    先查詢後修改:
    據客戶的id先查詢客戶信息,而後請求轉發到edit.jsp頁面顯示要修改的客戶信息
    private Customer customer = new Customer();
    //使用模型驅動進行表單數據的封裝
    @Override
    public Customer getModel() {
        return customer;
    }
    使用模型驅動封裝要修改的的客戶id
    此時customer中只有客戶的id
    這時候若是還使用customer來接收的話,請求轉發到edit.jsp頁面中
    經過${成員屬性}是獲取不到內容的
    由於customer引用從新指向了一個對象
    
    要想在edit.jsp頁面中獲取到查詢到的內容
     有三種方式:
    一:使用customer來接收
        使用ActionContext.getContext.getValueStack().push(customer);
        向root棧中存放customer指向的新對象    可使用${成員屬性獲取到新內容}
    二:建立一個新的Customer findCustomer對象來接收,爲該customer也提供相應的get方法
        在頁面就可使用${findCustomer.成員屬性}來獲取導內容
    三:使用模型封裝的customer來接收
        在edit.jsp頁面就可使用${model.成員屬性}的方法來獲取到內容

no-session問題:
could not initialize proxy - no Session

緣由:懶加載的問題
使用id獲取要修改的對象時,使用load方法
訪問 service 訪問dao 返回是linkman的代理對象
代理對象返回給web層
頁面獲取數據 代理對象在使用時纔會真正的去查詢 可是session已經關閉

no-session的解決方案:
一、當即查詢
二、延長session的存活時間 在web層將代理對象的數據查詢完畢後在讓session關閉
spring提供了一個Filter ----- OpenSessionInView
<!-- openSessionInView -->
<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
注意一:必須讓該過濾器在Struts2核心過濾器以前執行
由於當Struts核心過濾器執行完成以後,action的方法已經被執行
在操做數據庫的時候session對象尚未建立,仍是會有no-session問題
注意二:OpenSessionInViewFilter    
hibernate5.support.OpenSessionInViewFilter
必須和你導入Hibernate的版本一致,在這裏是Hibernate5版本
不然可能會有異常:
org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

no-session:懶加載問題

原生hibernate(非jpa形式)
類級別的延遲
    load()  直接查詢實體對象時是否延遲  
    配置<class name="全包名" table="表名稱" lazy="true/false">        默認true
    關聯級別的延遲
    關聯類的延遲
        linkman關聯customer
        <many-to-one lazy="false/proxy">        ----默認proxy
        
    關聯集合的延遲
        customer關聯linkman
        <set lazy="true/false/extra">            ---默認是true
jpa形式的hibernate

一對多 查詢一的一方多 多的一方默認延遲加載
    查詢customer 對應的linkman 延遲加載
多對一 查詢多的一方 一的一方默認當即加載    
    查詢linkman  對應的customer的當即加載

刪除客戶(一方)刪除客戶的時候要刪除客戶下對應的聯繫人,配置級聯刪除
cascade=CascadeType.REMOVE
一方放棄維護關係,因此不能直接刪除,須要先查後刪
//在刪除一方的時候先查詢後刪除,由於查詢出來的對象和多表的一方存在關係,能夠級聯刪除
customer = hibernateTemplate.get(Customer.class, customer.getCust_id());
hibernateTemplate.delete(customer);

刪除多方的時候能夠直接刪除,單表操做的時候能夠直接刪除

ajax的遞歸錯誤
There is a cycle in the hierarchy
在json格式轉換的時候出現死循環
表之間存在關係,Customer和LinkMan表是一對多的關係
在打印customer對象的時候,因爲customer中存在LinkMan
會打印LinkMan對象,在打印LinkMan對象中,又存在Customer對象
遞歸調用,致使內存溢出
需求:
    添加聯繫人的時候要選擇所屬客戶
    在add.jsp頁面加載完成的時候,發送ajax請求
    獲取數據庫中的全部客戶信息
<script type="text/javascript">
    //當頁面加載完成的時候,獲取全部的客戶信息,發送ajax異步請求
    $(function(){
        $.ajax({
            url:"${pageContext.request.contextPath}/customer_findCustomerList.action",
            type:"POST",
            success:function(data){
                $(data).each(function(index){
                    $("#customerId").append("<option value='"+this.cust_id+"'>"+this.cust_name+"</option>");
                });
                //數據的回顯必須放置在回調函數中,當查詢到全部的客戶的時候才能進行信息的回顯
                $("#customerId option[value='${customer.cust_id}']").attr("selected",true);
            },
            dataType:"json"
        });    
    })
    </script>
web層的代碼:
//去數據庫中查找全部的客戶信息
    @Action("customer_findCustomerList")
    public void findCustomerList() throws IOException{
        List<Customer> customerList = customerService.findAll();
        //發送的是ajax請求  轉換爲json格式的數據
        JsonConfig jsonConfig = new JsonConfig();
        設置不參與轉換的字段
        jsonConfig.setExcludes(new String[]{"linkMen"});
        String json = JSONArray.fromObject(customerList,jsonConfig).toString();
        //將json格式的數據寫會給瀏覽器,解決中文亂碼問題
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println(json);
    }
相關文章
相關標籤/搜索