1.entity(dept.java , employee.java, dept.hbm.xml , employee.hbm.xml)
public class dept { private int deptid; private String deptname; private Set<employee> emps= new HashSet<employee>(); public dept(int deptid, String deptname) { super(); this.deptid = deptid; this.deptname = deptname; } public dept() { //如果该处没增加 构造函数 ,会报错无构造器 super(); // TODO Auto-generated constructor stub } public int getDeptid() { return deptid; } public void setDeptid(int deptid) { this.deptid = deptid; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<employee> getEmps() { return emps; } public void setEmps(Set<employee> emps) { this.emps = emps; } @Override public String toString() { return "dept [deptid=" + deptid + ", deptname=" + deptname + "]"; } }
public class employee { private int empid; private String empname; private double salary; private dept dept; public int getEmpid() { return empid; } public void setEmpid(int empid) { this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public double getSalary() { return salary;
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.entity" > <class name="dept" table="t_dept"> <id name="deptid" > <generator class="native"></generator> </id> <property name="deptname" length="20"/> <set name="emps"> <key column="dept_id"></key> <one-to-many class="employee"/> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.entity" > <class name="employee" table="t_employ"> <id name="empid" > <generator class="native"></generator> </id> <property name="empname" length="20" /> <property name="salary" type="double"></property> <many-to-one name="dept" column="dept_id " class="dept"></many-to-one> </class> </hibernate-mapping>
} public void setSalary(double salary) { this.salary = salary; } public dept getDept() { return dept; } public void setDept(dept dept) { this.dept = dept; } }
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 通常,一个session-factory节点代表一个数据库 --> <session-factory> <!-- 1. 数据库连接配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相关配置 --> <!-- 2.1 显示hibernate在运行时候执行的sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 2.3 自动建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="cn/itcast/entity/dept.hbm.xml"/> <mapping resource="cn/itcast/entity/employee.hbm.xml"/> </session-factory> </hibernate-configuration>
struts.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="dept" extends="struts-default"> <!-- 拦截器配置 --> <interceptors> <interceptor name="sessioninterceptor" class="cn.itcast.interceptor.sessioninterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="sessioninterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <!-- action配置 --> <action name="show" class="cn.itcast.deptaction.deptaction"> <result name="success">/index.jsp</result> </action> </package> </struts>
hibernateutils.java package cn.itcast.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class hibernateutils { private static SessionFactory sf; static{ sf= new Configuration().configure().buildSessionFactory(); } public static Session getsession(){ return sf.getCurrentSession(); } }
package cn.itcast.interceptor; import org.hibernate.Session; import cn.itcast.utils.hibernateutils; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class sessioninterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub try { Session session=hibernateutils.getsession(); session.beginTransaction(); String result=invocation.invoke();
//session 在action之前就已经存在 该处可以打断点理解 session.getTransaction().commit(); return result; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "error"; } } }
public class deptaction extends ActionSupport{ private deptservice deptservice= new deptservice(); @Override public String execute() throws Exception { // TODO Auto-generated method stub dept dept = deptservice.findbyid(4); ActionContext.getContext().getContextMap().put("dept",dept ); return SUCCESS; } }
public class deptdao { public dept findbyid(int id){ return (dept) hibernateutils.getsession().get(dept.class, id); } }
public class deptservice { private deptdao deptdao= new deptdao(); public dept findbyid(int id){ return deptdao.findbyid(id); } }
index.jsp
<body> <h2>部门:<s:property value="#request.dept.deptname"/></h2> <table> <tr> <td> 员工编号</td> <td>员工姓名</td> <td>员工薪水</td> </tr> <s:if test="#request.dept.emps!=null"> <s:iterator var="emp" value="#request.dept.emps"> <tr> <td><s:property value="#emp.empid"/></td> <td><s:property value="#emp.empname"/></td> <td><s:property value="#emp.salary"/></td> </tr> </s:iterator> </s:if> <s:else> <tr><td colspan="3">没有员工信息</td></tr> </s:else> </table> </body> </html>