博主是个初学者,哪里不对请指正哈。不多介绍了,直接上代码吧。我不知道怎么上传文件,有需要的留言我把文件发给你吧,这里就放一些关键代码
首先先导包
Hibernate:
Jar包:
\hibernate-release-5.0.7.Final\lib\required\*
C3p0+mysql驱动
Log4j日志包
配置文件:
映射文件 xxx.hbm.xml
核心文件 hibernate.cfg.xml
Struts2: 可插拔式框架
Jar包:
\struts-2.3.24\apps\struts2-blank\WEB-INF\lib\*
配置文件:
Struts.xml
Web.xml (配置StrutsPreparedAndExecutfilter)
Spring:
Jar包:
4个核心+日志+4个aop+tx+jdbc+test
配置文件:
applicationContext.xml
先是整合过渡版本,三大框架先独立完成自己的功能,功能就是简单的从通过Struts的modelDriven从网页封装数据,然后用Spring创建 Action,Service,Dao的实现类,用Hibernate和数据库交流。
下面开始第一步优化
万能的Spring有一个监听器类可以在服务器启动就自动加载,且只需加载一次。
我们要做的就是:导包+配置
提供的监听器位置:spring-web.jar org.springframework.web.context.ContextLoaderListener
配置代码写在web.xml
<listener>在服务器启动的时候就加载applicationContext.xml一次
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
由于默认是去/web-inf/下加载,需要改成去src下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
第二步,Spring整合struts2的
public class UserAction implements ModelDriven<User>{
private User user = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
public String saveUser(){
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService =(UserService)context.getBean("UserService");
user.setUid(10);
userService.saveUser(user);
return null;
}
如果交给Spring是这样的只要一句话
private UserService us;
public void setUs(UserService us) {
this.us = us;
}
public String saveUser(){
us.saveUser(user);
return null;
}
代码是<constant name= "struts.objectFactory" value="Spring">
struts2-spring-plugin.jar
这里的Struts配置文件要改一下。class要改成Spring bean的ID,不着急,等再一步优化就把Struts配置文件删了
<!-- 找spring创建action的bean的id值 来获取action对象 -->
<action name="user_*" class="userAction" method="{1}"></action>
Ps:action是多实例 需要配置Scope=prototype
优化,Spring整合hibernate、
@Entity
@Table(name="user")
public class User
{
@Id
@Column(name="uid")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer uid;
直接上优化后的Spring配置文件看看怎样一步步代替的
<!-- spring加载src下的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- c3p0 -->c3p0替代了cfg文件中数据库的信息
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- hibernatetemplate -->用来与数据库交流的工具类。需要传递sessionfactory
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="c3p0"></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>
扫描雷相当于扫描hbm文件,hbm已经通过注解写到了持久类里
<!-- 指定扫描类 -->
<property name="packagesToScan">
<list>
<value>cn.itcast.domain</value>
</list>
</property>
</bean>
这里还要通过Aop给方法添加事务,
hibernatetemplate没有事务提交功能,用的注解的方式,需要在方法上添加事务的注解
<!-- 事务的配置 切面类--> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 1 从sessionFactory中获取连接池 2 从连接池中获取到连接,开启事务 3 把开启了事务的连接绑定到当前线程中 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 自己的类 --> <!-- 开启注解扫描 --> <context:component-scan base-package="cn.itcast"></context:component-scan> <!-- 开启事务的注解方式 --> <tx:annotation-driven transaction-manager="transactionManager"/>
优化到现在差不多了,可以把Struts和Hibernate的配置文件全删了,Struts配置文件中还有把权限给Spring的代码,需要写到web中的拦截器里。
<init-param>
<param-name>struts.objectFactory</param-name>
<param-value>spring</param-value>
</init-param>
宿舍太冷了,打不动了,先说这些吧,