16ssh整合源码
Posted yungcs_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了16ssh整合源码相关的知识,希望对你有一定的参考价值。
16、ssh整合源码
**
本系列博客,仅作为学习使用,切勿转载,学习
**
EmployeeAction
public class EmployeeAction extends ActionSupport
// IOC容器注入
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService)
this.employeeService = employeeService;
@Override
public String execute()
int empid = 17;
// 调用Service
Employee emp = employeeService.findById(empid);
// 保存到request
Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
request.put("emp", emp);
return SUCCESS;
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
员工: $emp.empName
部门: $emp.dept.name
</body>
</html>
struts配置
<?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" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
bean-action.xml
<bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean>
bean-base.xml
<!-- 所有配置的公共部门 -->
<!-- 1) 连接池实例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>
<!-- 2) SessionFactory实例创建 -->
<!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- a. 连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
<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>
<!-- c. 映射配置 -->
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 3) 事务配置 -->
<!-- # 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</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 expression="execution(* cn.itcast.service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
bean-dao.xml
<bean id="employeeDao" class="cn.itcast.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
bean-service.xml
<bean id="employeeService" class="cn.itcast.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
EmployeeService
public class EmployeeService
// IOC注入
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao)
this.employeeDao = employeeDao;
/**
* 查询
* @param emp
*/
public Employee findById(Serializable id)
Employee emp = employeeDao.findById(id);
return emp;
EmployeeDao
public class EmployeeDao
// 注入SessionFactory对象
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
/**
* 查询
* @param emp
*/
public Employee findById(Serializable id)
return (Employee) sessionFactory.getCurrentSession().get(Employee.class, id);
各大实体类代码编写
Dept
public class Dept
private int id;
private String name;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
Employee
public class Employee
private int id;
private String empName;
private double salary;
private Dept dept;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getEmpName()
return empName;
public void setEmpName(String empName)
this.empName = empName;
public double getSalary()
return salary;
public void setSalary(double salary)
this.salary = salary;
public Dept getDept()
return dept;
public void setDept(Dept dept)
this.dept = dept;
/ssh/src/cn/itcast/entity/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="cn.itcast.entity">
<class name="Dept" table="t_dept">
<id name="id" column="deptId">
<generator class="native"></generator>
</id>
<property name="name" column="deptName"></property>
</class>
</hibernate-mapping>
/ssh/src/cn/itcast/entity/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="cn.itcast.entity">
<class name="Employee" table="t_employee">
<id name="id" column="empId">
<generator class="native"></generator>
</id>
<property name="empName"></property>
<property name="salary"></property>
<!-- 多对一 -->
<many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
</class>
</hibernate-mapping>
jar
spring-aop\\aopalliance.jar
spring-aop\\aspectjrt.jar
spring-aop\\aspectjweaver.jar
spring-aop\\spring-aop-3.2.5.RELEASE.jar
spring-core\\commons-logging-1.1.3.jar
spring-core\\spring-beans-3.2.5.RELEASE.jar
spring-core\\spring-context-3.2.5.RELEASE.jar
spring-core\\spring-core-3.2.5.RELEASE.jar
spring-core\\spring-expression-3.2.5.RELEASE.jar
spring-orm\\c3p0-0.9.1.2.jar
spring-orm\\mysql-connector-java-5.1.12-bin.jar
spring-orm\\spring-jdbc-3.2.5.RELEASE.jar
spring-orm\\spring-orm-3.2.5.RELEASE.jar
spring-web\\spring-web-3.2.5.RELEASE.jar
spring-web\\struts2-spring-plugin-2.3.4.1.jar
struts2.3\\c3p0-0.9.1.2.jar
struts2.3\\commons-dbutils-1.6.jar
struts2.3\\commons-fileupload-1.2.2.jar
struts2.3\\commons-io-2.0.1.jar
struts2.3\\commons-lang3-3.1.jar
struts2.3\\freemarker-2.3.19.jar
struts2.3\\javassist-3.11.0.GA.jar
struts2.3\\mysql-connector-java-5.1.12-bin.jar
struts2.3\\ognl-3.0.5.jar
struts2.3\\struts2-core-2.3.4.1.jar
struts2.3\\xwork-core-2.3.4.1.jar
hibernate3.60\\antlr-2.7.6.jar
hibernate3.60\\commons-collections-3.1.jar
hibernate3.60\\dom4j-1.6.1.jar
hibernate3.60\\hibernate3.jar
hibernate3.60\\hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.60\\javassist-3.12.0.GA.jar
hibernate3.60\\jta-1.1.jar
hibernate3.60\\mysql-connector-java-5.1.12-bin.jar
hibernate3.60\\slf4j-api-1.6.1.jar
以上是关于16ssh整合源码的主要内容,如果未能解决你的问题,请参考以下文章