1.ssh的原始整合方式
hibernate
导入jar:
hibernate-release-5.0.7.Final\lib\required*.jar
hibernate-release-5.0.7.Final\lib\optional\c3p0*.jar
日志相关jar
数据库驱动
配置文件:
hibernate.cfg.xml
XXX.hbm.xml
struts2
导入jar:
struts-2.3.24-all\struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar
配置文件:
struts.xml
web.xml
spring
导入jar:
4个基本:core、context、beans、expression
2个日志:logging、log4j
4个aop:aop、aspects、aop联盟、aspectj织入
2个事务:jdbc、tx
配置文件:
applicationContext.xml
其他内容:
jstl
log4j.properties
jdbc.properties
注意:去重重复的jar包:javassist-3.11.0.GA.jar
原始整合存在的问题
- dao层(spring集成hibernate)
事务应该出现业务层:声明式事务控制
简化dao层session操作:使用hibernateTemplate - web层
spring的配置文件加载多次,spring的容器创建多次:(spring集成web)
PS: ServletContextListener监听ServletContext对象创建与销毁的
ServletContext在服务器启动时创建
action中编写spring客户端代码过于繁重:struts2集成spring
完成直接在Action中注入Service
2.ssh的xml整合方法
- spring整合hibernate
- 导入整合包:spring-orm.jar
- spring集成hibernate的第一种方式:保留hibernate.cfg.xml
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 要加载hibernate的核心配置 -->
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
</bean>
<!-- 配置hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置dao -->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
3.spring集成hibernate的第二种方式:摒弃hibernate.cfg.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1、数据源连接信息 -->
<!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh323</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>-->
<property name="dataSource" ref="dataSource"></property>
<!-- 2、hibernate其他配置参数 -->
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 3、加载映射 -->
<!-- <mapping resource="com/itheima/domain/Customer.hbm.xml"/> -->
<property name="mappingResources">
<list>
<value>com/itheima/domain/Customer.hbm.xml</value>
</list>
</property>
</bean>
- spring集成web
- 导入spring集成web的包:spring-web.jar
<!-- 配置全局的初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ServletContext servletContext = ServletActionContext.getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
CustomerService customerService = (CustomerService)applicationContext.getBean("customerService");
customerList = customerService.findAll();
struts2集成spring
导入struts2集成spring的包:struts2-spring-plugin.jar注意:前提先让spring集成web
struts2集成spring的两种方式: 第一种:Action的创建权仍然是struts2 第二种:Action的创建权交给spring容器 struts2框架优先去spring容器中匹配Action
下面这种方式必须在容器中存在bean
<!-- 配置Action -->
<action name="customer_*" class="customerAction" method="{1}">
<bean id="customerAction" class="com.itheima.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
3.ssh的注解整合方法(半注解半配置文件)
注解开发需要导入额外的jar:
struts2:struts2-convention-plugin.jar
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.itheima"></context:component-scan
//控制层
@Controller
@Namespace("/")
@ParentPackage("test")
@Scope("prototype")
@Results(value = { @Result(name="list",location="/jsp/customer/list.jsp"),
@Result(name="listAction",type="redirectAction",location="customer_list")})
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
@Action("customer_list")
public String list(){
list = customerService.findCustomer();
return "list";
}
@Action("customer_save")
public String save(){
customerService.save(customer);
return "listAction";
}
//Dao
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
@Autowired
private HibernateTemplate hibernateTemplate;
//实体
@Entity
@Table(name="cst_customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long cust_id;
private String cust_name;
private String cust_source;
//service层
@Service("customerService")
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerDao dao;