1.创建目录结构
2.引入struts,hibernate,springjar包按照顺序
3.引入三个框架的配置文件。
4.hibernate+spring主要配置applicationContext.xml 连接池、sessionFactory、事务提交
5.struts+spring 将struts创建的action交给spring管理
需要引入struts-spring-plugin.jar struts的配置文件设置action的包的扫描范围
6.配置spring扫描包的范围
7.完善前端页面、action、service、dao、entity(注意实体映射注解)
dao层需要依赖注入SessionFactory 注意不要忘了setget方法
hibernate.xml配置文件实体映射
框架调试
8.jar包去重 留下同名高版本的jar包
2.引入struts,hibernate,springjar包按照顺序
3.引入三个框架的配置文件。
4.hibernate+spring主要配置applicationContext.xml 连接池、sessionFactory、事务提交
5.struts+spring 将struts创建的action交给spring管理
需要引入struts-spring-plugin.jar struts的配置文件设置action的包的扫描范围
6.配置spring扫描包的范围
7.完善前端页面、action、service、dao、entity(注意实体映射注解)
dao层需要依赖注入SessionFactory 注意不要忘了setget方法
hibernate.xml配置文件实体映射
框架调试
8.jar包去重 留下同名高版本的jar包
9.web.xml 配置struts过滤器 启动监听器(加载bean)
spring的配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描包的范围,自动扫描包下有相应注解的类,完成bean的装配-->
<context:component-scan base-package="org.sdibt.king"></context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="3306"></property>
<property name="acquireIncrement" value="5"></property> <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->
<property name="initialPoolSize" value="10"></property> <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<!-- 最大空闲时间,超过空闲时间的连接将被丢弃
[需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800]
-->
<property name="maxIdleTime" value="300"></property>
<property name="idleConnectionTestPeriod" value="60"></property> <!-- 每60秒检查连接池中的空闲连接 -->
<property name="maxStatements" value="20"></property> <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 -->
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务支持注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
hibernate的配置文件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>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<!-- 配置自动根据实体生成数据库的表一般设置成update,设置为create每次都会删除原来的表再新建一个表-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示生成的sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化生成的sql -->
<property name="hibernate.format_sql">true</property>
<!-- 添加实体类 -->
<mapping class="org.sdibt.king.entity.User"/>
</session-factory>
</hibernate-configuration>
struts的配置文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 增加struts搜索action的包范围 -->
<constant name="struts.convention.action.packages" value="org.sdibt.king.controller"/>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>web_08_ssh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置自定义过滤器必须放在struts过滤器之前配置-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.sdibt.king.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置struts2过滤器 -->
<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>
</web-app>
controller层代码示例
@Controller
@Namespace("/user")
@ParentPackage("struts-default")
public class UserAction {
@Resource
private IUserService userService;//用户信息业务逻辑处理对象
private User user;
/**
* 添加用户
* @return resultStr 指定跳转页面
*/
@Action(value="addUser",results={@Result(name="success",location="/add_success.jsp"),
@Result(name="fail",location="/add_fail.jsp")})
public String addUser(){
String resultStr="";
boolean result = userService.addUser(user);//添加一个用户
//判断添加用户是否成功
if(result==true){
resultStr="success";
}else{
resultStr="fail";
}
return resultStr;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
service实现层代码示例
@Service
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;//用户信息数据库操作对象
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
/**
* 添加用户信息
* @param user 用户对象
* return result 添加结果
*/
@Override
@Transactional
public boolean addUser(User user) {
boolean result = userDao.saveUser(user);//保存用户信息
return result;
}
}
dao实现层代码示例
@Repository
public class UserDaoImpl implements IUserDao {
@Resource
private SessionFactory sessionFactory;//session工厂
/**
* 保存用户信息
* @param user 用户对象
* return result 保存结果
*/
@Override
public boolean saveUser(User user) {
boolean result=true;//保存结果
Session session=null;//session
try {
session = sessionFactory.getCurrentSession();//获取session
session.save(user);//保存用户信息
} catch (HibernateException e) {
result=false;//保存失败
}
return result;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}