需求:
查询员工和所属部门
1.引入jar包
hibernate
struts
spring-core
spring-aop
spring-orm
spring-web
2.实体类及映射文本配置
Dept
public class Dept {
private int deptId;
private String deptName;
set...
get...
}
Dept.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.entity"> <class name="Dept" table="t_dept"> <id name="deptId" column="depId"> <generator class="native"/> </id> <property name="deptName"/> </class> </hibernate-mapping>
Employee
public class Employee { private int empId; private String empName; private double salary; private Dept dept; set... get... }
Employee.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.entity"> <class name="Employee" table="t_employee"> <id name="empId"> <generator class="native"/> </id> <property name="empName"/> <property name="salary"/> <many-to-one name="dept" class="Dept" column="dept_id"/> </class> </hibernate-mapping>
3.dao、service和action
EmployeeDao
public class EmployeeDao { // 注入sessionfactory private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Employee findById(Serializable id){ return (Employee) this.sessionFactory.getCurrentSession().get(Employee.class,id); } }
EmployeeService
public class EmployeeService { private EmployeeDao employeeDao; public void setEmployeeDao(EmployeeDao employeeDao) { this.employeeDao = employeeDao; } public Employee findById(Serializable id){ return employeeDao.findById(id); } }
EmployeeAction
public class EmployeeAction extends ActionSupport{ private EmployeeService employeeService; public void setEmployeeService(EmployeeService employeeService) { this.employeeService = employeeService; } @Override public String execute() throws Exception { int empid= 3; Employee employee = employeeService.findById(empid); Map<String, Object> request = ActionContext.getContext().getContextMap(); request.put("employee",employee); return SUCCESS; } }
4.struts和spring相关配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--struts配置--> <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> <!--Spring配置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
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="emp" extends="struts-default"> <!--action实例交给spring去创建--> <action name="show" class="employeeAction"> <result name="success">/show.jsp</result> </action> </package> </struts>
bean-base.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--所有配置的公共部分--> <!--连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test"></property> <property name="user" value="root"></property> <property name="password" value="juaner767"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"/> </bean> <!--sessionfactory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--hibernate常用配置--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:com/juaner/entity/*.hbm.xml</value> </list> </property> </bean> <!--事务配置--> <!--事务管理器--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--事务增强--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!--aop配置--> <aop:config> <aop:pointcut id="pt" expression="execution(* com.juaner.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
bean-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="productDao" class="com.juaner.dao.ProductDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="employeeDao" class="com.juaner.dao.EmployeeDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
bean-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="productService" class="com.juaner.service.ProductService"> <property name="productDao" ref="productDao"/> </bean> <bean id="employeeService" class="com.juaner.service.EmployeeService"> <property name="employeeDao" ref="employeeDao"/> </bean> </beans>
bean-action.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--scope=prototype 用的时候创建实例--> <bean id="employeeAction" class="com.juaner.EmployeeAction" scope="prototype"> <property name="employeeService" ref="employeeService"/> </bean> </beans>
5.显示页面
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>显示员工信息</title> </head> <body> 员工:${employee.empName}<br> 薪水:${employee.salary}<br> </body> </html>
6.为了访问懒加载数据dept,需要配置open session in view
<!--配置spring的open session in view 为了在jsp中使用懒加载数据--> <filter> <filter-name>OpenSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInView</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
结果: