-
hibernate 5.4.27 導(dǎo)入包更換:org.hibernate.boot.registry.StandardServiceRegistryBuilder; ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config .getProperties()).build();查看全部
-
Hibernate 實體類關(guān)系映射 配置文件
查看全部 -
Struts2與Hibernate整合:???? 1.?創(chuàng)建struts2和hibernate用戶類庫??? 2.?導(dǎo)入struts2與hibernate的jar包???? 3.?配置web.xml文件(加入struts2的過濾器) ??<filter>???????? ???<filter-name>struts2</filter-name>???????? ???<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>?//?struts2過濾器???? ???</filter>???? ???<filter-mapping>???????? ??????<filter-name>struts2</filter-name>???????? ??????<url-pattern>/*</url-pattern>??//?過濾所有請求???? ???</filter-mapping>???? 4.?創(chuàng)建struts.xml????? ????WEB-INF/classes/struts.xml?-->?src/struts.xml????? ????<package?name="default"?namespace="/"?extends="struts-default"></package>???? 5.?配置hibernate.cfg.xml(hibernate的主配置文檔)???? ????src/hibernate.cfg.xml?? ????<hibernate-configuration>??? ??????<session-factory>???? ???????<property?name="connection.username">root</property>???? ???????<property?name="connection.password"></property>???? ???????<property?name="connection.driver_class">com.mysql.jbdc.Driver</property>???? ???????<property?name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property> ???????<property?name="dialect">org.hibernate.dialet.MySQLDialect</property>???? ???????<property?name="show_sql">true</property>???? ???????<property?name="format_sql">true</property>???? ???????<property?name="hbm2dd1.auto">update</property>???? ???????<property?name="hibernate.current_session_context_class">thread</property>//?使用getCurrentSession方式打開會話??? ??????</session-factory>?? ????</hibernate-configuration>
查看全部 -
重點? ?重點
查看全部 -
實現(xiàn)修改學(xué)生資料action和頁面調(diào)用
業(yè)務(wù)邏輯代碼:
public boolean updateStudent(Student stu) {
Transaction transaction=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction=session.beginTransaction();
session.update(stu);;
transaction.commit();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
action代碼:
public String save() throws ParseException{
Student stu=new Student();
stu.setSid(request.getParameter("sid"));
stu.setSname(request.getParameter("sname"));
stu.setGender(request.getParameter("gender"));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
stu.setBirthday(sdf.parse(request.getParameter("birthday")));
stu.setAddress(request.getParameter("address"));
StudentDao sd=new StudentDaoImpl();
sd.updateStudent(stu);
return "save_success";
}
查看全部 -
編寫修改學(xué)生的業(yè)務(wù)代碼:
動作1——頁面顯示學(xué)生資料的動作
邏輯查詢:
public Student queryStudentById(String sid) {
Transaction transaction=null;
Student s=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction=session.beginTransaction();
s=session.get(Student.class, sid);
transaction.commit();
return s;
}catch(Exception e){
e.printStackTrace();
return s;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
Action動作:
public String modify(){
String sid=request.getParameter("sid");
StudentDao sd=new StudentDaoImpl();
Student s=sd.queryStudentById("sid");
session.setAttribute("student_modify", s);
return "student_modify_success";
}
頁面顯示學(xué)生資料:
<form name="modifyForm" action="<%=path%>/students/Students_save.action" method="post">
<table width="400" >
? <tr>
? ? <td width="30%">學(xué)號:</td>
? ? <td><input type="text" name="sid" value='<s:property value="#session.student_modify.sid"/>' ?readonly="readonly"/></td>
? </tr>
? <tr>
? ? <td width="30%">姓名:</td>
? ? <td><input type="text" name="sname" value='<s:property value="#session.student_modify.sname"/>'/></td>
? </tr>
? <tr>
? ? <td>性別:</td>
? ? <td>
? ? ? <s:if test='%{#session.student_modify.gender=="男"}'>
? ? ? ? ?<input type="radio" name="gender" value="男" checked="checked"/>男
? ? ? ? ?<input type="radio" name="gender" value="女"/>女
? ? ? </s:if>
? ? ? <s:else>
? ? ? ? ?<input type="radio" name="gender" value="男" />男
? ? ? ? ?<input type="radio" name="gender" value="女" checked="checked"/>女
? ? ? </s:else>
? ? ? </td>
? </tr>
? <tr>
? ? <td>出生日期:</td>
? ? <td><input name="birthday" type="text" id="control_date" size="20"
? ? ? maxlength="10" onclick="new Calendar().show(this);" readonly="readonly"?
? ? ? value="<s:date name="#session.student_modify.birthday" format="yyyy-MM-dd"/>"
? ? ? />
? ? </td>
? </tr>
? <tr>
? ? <td>地址:</td>
? ? <td><input type="text" name="address" value='<s:property value="#session.student_modify.address"/>'/></td>
? </tr>
? <tr>
? ? <td colspan="2" align="center"><input class="button" type="submit" value="修改"></td>
? </tr>
</table>
</form>
動作2——保存修改后的學(xué)生資料的動作
查看全部 -
修改學(xué)生資料
介紹:通過點擊學(xué)生姓名,跳轉(zhuǎn)到需改頁面,涉及技術(shù)(信息回顯),注意:學(xué)生學(xué)號不能修改,回顯的信息是從數(shù)據(jù)庫中查出來的
1、界面原型演示
2、編寫修改學(xué)生業(yè)務(wù)邏輯代碼
3、編寫修改action
4、頁面調(diào)用
查看全部 -
添加學(xué)生的方法
提交事物:是為了下次開啟事物,可以正常執(zhí)行。
public boolean addStudent(Student stu) {
Transaction transaction=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction=session.beginTransaction();
stu.setSid(this.getNewSid());
session.save(stu);
transaction.commit();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
查看全部 -
添加學(xué)生信息
1、學(xué)生主鍵生成策略:學(xué)生sid是字符串類型,每次由系統(tǒng)自動生成。
額外編寫生成學(xué)生學(xué)號的方法:該方法功能——是去掉學(xué)號前的S,剩下轉(zhuǎn)換為數(shù)字,轉(zhuǎn)換成數(shù)字之后+1,之后再還原拼成8位的學(xué)生編號。
//獲得學(xué)生主鍵最大值,并且進行加1操作。
public String getNewSid(){
Transaction transaction=null;
String hql="";
String sid=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction=session.beginTransaction();
hql="select max(sid) from Student";
Query query=session.createQuery(hql);
sid=(String)query.uniqueResult(); ?
if(sid==null||"".equals(sid)){
sid="S00000001";
}else{
int i=Integer.parseInt(sid.substring(1));
i++;
String temp=String.valueOf(i);
int length=temp.length();
for(int j=0;j<=7-length;j++){
temp="0"+temp;
}
sid="S"+temp;
}
return sid;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
2、編寫添加學(xué)生業(yè)務(wù)邏輯代碼
查看全部 -
添加學(xué)生資料——實現(xiàn)步驟和界面原型設(shè)計
1、界面原型設(shè)計
2、編寫添加學(xué)生業(yè)務(wù)邏輯代碼
3、編寫添加action
4、頁面調(diào)用
查看全部 -
刪除學(xué)生資料
一:界面調(diào)用
<td><a href="<%=path %>/students/Student_delete?sid=<s:property value="#stu.sid"/>" onclick="javascript: return confirm('確認刪除嗎?');">刪除</a></td>
二:編寫業(yè)務(wù)邏輯代碼
public boolean deleteStudent(String sid) {
// TODO Auto-generated method stub
Transaction transaction=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
Student stu=(Student)session.get(Student.class, sid);
session.delete(stu);
transaction.commit();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
三:編寫刪除action
public String delete(){
String sid=request.getParameter("sid");
sd.deleteStudent(sid);
return "student_delete_success";
}
四:測試
? ? <result name="student_delete_success" type="chain">Student_query</result>
查看全部 -
頁面調(diào)用與數(shù)據(jù)展示
步驟1:修改tree.jsp訪問action的路徑由于樹形菜單打開方式,通過框架方式來打開。
步驟2:Students_query_success.jsp中添加struts標簽<%@ taglib prefix="s" uri="/struts-tags"%>,因為用到<s:iterator>標簽來遍歷學(xué)生,其中該標簽用到兩個屬性,value屬性:從哪里獲得要遍歷的集合,這里采用ognl表達式,struts的值棧分為對象棧和上下文棧,我們放到session中,其實是放到上下文棧中,從上下文棧中獲取數(shù)據(jù)必須以#開頭例如:value="#session.student_list"另外一個屬性var:表示集合中取出每一個對象的名字。
每一個對象的屬性用<s:property value="#stu.sid">來表示,(說明:
每次遍歷,將session中的list的值取出一個放到對象stu中,然后從stu中取出Student類的信息。)
出生日期:用中文格式顯示,可以用struts格式化日期標簽<s:date name
="#stu.birthday" format="yyyy年MM月dd日/>(format等同于SimpleDateFormat)
查看全部 -
設(shè)計學(xué)生Action類(action包中創(chuàng)建StudentAction類并編寫action方法來執(zhí)行查詢所有學(xué)生的方法)
action方法:
public String query(){
StudentDao sd=new StudentDaoImpl();
List<Student> list=sd.queryAllStudent();
if(list!=null&&list.size()>0){
session.setAttribute("student_list", list);
}
return "student_query_success";
}
struts.xml:業(yè)務(wù)分層原理,所以重新創(chuàng)建package,登陸是登陸,學(xué)生管理是學(xué)生管理的package。
?<package name="student_manage" namespace="/users" extends="struts-default">
? ? <action name="*_*" method="{2}" class="action.{1}Action">
? ? <result name="student_query_success">/students/Students_query_success.jsp</result>
? ? </action>
? ? </package>
查看全部 -
學(xué)生業(yè)務(wù)邏輯接口實現(xiàn)
查詢所有學(xué)生方法
public List<Student> queryAllStudent() {
Transaction transaction=null;
String hql="";
List<Student> list=null;
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction=session.beginTransaction();
hql="from Student";
Query query=session.createQuery(hql);
list=query.list();
transaction.commit();
return list;
}catch(Exception e){
e.printStackTrace();
return list;
}finally{
if(transaction!=null){
transaction=null;
}
}
}
test方法
@Test
public void testAddAllStudent(){
StudentDao sd=new StudentDaoImpl();
List<Student> studentList=sd.queryAllStudent();
for(int i=0;i<studentList.size();i++){
System.out.println(studentList.get(i));
}
}
查看全部 -
設(shè)計學(xué)生業(yè)務(wù)邏輯接口(包括學(xué)生的增刪改查的抽象方法)
添加學(xué)生:
public abstract boolean addStudent(Student s);
刪除學(xué)生:
public abstract boolean deleteStudent(String sid)
修改學(xué)生:
public abstract boolean updateStudent(Student stu);
查詢學(xué)生:
public abstract List<Student> queryAllStudent();public abstract Student queryStudentById(String sid);
查看全部 -
學(xué)生管理模塊
一、顯示學(xué)生資料
實現(xiàn)步驟:1、添加測試數(shù)據(jù)(TestStudent類中添加測試方法TestSaveStudent)
目的:在該方法中添加幾條數(shù)據(jù),查看是否能插入數(shù)據(jù)庫,從而驗證我們創(chuàng)建生成的student表是否可以進行操作。
? ? ? ? ? ? ? ? ?2、設(shè)計學(xué)生業(yè)務(wù)邏輯接口
? ? ? ? ? ? ? ? ?3、設(shè)計學(xué)生業(yè)務(wù)邏輯接口實現(xiàn)類
? ? ? ? ? ? ? ? ?4、設(shè)計學(xué)生Action類
? ? ? ? ? ? ? ? ?5、頁面調(diào)用
? ? ? ? ? ? ? ? ?6、顯示數(shù)據(jù)
2、刪除學(xué)生資料
3、學(xué)生主鍵生成策略
4、添加學(xué)生資料
5、修改學(xué)生資料
查看全部 -
表單驗證功能(登陸表單上顯示報錯信息)
方式一:客戶端(前端界面JavaScript)完成。
方式二:服務(wù)器端(后端struts的驗證框架)完成。
如果出錯,則返回登陸界面,并在表單上提示出錯信息。
步驟1:重寫從Actionsupport繼承的validate()方法。
addFieldError(key名,錯誤提示信息):提示錯誤信息的方法。
注意:validate()方法會對Action中所有方法進行驗證,只對登陸進行驗證,注銷不需要進行表單驗證,所以在注銷方法上加@SkipValidation注解,保證執(zhí)行注銷方法時不對表單進行驗證。
public void validate() {
if("".equals(user.getUsername().trim())){
addFieldError("usernameError", "用戶名不能為空");//錯誤字段提示信息
}
if((user.getPassword()).length()<6){
addFieldError("passwordError","密碼不能小于6位");
}
}
步驟2:
加入struts核心標簽庫<%@ taglib prefix="s" uri="/struts-tags"%>
加入
<div>
?<s:fielderror/> <!-- 顯示表單驗證的出錯信息 -->
</div>
查看全部
舉報