19:47:49
这是截图
闲话不说 就开始了
web-xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- structs2的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts-xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <!-- 用于页面重转 --> <action name="turnaddMessage" class="sedion.xq.action.turnAction"> <result name="success"> /WEB-INF/student/addMessage.jsp </result> </action> <action name="turndeleteMessage" class="sedion.xq.action.turnAction" method="getid"> <result name="success"> /WEB-INF/student/deleteMessage.jsp </result> </action> <action name="turnfindMessage" class="sedion.xq.action.turnAction" method="getid"> <result name="success"> /WEB-INF/student/findMessage.jsp </result> </action> <!-- look stu message --> <action name="lookMessageAction" class="sedion.xq.action.lookMessageAction"> <result name="success"> /WEB-INF/student/lookMessage.jsp </result> <result name="input">/index.jsp</result> </action> <!-- delet stu message --> <action name="deleteMessageAction" class="sedion.xq.action.deleteMessageAction"> <result name="success" type="chain"> lookMessageAction </result> <result name="input">/student/deleteMessage.jsp</result> </action> <!-- add stu message --> <action name="addMessageActon" class="sedion.xq.action.addMessageAction"> <result name="success" type="chain"> lookMessageAction </result> <result name="input">/student/deleteMessage.jsp</result> </action> <!-- find stu message --> <action name="findMessageAction" class="sedion.xq.action.findMessageAction"> <result name="success"> /WEB-INF/student/updateMessage.jsp </result> <result name="input">/student/findMessage.jsp</result> </action> <!-- update stu message --> <action name="updateMessageActon" class="sedion.xq.action.updateMessageAction"> <result name="success" type="chain"> lookMessageAction </result> <result name="input">/WEB-INF/student/updateMessage.jsp</result> </action> </package> </struts>
hibernate.cfg.xml:(申明 这里用的是SQL service 2005 若修改 这里修改 链接数据库 该数据库 名字LQQ 里面一张表格 在最后会发布)
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">sa</property> <property name="connection.url"> jdbc:jtds:sqlserver://localhost:1433;DatabaseName=LQQ </property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <!--<property name="myeclipse.connection.profile"> LoginSystem </property> --> <property name="connection.password">sa</property> <property name="connection.driver_class"> net.sourceforge.jtds.jdbc.Driver </property> <property name="show_sql">true</property> <!-- POJO 类映射配置--> <mapping resource="sedion/xq/ORM/Stuinfo.hbm.xml" /> </session-factory> </hibernate-configuration>
然后 贴出 HibernateSessionFactory.java:
package sedion.xq.hibernate; import javax.swing.JOptionPane; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static SessionFactory sessionFactory; private static Configuration configuration = new Configuration(); public HibernateSessionFactory() { } static { try { Configuration configure = configuration .configure("hibernate.cfg.xml"); System.out.println(configure); sessionFactory = configure.buildSessionFactory(); } catch (Exception e) { // TODO: handle exception System.out.println("HibernateSessionFactory wrong!!"); message("生成SessionFactory失败"+e); } } public static Session getSession() { System.out.println(sessionFactory.openSession()); return sessionFactory.openSession(); } public static void message(String mess){ int type=JOptionPane.YES_NO_CANCEL_OPTION; String title="提示消息"; JOptionPane.showMessageDialog(null,mess,title, type); } }
然后 再贴出 ORM:
Stuinfo.java:
package sedion.xq.ORM; public class Stuinfo { private String id; private String name; private String sex; private int age; private float weight; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } }
该类的映射文件如下:
Stuinfo.hbm.xml(自然可以根据这个表格 还原数据库的表格 表名:stuinfo):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0 //EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="sedion.xq.ORM.Stuinfo" table="stuinfo"> <id name="id" type="string"> <column name="id" length="20" /> <generator class="assigned" /> </id> <property name="name" type="string" length="20" /> <property name="sex" type="string" length="5" /> <property name="age" type="integer" /> <property name="weight" type="float" /> </class> </hibernate-mapping>
然后 再贴出 DAO层:
StudentDao.java:
package sedion.xq.DAO; import java.util.List; import javax.swing.JOptionPane; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import sedion.xq.ORM.Stuinfo; import sedion.xq.hibernate.HibernateSessionFactory; import sun.swing.UIAction; public class StudentDao { private Transaction transaction; private Session session; private Query query; public StudentDao() { } // 显示 public List findAllInfo() { session = HibernateSessionFactory.getSession(); try { transaction = session.beginTransaction(); String querysString = "from Stuinfo"; query = session.createQuery(querysString); List<Stuinfo> list = query.list(); System.out.println(list); transaction.commit(); session.close(); return list; } catch (Exception e) { message("findInfo.error" + e); e.printStackTrace(); return null; } } // save message public boolean saveInfo(Stuinfo info) { try { session = HibernateSessionFactory.getSession(); transaction = session.beginTransaction(); session.save(info); transaction.commit(); session.close(); return true; } catch (Exception e) { message("saveInfo.error" + e); e.printStackTrace(); return false; } } public boolean deleteInfo(String id) { try { session = HibernateSessionFactory.getSession(); transaction = session.beginTransaction(); Stuinfo info = new Stuinfo(); info = (Stuinfo) session.get(Stuinfo.class, id); session.delete(info); transaction.commit(); session.close(); return true; } catch (Exception e) { message("deleteInfo.error" + e); e.printStackTrace(); return false; } } public List<Stuinfo> findInfo(String id) { try { session = HibernateSessionFactory.getSession(); transaction = session.beginTransaction(); query = session.createQuery("from Stuinfo stu where stu.id like?"); query.setString(0, id); List<Stuinfo> list = query.list(); System.out.println(list); transaction.commit(); session.close(); return list; } catch (Exception e) { message("deleteInfo.error" + e); e.printStackTrace(); return null; } } public boolean updateInfo(Stuinfo info,String id) { try { System.out.println(id+"didiididid"); session = HibernateSessionFactory.getSession(); transaction = session.beginTransaction(); query = session.createQuery("delete from Stuinfo stu where stu.id like?"); query.setString(0, id); query.executeUpdate(); session.saveOrUpdate(info); transaction.commit(); session.close(); return true; } catch (Exception e) { message("updateInfo.error" + e); e.printStackTrace(); return false; } } private void message(String mess) { int type = JOptionPane.YES_NO_CANCEL_OPTION; String title = "提示消息"; JOptionPane.showMessageDialog(null, mess, title, type); } }
下面是6个 jsp:
index.JSP:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息管理</title> </head> <body> <div align="center"> <font size=6>学生信息管理系统Struts2+ Hibernate</font> <hr color="red"/> <font > ------made by sedion.xq</font> <br /> <s:a href="lookMessageAction.action">点击进入</s:a> </div> </body> </html>
lookMessage.jsp:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息统一查询</title> </head> <body bgcolor="#7EC0EE"> <s:div align="center"> <hr color="red" /> <br /> <table align="center" width="80%"> <tr> <td width="25%"> 查看学生信息 </td> <td width="25%"> <s:a href="turnaddMessage.action">添加学生信息 </s:a> </td> <td width="25%"> <s:a href="turnfindMessage.action">修改学生信息 </s:a> </td> <td width="25%"> <s:a href="turndeleteMessage.action">删除学生信息 </s:a> </td> </tr> </table> <br /> <hr color="red" /> <br /> <br /> <br /> <span>你要查询的数据表的人数共有 <%=request.getSession().getAttribute("count")%>人</span> </s:div> <table align="center" width="80%" border="2" bordercolor="blue"> <tr> <th> 记录条数 </th> <th> 学号 </th> <th> 姓名 </th> <th> 性别 </th> <th> 年龄 </th> <th> 体重 </th> </tr> <s:iterator id="lsit" value="list" status="st"> <tr> <td align="center"> <s:property value="#st.count" /> </td> <td> ${id} </td> <td> ${name} </td> <td> ${sex} </td> <td> ${age} </td> <td> ${weight} </td> </tr> </s:iterator> </table> </body> </html>
updateMessage.jsp:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息--修改信息</title> </head> <body bgcolor="#7EC0EE"> <s:div align="center"> <hr color="red" /> <br /> <table align="center" width="80%"> <tr> <td width="25%"> <s:a href="lookMessageAction.action">查看学生信息</s:a> </td> <td width="25%"> <s:a href="turnaddMessage.action">添加学生信息</s:a> </td> <td width="25%"> <s:a>修改学生信息 </s:a> </td> <td width="25%"> <s:a href="turndeleteMessage.action">删除学生信息 </s:a> </td> </tr> </table> <br /> <hr color="red" /> <br /> <br /> <br /> <center> <font color="black" size="6">修改学生信息</font> </center> </s:div> <s:form action="updateMessageActon" method="post"> <table align="center" width="30%" border="2"> <s:iterator value="list" id="list"> <tr> <td> <s:textfield readonly="true" name="id" label="学号" maxLength="16" value="%{id}"> </s:textfield> </td> <td> <s:textfield name="name" label="姓名" value="%{name}"> </s:textfield> </td> <td> <s:select name="sex" label="性别" list="{'男','女'}" value="%{sex}" /> </td> <td> <s:textfield name="age" label="年龄" value="%{age}"> </s:textfield> </td> <td> <s:textfield name="weight" label="体重" value="%{weight}"> </s:textfield> </td> <td colspan="2"> <s:submit value="提交" align="center"></s:submit> <s:reset value="清除" align="center"></s:reset> </td> </tr> </s:iterator> </table> </s:form> <br> </body> </html>
findMessage.jsp:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息管理系统-查找</title> </head> <body bgcolor="#7EC0EE"> <s:div align="center"> <hr color="red" /> <br /> <table align="center" width="80%"> <tr> <td width="25%"> <a href="lookMessageAction.action">查看学生信息</a> </td> <td width="25%"> <s:a href="turnaddMessage.action">添加学生信息 </s:a> </td> <td width="25%"> <s:a>修改学生信息 </s:a> </td> <td width="25%"> <s:a href="turndeleteMessage.action">删除学生信息 </s:a> </td> </tr> </table> <br /> <hr color="red" /> <br /> <br /> <br /> <font size="5">修改学生信息</font> </s:div> <s:form action="findMessageAction" method="post"> <table align="center" width="40%" border="2" bordercolor="blue"> <tr> <td width="40%"> <s:select name="id" label="请选择要修改的学生的学号:" list="list.{id}"></s:select> </td> <td> <s:submit value="确定" align="center"></s:submit> </td> </tr> </table> </s:form> </body> </html>
deleteMessage.jsp:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息--删除</title> </head> <body> <body bgcolor="#7EC0EE"> <s:div align="center"> <hr color="red" /> <br /> <table align="center" width="80%"> <tr> <td width="25%"> <s:a href="lookMessageAction.action">查看学生信息</s:a> </td> <td width="25%"> <s:a href="turnaddMessage.action">添加学生信息 </s:a> </td> <td width="25%"> <s:a href="turnfindMessage.action">修改学生信息 </s:a> </td> <td width="25%"> <s:a>删除学生信息 </s:a> </td> </tr> </table> <br /> <hr color="red" /> <br /> <br /> <br /> <font size="5">删除学生信息</font> </s:div> <s:form action="deleteMessageAction" method="post"> <table align="center" width="40%" border="2" bordercolor="blue"> <tr> <td width="40%"> <s:select name="id" label="请选择要删除的学生的学号:" list="list.{id}"></s:select> </td> <td > <s:submit value="确定" align="center"></s:submit> </td> </tr> </table> </s:form> </body> </html>
addMessage.jsp:
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息--添加信息</title> </head> <body bgcolor="#7EC0EE"> <s:div cssClass="align"> <hr color="red" /> <br /> <table align="center" width="80%"> <tr> <td width="25%"> <s:a href="lookMessageAction.action">查看学生信息</s:a> </td> <td width="25%"> 添加学生信息 </td> <td width="25%"> <s:a href="turnfindMessage.action">修改学生信息 </s:a> </td> <td width="25%"> <s:a href="turndeleteMessage.action">删除学生信息 </s:a> </td> </tr> </table> <br /> <hr color="red" /> <br /> <br /> <br /> <center> <font color="black" size="6">添加学生信息</font> </center> </s:div> <s:form action="addMessageActon" method="post"> <table align="center" width="30%" border="2"> <tr> <td> <s:textfield name="id" label="学号" maxLength="16"> </s:textfield> </td> <td> <s:textfield name="name" label="姓名" > </s:textfield> </td> <td> <s:select name="sex" label="性别" list="{'男','女'}"/> </td> <td> <s:textfield name="age" label="年龄"> </s:textfield> </td> <td> <s:textfield name="weight" label="体重" > </s:textfield> </td> <td colspan="2" > <s:submit value="提交" align="center"></s:submit> <s:reset value="清除" align="center"></s:reset> </td> </tr> </table> </s:form> </body> </html>
下面是6个action包里面的处理:
turnAction.java:
package sedion.xq.action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import sedion.xq.DAO.StudentDao; import sedion.xq.ORM.Stuinfo; public class turnAction extends ActionSupport { private List<Stuinfo> list = new ArrayList<Stuinfo>(); public List<Stuinfo> getList() { return list; } public void setList(List<Stuinfo> list) { this.list = list; } public String execute() throws Exception { return "success"; } public String getid() throws Exception { StudentDao dao = new StudentDao(); list = dao.findAllInfo(); return "success"; } }
lookMessageAction.java:
package sedion.xq.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.sun.org.apache.bcel.internal.generic.NEW; import sedion.xq.DAO.StudentDao; import sedion.xq.ORM.Stuinfo; public class lookMessageAction extends ActionSupport{ private HttpServletRequest request; private String message = "input"; private List<Stuinfo> list=new ArrayList<Stuinfo>(); public List<Stuinfo> getList() { return list; } public void setList(List<Stuinfo> list) { this.list = list; } @SuppressWarnings("unchecked") public String execute() throws Exception { try { request = ServletActionContext.getRequest(); StudentDao dao = new StudentDao(); list = dao.findAllInfo(); request.getSession().setAttribute("count", list.size()); request.getSession().setAttribute("allInfo", list); message = "success"; } catch (Exception e) { e.printStackTrace(); } return message; } }
updateMessageAction:
package sedion.xq.action; import javax.swing.JOptionPane; import sedion.xq.DAO.StudentDao; import sedion.xq.ORM.Stuinfo; import com.opensymphony.xwork2.ActionSupport; public class updateMessageAction extends ActionSupport { private String id; private String name; private String sex; private int age; private float weight; private String message = "input"; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } private Stuinfo Info() { Stuinfo info = new Stuinfo(); info.setId(this.getId()); info.setName(this.getName()); info.setSex(this.getSex()); info.setAge(this.getAge()); info.setWeight(this.getWeight()); return info; } public String execute() throws Exception { StudentDao dao = new StudentDao(); boolean update = dao.updateInfo(Info(),this.getId()); if (update) { message = "success"; } return message; } private void message(String mess) { int type = JOptionPane.YES_NO_CANCEL_OPTION; String title = "提示消息"; JOptionPane.showMessageDialog(null, mess, title, type); } }
findMessageAction.java:
package sedion.xq.action; import java.util.ArrayList; import java.util.List; import sedion.xq.DAO.StudentDao; import sedion.xq.ORM.Stuinfo; import com.opensymphony.xwork2.ActionSupport; public class findMessageAction extends ActionSupport { private String id; private String message; private List<Stuinfo> list = new ArrayList<Stuinfo>(); public List<Stuinfo> getList() { return list; } public void setList(List<Stuinfo> list) { this.list = list; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String execute() throws Exception { try { StudentDao dao = new StudentDao(); list = dao.findInfo(this.getId()); message = "success"; } catch (Exception e) { e.printStackTrace(); } return message; } }
deleteMessageAction.java:
package sedion.xq.action; import org.apache.struts2.ServletActionContext; import sedion.xq.DAO.StudentDao; public class deleteMessageAction { private String id; private String message; public String getId() { return id; } public void setId(String id) { this.id = id; } public String execute() throws Exception { try { StudentDao dao = new StudentDao(); boolean del=dao.deleteInfo(this.getId()); if (del) { message = "success"; } } catch (Exception e) { e.printStackTrace(); } return message; } }
addMessageAction.java:
package sedion.xq.action; import java.util.List; import javax.swing.JOptionPane; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.finder.ClassFinder.Info; import sedion.xq.DAO.StudentDao; import sedion.xq.ORM.Stuinfo; public class addMessageAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String id; private String name; private String sex; private int age; private float weight; private String message = "input"; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } public String execute() throws Exception { StudentDao dao = new StudentDao(); boolean save = dao.saveInfo(Info()); if (save) { message = "success"; } return message; } private Stuinfo Info() { Stuinfo info = new Stuinfo(); info.setId(this.getId()); info.setName(this.getName()); info.setSex(this.getSex()); info.setAge(this.getAge()); info.setWeight(this.getWeight()); return info; } private void message(String mess) { int type = JOptionPane.YES_NO_CANCEL_OPTION; String title = "提示消息"; JOptionPane.showMessageDialog(null, mess, title, type); } }
终于到最后了 , 下面发下 表格的截图
谢谢 java大师我的偶像 一步一步接近