SSH框架整合思想
SSH框架中,web层使用struts2框架响应浏览器请求,返回数据;service层使用spring框架ioc管理项目中对象,aop抽取冗余代码;dao层使用hibernate操作数据库。
SSH框架整合思想,就是先把struts2与spring整合,再把hibernate与spring整合。
struts2与spring整合:将struts2的action创建交给spring,并且配置action多实例。
hibernate与spring整合:把hibernate的sessionFactory交给spring管理,把hibernate的数据库信息配置交给spring管理。
SSH框架整合案例
导入jar包
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- struts2过滤器 -->
<display-name>Struts2</display-name>
<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:applicationContext.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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: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">
<!-- 开启扫描 -->
<context:component-scan base-package="org.haiwen"></context:component-scan>
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- sessionFactory创建交给spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定使用hibernate核心配置文件 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 创建hibernateTemplate对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2dll.auto">update</property>
<mapping resource="org/haiwen/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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="userDemo" extends="struts-default" namespace="/">
<!-- class属性不能写action全路径,写spring配置action对象bean标签的id值 -->
<action name="userAction" class="userAction"></action>
</package>
</struts>
UserAction
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
@Override
public String execute() {
System.out.println("action运行--------------------");
userService.save();
return NONE;
}
}
UserService
@Transactional
public interface UserService {
public void save();
}
UserServiceImpl
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;
@Override
public void save() {
System.out.println("service运行--------------------");
userDao.save();
}
}
UserDao
public interface UserDao {
public void save();
}
UserDaoImpl
@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {
@Autowired
@Qualifier("hibernateTemplate")
private HibernateTemplate hibernateTemplate;
@Override
public void save() {
System.out.println("dao运行--------------------");
User user = new User();
user.setUserName("马飞飞");
hibernateTemplate.save(user);
}
}
User
public class User {
private Integer userId;
private String userName;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + "]";
}
}
User.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>
<class name="org.haiwen.entity.User" table="user">
<id name="userId" column="user_id">
<generator class="native" />
</id>
<property name="userName" type="java.lang.String" column="user_name" />
</class>
</hibernate-mapping>
访问地址:http://localhost:8080/SSH/userAction.action,调用dao层方法执行。
HibernateTemplate介绍
HibernateTemplate对hibernate框架进行封装,直接调用HibernateTemplate里面的方法实现功能。HibernateTemplate常用的方法:
- Serializable save(Object entity):添加
- void update(Object entity):修改
- void delete(Object entity):删除
- <T> T get(Class<T> entityClass, Serializable id):根据id查询
- <T> T load(Class<T> entityClass, Serializable id):根据id查询