七、使用SVN
- 步驟一:創建空文件夾crm,並創建crm倉庫,同時創建目錄規範,並瀏覽目錄規範,操作步驟如下圖所示: 1、
2、
3、
4、
- 步驟二:配置SVN權限,配置順序如下圖所示:
svnserve.conf
passwd
authz(多倉庫)
- 步驟三:在myeclipse 中新建 svn 資源庫路徑並查看,步驟如下: 1、
2、
3、
- 步驟四:將項目分享到svn服務器,右鍵要分享的項目 --> Team --> Share Project… 1、
2、
3、
4、
5、
6、同步資源庫後,由於只有我一個人進行操作,所以選中項目進行提交即可
7、
8、項目提交完成截圖
9、
- 步驟五:查看項目日誌記錄 1、項目右鍵/team/顯示資源歷史記錄
2、會出現一個錯誤
3、 解決辦法:在配置文件中添加一些信息 svnserve.conf
authz
再次單擊 顯示資源歷史記錄,效果如下圖所示:
八、員工的編輯
思路: 1、先查詢 2、用戶在瀏覽器端修改 3、提交表單,進行更新
8.1、標籤的回顯
- 標籤回顯的原則:
- 如果是文本框數據,我們通過name屬性從值棧的棧頂開始獲得數據,如果獲得到數據,則將數據回顯到jsp頁面。
- 如果是checkbox、radio、select,它們本身需要有數據才能回顯,我們通過name屬性從值棧獲得數據,如果獲得到的數據與提供的數據一致,將回顯數據到jsp頁面。
8.1.1、編輯前:通過員工id查詢員工信息
StaffDao.java
/** * 通過員工id查詢員工 * * @param staffId * @return */ public CrmStaff findById(String staffId);
StaffDaoImpl.java
@Override public CrmStaff findById(String staffId) { return this.getHibernateTemplate().get(CrmStaff.class, staffId); }
StaffService.java
/** * 通過員工id查詢員工 * * @param staffId * @return */ public CrmStaff findById(String staffId);
StaffServiceImpl.java
@Override public CrmStaff findById(String staffId) { return this.staffDao.findById(staffId); }
StaffAction.java
/** * 編輯員工信息前的顯示操作 * * @return */ public String editUI() { // 1、通過員工id查詢員工信息 CrmStaff findStaff = this.staffService.findById(staff.getStaffId()); // ActionContext.getContext().put("abc", findStaff); // jsp中取值方式爲:#abc.getXxx 這種方式叫做手動回顯 ActionContext.getContext().getValueStack().push(findStaff); // 自動回顯 // 2、查詢所有部門 List<CrmDepartment> allDepartment = departmentService.findAllDepartment(); // jsp頁面通過“key”來獲得 ActionContext.getContext().getValueStack().set("allDepartment", allDepartment); // 建議使用set(),如果使用push()的話,會比較慢 return "editUI"; }
jsp頁面回顯普通數據:editStaff.jsp
<s:form> <table width="88%" border="0" class="emp_table" style="width:80%;"> <tr> <td>登錄名:</td> <td><s:textfield name="loginName"></s:textfield></td><!-- 普通數據自動回顯 --> <td>密碼:</td> <td><s:password name="loginPwd" showPassword="true"></s:password></td> </tr> <tr> <td>姓名:</td> <td><s:textfield name="staffName"></s:textfield></td> <td>性別:</td> <td> <s:radio list="{'男', '女'}" name="gender"></s:radio> </td> </tr> ...... <tr> <td width="10%">入職時間:</td> <td width="20%"> <s:date name="onDutyDate" format="yyyy-MM-dd" var="myDate"/><!-- 日期需要手動回顯,因爲需要格式化 --> <s:textfield name="onDutyDate" readonly="true" value="%{#myDate}" onfocus="c.showMoreDay=true; c.show(this)"></s:textfield> </td> <td width="8%"></td> <td width="62%"></td> </tr> </table> </s:form>
8.1.2、部門和職務數據回顯
DepartmentDao.java
/** * 查詢所有的部門 * * @return */ public List<CrmDepartment> findAll();
DepartmentDaoImpl.java
@Override public List<CrmDepartment> findAll() { return this.getHibernateTemplate().find("from CrmDepartment"); }
DepartmentService.java
public List<CrmDepartment> findAllDepartment();
StaffDao.java
private DepartmentDao departmentDao; public void setDepartmentDao(DepartmentDao departmentDao) { this.departmentDao = departmentDao; } @Override public List<CrmDepartment> findAllDepartment() { return departmentDao.findAll(); }
spring 的配置
- 每一個模塊單獨使用xml,必須要在 applicationContext.xml 中進行import
applicationContext.xml
<!-- 4.2、部門 --> <import resource="applicationContext-department.xml"/>
applicationContext-department.xml
<!-- 部門配置項 dao、service--> <bean id="departmentDao" class="com.itheima.crm.department.dao.impl.DepartmentDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="departmentService" class="com.itheima.crm.department.service.impl.DepartmentServiceImpl"> <property name="departmentDao" ref="departmentDao"></property> </bean>
StaffAction.java
/** * 編輯員工信息前的顯示操作 * * @return */ public String editUI() { // 1、通過員工id查詢員工信息 CrmStaff findStaff = this.staffService.findById(staff.getStaffId()); // ActionContext.getContext().put("abc", findStaff); // jsp中取值方式爲:#abc.getXxx 這種方式叫做手動回顯 ActionContext.getContext().getValueStack().push(findStaff); // 自動回顯 // 2、查詢所有部門 List<CrmDepartment> allDepartment = departmentService.findAllDepartment(); // jsp頁面通過“key”來獲得 ActionContext.getContext().getValueStack().set("allDepartment", allDepartment); // 建議使用set(),如果使用push()的話,會比較慢 return "editUI"; }
jsp頁面回顯部門和職務數據:editStaff.jsp
- listKey 和 listValue ,獲得當前列表項所使用javabean的某一個屬性的值
- name ,通過name從值棧獲得數據,如果獲得數據和listKey確定的數據一致,會將數據進行回顯
注意:當前員工的職務所屬的部門,此部門下的所有職務。代碼表示:post.department.postSet editStaff.jsp
<tr> <td width="10%">所屬部門:</td> <td width="20%"> <%-- <select name="crmPost.crmDepartment.depId" onchange="changePost(this)"> <option value="">----請--選--擇----</option> <option value="2c9091c14c78e58b014c78e67de10001" selected="selected">java學院</option> <option value="2c9091c14c78e58b014c78e68ded0002">諮詢部</option> </select> --%> <s:select list="allDepartment" name="post.department.depId" headerKey="" headerValue="----請--選--擇----" listKey="depId" listValue="depName" > </s:select> </td> <td width="8%">職務:</td> <td width="62%"> <%-- <select name="crmPost.postId" id="postSelectId"> <option value="">----請--選--擇----</option> <option value="2c9091c14c78e58b014c78e6b34a0003">總監</option> <option value="2c9091c14c78e58b014c78e6d4510004" selected="selected">講師</option> </select> --%> <s:select list="post != null ? post.department.postSet : {}" name="post.postId" headerKey="" headerValue="----請--選--擇----" listKey="postId" listValue="postName" > </s:select> </td> </tr>
8.2、ajax二級聯動
8.2.1、思路分析
8.2.2、查詢職務
PostDao.java
/** * 查詢指定部門下的所有職務 * * @return */ public List<CrmPost> findAll(CrmDepartment department);
PostDaoImpl.java
@Override public List<CrmPost> findAll(CrmDepartment department) { return this.getHibernateTemplate().find("from CrmPost where department=?", department); // 面向對象的角度來寫 }
PostService.java
public List<CrmPost> findAllWithDepartment(CrmDepartment department);
PostServiceImpl.java
private PostDao postDao; public void setPostDao(PostDao postDao) { this.postDao = postDao; } @Override public List<CrmPost> findAllWithDepartment(CrmDepartment department) { return postDao.findAll(department); }
spring 的配置 applicationContext.xml
<!-- 4.3、職務 --> <import resource="applicationContext-post.xml"/>
applicationContext-post.xml
<!-- 職務配置項 dao、service--> <bean id="postDao" class="com.itheima.crm.post.dao.impl.PostDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="postService" class="com.itheima.crm.post.service.impl.PostServiceImpl"> <property name="postDao" ref="postDao"></property> </bean>
PostAction.java
package com.itheima.crm.post.web.action; import java.io.IOException; import java.util.List; import org.apache.struts2.ServletActionContext; import com.itheima.crm.post.domain.CrmPost; import com.itheima.crm.post.service.PostService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; public class PostAction extends ActionSupport implements ModelDriven<CrmPost> { // **************************************************************** // 公共代碼 // 封裝數據 private CrmPost post = new CrmPost(); @Override public CrmPost getModel() { return post; } // 默認按照名稱進行注入 // 職務的service private PostService postService; public void setPostService(PostService postService) { this.postService = postService; } // **************************************************************** // 業務代碼(功能代碼) /** * ajax 通過部門,查詢指定部門下的所有職務 * * @return * @throws IOException */ public String findAllWithDepartment() throws IOException { // 1、通過部門,查詢指定部門下的所有職務 List<CrmPost> allPost = postService.findAllWithDepartment(post.getDepartment()); // 2、將查詢到的結果 javabean 對象轉換成 json 數據 // 2.1、排除不需要的數據(即需要排除關聯對象) JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{"department", "staffSet"}); // 2.2、轉換 String jsonData = JSONArray.fromObject(allPost, jsonConfig).toString(); // 3、 將json 數據 發送給瀏覽器(就相當於發送給ajax引擎了) // 3.1、解決響應的中文亂碼問題 ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); // 這樣寫不標準,因爲你發送的是json數據,但是你告訴瀏覽器是html,學習JQuery的時候再說 // 3.2、發送 ServletActionContext.getResponse().getWriter().print(jsonData); return "none"; } }
struts 的配置 struts.xml
<!-- 3.2、職務--> <include file="struts/struts-post.xml"></include>
struts-post.xml
<!-- 職務的配置 --> <package name="pos" namespace="/" extends="common"> <action name="postAction_*" class="com.itheima.crm.post.web.action.PostAction" method="{1}"> </action> </package>
8.2.3、json數據的生成
- 將採用json-lib 工具生成
- 需要導入jar包:(注意:重複的jar包就不要導入了,留下最新版本的jar包)
- json api的使用: 如果處理 JavaBean 或 Map 數據,使用:JSONObject.fromObject(…).toString(); 如果處理 List 或 Array 數據,使用:JSONArray.fromObject(…).toString();
8.2.4、發送 ajax 請求
- /day36_06_Spring_crm/WebRoot/WEB-INF/pages/staff/editStaff.jsp
- 步驟:
- 獲得當前選中部門
- 發送ajax查詢職務(獲得引擎、設置回調、創建連接、發送請求)
- 獲得數據後,將數據添加到職務的select標籤中
editStaff.jsp
...... <script type="text/javascript"> function showPost(obj) { // 1、先獲得所選的部門 var depId = obj.value; // 2、發送ajax,通過部門查詢職務 // 2.1、獲得ajax引擎,不用記,找着位置就行:W3CSchool全套離線手冊_最新版.chm/XML/XML DOM/DOM XMLHttpRequest/瀏覽器支持/XMLHttpRequest 對象/實例 var xmlhttp = null; if (window.XMLHttpRequest) { // code for all new browsers xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE5 and IE6 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } // 2.2、設置回調函數 xmlhttp.onreadystatechange = function() { // 判斷請求完成和服務器正常響應 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 3、獲得數據並顯示,因爲是手動ajax,所以獲得的是json數據的字符串 var textData = xmlhttp.responseText; // 3.1、將字符串手動的轉換成json對象,是數組 var jsonData = eval("(" + textData + ")"); // 獲得select標籤對象 var postSelectElement = document.getElementById("postSelectId"); postSelectElement.innerHTML = "<option value=''>----請--選--擇----</option>"; // 3.2、遍歷數組 for (var i = 0 ; i < jsonData.length; i++) { var postObj = jsonData[i]; // 獲得職務的id var postId = postObj.postId; // 獲得職務的名稱 var postName = postObj.postName; // 3.3、顯示數據在select標籤中,先給select標籤起一個id postSelectElement.innerHTML += "<option value='" + postId + "' >" + postName + "</option>"; } } } // 2.3、創建連接 var url = "${pageContext.request.contextPath}/postAction_findAllWithDepartment?department.depId=" + depId; xmlhttp.open("GET", url); // 2.4、發送請求 xmlhttp.send(null); // 調試用 // alert("depId"); // alert("O(∩_∩)O哈哈~"); } </script> ......
8.3、更新(保存)功能
8.3.1、完善表單
填寫表單提交位置,添加隱藏字段
<s:form namespace="/" action="staffAction_edit"> <%-- 隱藏域,用來存放員工的id --%> <s:hidden name="staffId" value="%{staffId}"></s:hidden>
添加javascript onclick事件,提交表單
<!-- 提交表單/保存按鈕 --> <a href="javascript:void(0)" onclick="document.forms[0].submit()"> <img src="${pageContext.request.contextPath}/images/button/save.gif"/> </a>
8.3.2、修改action
StaffAction.java
/** * 更新(保存)編輯後的員工信息 * * @return */ public String edit() { this.staffService.updateStaff(staff); return "edit"; }
8.3.3、編寫service
- 採用 hibernate的 快照 和 一級緩存,特別注意:一級緩存如果被修改了,與快照不一致,在默認情況下,當提交時,會自動執行update語句,所以
dao層就沒有相應的update代碼
了。
StaffServiceImpl.java
@Override public void updateStaff(CrmStaff staff) { // 密碼怎麼辦? /* 方案一:判斷密碼是否是32位長度,(我們只考慮簡單情況) * 如果是,說明密碼沒有修改,是之前的密碼,是加密後的密碼 * 如果不是,說明密碼已經修改,是之後的密碼,需要進行加密 */ /* * 方案二:我們不更新密碼, * dao層執行的是 update(staff) 即默認把一個對象的所有數據更新 * CrmStaff.hbm.xml 文件中我們需要配置密碼不參加更新,即 <property name="loginPwd" update="false"></property> */ /* * 方案三:先通過員工id查詢,比較密碼是否一致, * 如果密碼不一致,則需要對密碼進行MD5加密,然後將除OID之外的數據,全部進行手動設置 * 原因:一級緩存被修改了,與快照不一致,默認情況下,當提交時,會自動執行update語句,所以dao層就沒有相應的update代碼了 */ CrmStaff findStaff = staffDao.findById(staff.getStaffId()); if (! findStaff.getLoginPwd().equals(staff.getLoginPwd())) { // 不一致 findStaff.setLoginPwd(MyStringUtils.getMD5Value(staff.getLoginPwd())); } findStaff.setLoginName(staff.getLoginName()); // 登錄名 findStaff.setStaffName(staff.getStaffName()); // 員工姓名 findStaff.setGender(staff.getGender()); // 性別 findStaff.setOnDutyDate(staff.getOnDutyDate()); // 入職時間 findStaff.setPost(staff.getPost()); // 職務 /* * 方案四:自己編寫update語句 */ }
8.3.4、struts 的配置
struts-staff.xml
<!-- 6、編輯員工信息成功,動作重定向至查詢所有,方式一:簡便寫法 <result name="edit" type="redirectAction">staffAction_findAll</result> --> <!-- 6、編輯員工信息成功,動作重定向至查詢所有,方式二:完整寫法,可以注入詳細配置 --> <result name="edit" type="redirectAction"> <param name="namespace">/</param> <param name="actionName">staffAction_findAll</param> </result>
九、課程類別
9.1、查詢所有課程類別
1、dao + dao.impl
CourseTypeDao.java
package com.itheima.crm.coursetype.dao; import java.util.List; import com.itheima.crm.coursetype.domain.CrmCourseType; public interface CourseTypeDao { /** * 查詢所有的課程類別 * * @return */ public List<CrmCourseType> findAll(); }
CourseTypeDaoImpl.java
package com.itheima.crm.coursetype.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.itheima.crm.coursetype.dao.CourseTypeDao; import com.itheima.crm.coursetype.domain.CrmCourseType; @SuppressWarnings("unchecked") public class CourseTypeDaoImpl extends HibernateDaoSupport implements CourseTypeDao { @Override public List<CrmCourseType> findAll() { return this.getHibernateTemplate().find("from CrmCourseType"); } }
2、service + service.impl
CourseTypeService.java
package com.itheima.crm.coursetype.service; import java.util.List; import com.itheima.crm.coursetype.domain.CrmCourseType; public interface CourseTypeService { /** * 查詢所有的課程類別 * * @return */ public List<CrmCourseType> findAllCourseType(); }
CourseTypeServiceImpl.java
package com.itheima.crm.coursetype.service.impl; import java.util.List; import com.itheima.crm.coursetype.dao.CourseTypeDao; import com.itheima.crm.coursetype.domain.CrmCourseType; import com.itheima.crm.coursetype.service.CourseTypeService; public class CourseTypeServiceImpl implements CourseTypeService { private CourseTypeDao courseTypeDao; public void setCourseTypeDao(CourseTypeDao courseTypeDao) { this.courseTypeDao = courseTypeDao; } @Override public List<CrmCourseType> findAllCourseType() { return this.courseTypeDao.findAll(); } }
3、spring
applicationContext.xml
<!-- 4.4、課程類別 --> <import resource="applicationContext-courseType.xml"/>
applicationContext-courseType.xml
<!-- 課程類別配置項 dao、service--> <bean id="courseTypeDao" class="com.itheima.crm.coursetype.dao.impl.CourseTypeDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="courseTypeService" class="com.itheima.crm.coursetype.service.impl.CourseTypeServiceImpl"> <property name="courseTypeDao" ref="courseTypeDao"></property> </bean>
4、action class
CourseTypeAction.java
package com.itheima.crm.coursetype.web.action; import java.util.List; import com.itheima.crm.coursetype.domain.CrmCourseType; import com.itheima.crm.coursetype.service.CourseTypeService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CourseTypeAction extends ActionSupport implements ModelDriven<CrmCourseType> { // **************************************************************** // 公共代碼 // 封裝數據 private CrmCourseType courseType = new CrmCourseType(); @Override public CrmCourseType getModel() { return courseType; } // 默認按照名稱進行注入 // 課程的service private CourseTypeService courseTypeService; public void setCourseTypeService(CourseTypeService courseTypeService) { this.courseTypeService = courseTypeService; } // **************************************************************** // 業務代碼(功能代碼) /** * 查詢所有課程類別 * * @return */ public String findAll() { // 1、簡單查詢:查詢所有課程類別 List<CrmCourseType> allCourseType = this.courseTypeService.findAllCourseType(); // 2、將查詢的結果放入值棧中,本次放入context(大map中),使用put(key, value) 方法,jsp頁面使用 “#key” 方式獲得 ActionContext.getContext().put("allCourseType", allCourseType); return "findAll"; } }
5、struts
struts.xml
<!-- 3.3、課程類別--> <include file="struts/struts-courseType.xml"></include>
struts-courseType.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 課程類別的配置 --> <package name="cour" namespace="/" extends="common"> <action name="courseTypeAction_*" class="com.itheima.crm.coursetype.web.action.CourseTypeAction" method="{1}"> <!-- 1、查詢所有課程類別 --> <result name="findAll">/WEB-INF/pages/coursetype/listCourse.jsp</result> </action> </package> </struts>
6、jsp
/day36_06_Spring_crm/WebRoot/WEB-INF/pages/coursetype/listCourse.jsp
...... <table width="97%" border="1"> <tr class="henglan" style="font-weight:bold;"> <td width="14%" align="center">名稱</td> <td width="33%" align="center">簡介</td> <td width="13%" align="center">總學時</td> <td width="18%" align="center">收費標準</td> <td width="11%" align="center">編輯</td> </tr> <%--數據展示,單行:tabtd1;雙行:tabtd2 --%> <s:iterator value="#allCourseType" status="vs" var="courseType"> <tr class="<s:property value="#vs.even ? 'tabtd2' : 'tabtd1'"/>"><%--設置奇偶行不同的樣式 --%> <td align="center"><s:property value="#courseType.courseName"/></td> <td align="center"><s:property value="#courseType.remark"/></td> <td align="center"><s:property value="#courseType.total"/></td> <td align="center"><s:property value="#courseType.courseCost"/></td> <td width="11%" align="center"> <a href="${pageContext.request.contextPath}/pages/coursetype/addOrEditCourse.jsp"> <img src="${pageContext.request.contextPath}/images/button/modify.gif" class="img"/> </a> </td> </tr> </s:iterator> </table> ......
9.2、課程類別的條件查詢
9.2.1、分析
9.2.2、jsp表單
listCourse.jsp
...... <%--條件查詢 start --%> <s:form namespace="/" action="courseTypeAction_findAll"> <table width="88%" border="0" class="emp_table" style="width:80%;"> <tr> <td width="10%">課程類別:</td> <td> <!-- <input type="text" name="courseName" size="30" value="" /> --> <s:textfield name="courseName" size="30"></s:textfield> </td> </tr> <tr> <td >課程簡介:</td> <td ><s:textfield name="remark" size="30"></s:textfield></td> </tr> <tr> <td >總學時:</td> <td > <s:textfield name="totalStart" size="12"></s:textfield> 至 <s:textfield name="totalEnd" size="12"></s:textfield> </td> </tr> <tr> <td>課程費用:</td> <td > <s:textfield name="courseCostStart" size="12"></s:textfield> 至 <s:textfield name="courseCostEnd" size="12"></s:textfield> </td> </tr> </table> </s:form> <%--條件查詢 end --%> ......
9.2.3、修改PO類:CrmCourseType.java
CrmCourseType.java
// 修改javabean // 查詢條件的字段,注意:一般查詢條件都是字符串 // 1、總學時 private String totalStart; private String totalEnd; // 2、課程費用 private String courseCostStart; private String courseCostEnd;
9.2.4、action類傳入參數
CourseTypeAction.java
/** * 查詢所有課程類別 * * @return */ public String findAll() { /* // 1、簡單查詢:查詢所有課程類別,沒有參數 List<CrmCourseType> allCourseType = this.courseTypeService.findAllCourseType(); // 2、將查詢的結果放入值棧中,本次放入context(大map中),使用put(key, value) 方法,jsp頁面使用 “#key” 方式獲得 ActionContext.getContext().put("allCourseType", allCourseType); */ // 2、條件查詢:(包括簡單查詢沒有條件的查詢),有參數 List<CrmCourseType> allCourseType = this.courseTypeService.findAllCourseType(courseType); ActionContext.getContext().put("allCourseType", allCourseType); return "findAll"; }
9.2.5、service層拼湊查詢條件
- 使用StringBuilder 拼湊字符串,所有的查詢條件格式爲:“ and 屬性 運算符 ? ” 把查詢條件使用 toString 轉成字符串
- 使用List 拼湊實際參數,使用的是List 的特性:重複、有序 把實際參數使用 toArray 轉成數組
- commons-lang3 提供了一個 StringUtils 工具類
@Override public List<CrmCourseType> findAllCourseType() { return courseTypeDao.findAll(); } @Override public List<CrmCourseType> findAllCourseType(CrmCourseType courseType) { // 1.1、使用StringBuilder 接收:拼湊查詢條件 StringBuilder builder = new StringBuilder(); // 1.2、使用List 接收:拼湊實際參數,需要可以重複,有順序 => 選擇 List集合 List<Object> paramsList = new ArrayList<Object>(); // 2、過濾條件 // 2.1、過濾課程類別,不需要 null 或者 "" 或者 " ",使用工具類 if (StringUtils.isNotBlank(courseType.getCourseName())) { builder.append(" and courseName like ?"); paramsList.add("%" + courseType.getCourseName() + "%"); } // 2.2、過濾課程簡介,不需要 null 或者 "" 或者 " ",使用工具類 if (StringUtils.isNotBlank(courseType.getRemark())) { builder.append(" and remark like ?"); paramsList.add("%" + courseType.getRemark() + "%"); } // 2.3、過濾總學時 if (StringUtils.isNotBlank(courseType.getTotalStart())) { builder.append(" and total >= ?"); paramsList.add(Integer.parseInt(courseType.getTotalStart())); } if (StringUtils.isNotBlank(courseType.getTotalEnd())) { builder.append(" and total <= ?"); paramsList.add(Integer.parseInt(courseType.getTotalEnd())); } // 2.3、過濾總費用 if (StringUtils.isNotBlank(courseType.getCourseCostStart())) { builder.append(" and courseCost >= ?"); paramsList.add(Double.parseDouble(courseType.getCourseCostStart())); } if (StringUtils.isNotBlank(courseType.getCourseCostEnd())) { builder.append(" and courseCost <= ?"); paramsList.add(Double.parseDouble(courseType.getCourseCostEnd())); } // 3、使用數據 // 1、條件,格式:“and ...? and ...?” String condition = builder.toString(); // 2、實際參數 Object[] params = paramsList.toArray(); return this.courseTypeDao.findAll(condition, params); }
9.2.6、dao層查詢
- service層拼湊查詢條件,在dao層直接使用,通過 where 1=1 產生一個恆定條件
CourseTypeDaoImpl.java
@Override public List<CrmCourseType> findAll(String condition, Object[] params) { String hql = "from CrmCourseType where 1=1 " + condition; return this.getHibernateTemplate().find(hql, params); }
9.3、課程類別的添加或編輯
思路: 1.dao 層,hibernate 提供 saveOrUpdate() 方法 和 get() 方法 1.1 通過課程類別id查詢詳情,hibernate 提供 get(Class, String) 方法 1.2 代理主鍵 uuid 如果沒有OID,即uuid值爲null,則hibernate底層執行save(),即insert語句 如果有OID,即uuid有值,則hibernate底層執行update(),即update語句 2.service 層,需要事務管理器(add、update、delete、find) findById(String courseTypeId); addOrEditCourseType(CrmCourseType courseType); 3.action 類 addOrEditUI()方法,數據回顯在jsp上 如果是添加,在jsp頁面上顯示爲空 如果是更新,需要通過課程類別id查詢詳情,findById(XxxId) 將數據回顯在jsp頁面上 addOrEdit()方法, 直接保存或更新,注意:如果是更新,需要傳遞課程類別id值 4.jsp 頁面 添加,沒有課程類別id,直接顯示jsp頁面,沒有數據 編輯,有課程類別id,傳遞課程類別id值,通過id查詢到數據,將數據顯示在jsp頁面
9.3.1、dao 層
- 編輯課程類別時,需要通過課程類別id查詢課程類別詳情
- 保存(添加)或更新(編輯)課程類別,則直接執行 saveOrUpdate()
CourseTypeDao.java
/** * 通過課程類別id查詢課程類別詳情 * * @param courseType * @return */ public CrmCourseType findById(String courseTypeId); /** * 保存(添加)或更新(編輯)課程類別 * * @param courseType */ public void saveOrUpdate(CrmCourseType courseType);
CourseTypeDaoImpl.java
@Override public CrmCourseType findById(String courseTypeId) { return this.getHibernateTemplate().get(CrmCourseType.class, courseTypeId); } @Override public void saveOrUpdate(CrmCourseType courseType) { this.getHibernateTemplate().saveOrUpdate(courseType); }
9.3.2、service 層
CourseTypeService.java
/** * 通過課程類別id查詢課程類別詳情 * * @param courseTypeId * @return */ public CrmCourseType findById(String courseTypeId); /** * 添加或編輯課程類別 * * @param courseType */ public void addOrEditCourseType(CrmCourseType courseType);
CourseTypeServiceImpl.java
@Override public CrmCourseType findById(String courseTypeId) { return this.courseTypeDao.findById(courseTypeId); } @Override public void addOrEditCourseType(CrmCourseType courseType) { this.courseTypeDao.saveOrUpdate(courseType); }
9.3.3、action
- 添加課程類別時,需要顯示jsp,不需要通過課程類別id查詢課程類別詳情
- 更新課程類別時,需要顯示jsp,需要通過課程類別id查詢課程類別詳情,我們使用id進行區分。
CourseTypeAction.java
/** * 添加或編輯課程類別,顯示jsp頁面 * * @return */ public String addOrEditUI() { // 如果課程類別id有值,說明是編輯課程類別,那麼編輯需要查詢課程類別詳情 if (StringUtils.isNotBlank(this.courseType.getCourseTypeId())) { // 根據課程類別id,查詢課程類別詳情 CrmCourseType findCourseType = this.courseTypeService.findById(this.courseType.getCourseTypeId()); // 將查詢到的課程類別詳情壓入棧頂,方便jsp標籤自動回顯數據 ActionContext.getContext().getValueStack().push(findCourseType); } // 否則就是添加課程類別,回顯空的jsp頁面 return "addOrEditUI"; } /** * 添加或編輯課程類別功能 * * @return */ public String addOrEdit() { this.courseTypeService.addOrEditCourseType(courseType); return "addOrEdit"; }
9.3.4、jsp頁面
- 使用struts標籤進行數據的自動回顯
- 提供隱藏字段進行更新,添加時不需要,加入判斷條件
addOrEditCourse.jsp
...... <td width="52%"align="right"> <%-- 保存按鈕 --%> <a href="javascript:void(0)" onclick="javascript:document.forms[0].submit();"> <img src="${pageContext.request.contextPath}/images/button/save.gif" /> </a> <%-- 退回按鈕 <a href="#"> <img src="${pageContext.request.contextPath}/images/button/tuihui.gif"/> </a> --%> <s:a namespace="/" action="courseTypeAction_findAll"> <img src="${pageContext.request.contextPath}/images/button/tuihui.gif"/> </s:a> </td> ...... <%-- 表單 --%> <s:form namespace="/" action="courseTypeAction_addOrEdit"> <%-- 添加隱藏域,如果課程類別id有值,說明是更新操作,則提交表單的時候,我們就把課程類別id 隱藏着傳過去 --%> <s:if test="courseTypeId != null"> <s:hidden name="courseTypeId" value="%{courseTypeId}"></s:hidden> </s:if> <%-- 如果課程類別id值爲空字符串,說明保存操作 --%> <table width="88%" border="0" class="emp_table" style="width:80%;"> <tr> <td width="10%">課程類別:</td> <td width="20%"><s:textfield name="courseName"></s:textfield></td> <td width="8%">總學時:</td> <td width="62%"><s:textfield name="total"></s:textfield></td> </tr> <tr> <td>課程費用:</td> <td><s:textfield name="courseCost"></s:textfield></td> <td></td> <td></td> </tr> <tr> <td>課程簡介:</td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="4"> <s:textarea name="remark" cols="60" rows="10"></s:textarea> </td> </tr> </table> </s:form> ......