ssh框架整合
一.思路
1.导包
struts2: \apps\struts2-blank\WEB-INF\lib\所有包
struts2-spring-plugin-2.3.28.jar
hibernate: \lib\required\所有包
\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final(持久化规范,接口)
hibernate3.jar
slf4j-api.jar
slf4j-log4j12.jar
spring: 4+2个:libs目录\ beans|context|core|expression|
commons-logging|log4j
aop包4个:libs目录\ spring-aop|spring-aspect
aopalliance(联盟包)|aspectj.weaver.jar(织入包)
jdbc+事务:spring-jdbc|spring-tx|spring-orm|c3p0
web包:spring-web
数据库驱动包: mysql-connector-java.jar
注:总共目前先用的包是38个包
2.创建配置文件
struts
hibernate
spring
3.整合hibernate
三个重要的对象
1)HibernateTemplate ,操作我们po类(可以配置bean也可以继承HibernateDaoSupport)
2)LocalSessionFactoryBean:创建sessionFactory
3)HibernateTransactionManager:hibernate管理事务
李老师讲了两种方法,第一种是不用c3po数据源,另外一种使用c3p0数据源。
二 整合(c3p0数据源)
2.0配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>myssh_2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring的监听 用于启动applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext2.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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>
2.1 配置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>
<constant name="struts.devMode" value="true"></constant>
<!-- 把Action实列交由spring来管理,这个常量必须配置 -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="" namespace="/" extends="struts-default">
<!-- 把Action创建交于spring容器去创建,class:名字对应的是applicationContext.中,要配置的 Action的bean的名字 -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="success" type="redirect">/success.jsp</result>
</action>
</package>
</struts>
2.2 hibernate.cfg.xml和User.hbm.xml
说明:此时我们是用Spring去整合hibernate,这里不用配置
配置User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.icss.pojo">
<class name="User" table="user">
<id name="uid">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="pwd"></property>
</class>
</hibernate-mapping>
2.3 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 是要引用外部properties文件来使用 c3p0连接池 -->
<!-- 0.1读取properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 0.2配置c3po -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 1.整合hibernate-->
创建sessionFactory
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 连接数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- spring 代替 hibernate.cfg.xml文件 -->
<property name="hibernateProperties">
<props>
<!-- 切记key要写全路径 hibernate. xxx-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.autoReconnect">true</prop>
<!-- 建议 不加 -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>数据源提供商
</props>
</property>
<!-- mapping 映射文件引入方式有两种 -->
<!-- 1单个文件引入 -->
<property name="mappingResources" value="com/icss/pojo/User.hbm.xml"></property>
<!-- 2 全部引入 -->
<!-- <property name="mappingDirectoryLocations" value="classpath:cn/icss/pojo"></property> -->
</bean>
<!-- 3配置dao -->
<bean name="userDao" class="com.icss.dao.impl.UserDaoImpl2">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4配置service -->
<bean name="userService" class="com.icss.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 5配置action -->
<bean name="userAction" class="cn.icss.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
hibernate管理事务
<!--6配置事物分为三步走 -->
<!-- 6.1核心事物管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 6.2配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 6.3切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.icss.service..*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
2.4 db.properties文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.user=root
jdbc.password=123456
2.5. UserDaoImpl
public class UserDaoImpl2 extends HibernateDaoSupport implements UserDao {
@Override
public void save(User u) {
getHibernateTemplate().save(u);
}
@Override
public void update(User u) {
getHibernateTemplate().update(u);
}
@Override
public void delete(User u) {
getHibernateTemplate().delete(u);
}
@Override
public User findById(Integer uid) {
User user = getHibernateTemplate().get(User.class, uid);
return user;
}
@Override
public List<User> findAll() {
String hql="from User";
List<User> list = (List<User>) getHibernateTemplate().find(hql);
return list;
}
}
2.6 UserServiceImpl
public class UserServiceImpl implements UserService {
private UserDao userDao; //set方式注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save(User u) {
userDao.save(u);
}
@Override
public void update(User u) {
userDao.update(u);
}
@Override
public void delete(User u) {
userDao.delete(u);
}
@Override
public User findById(Integer uid) {
return userDao.findById(uid);
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
2.6 UserAction
public class UserAction extends ActionSupport implements ModelDriven<User> {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
//声明User对象(一定实例化)
private User user = new User();
//实现用户的添加功能
public String add() throws Exception {
this.userService.save(user);
return "success";
}
@Override
public User getModel() {
return user;
}
}
2.6 Pojo(User.java)
public class User {
private Integer uid;
private String name;
private String pwd;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uid=" + uid + ", name=" + name + ", pwd=" + pwd + "]";
}
}