码迷,mamicode.com
首页 > 其他好文 > 详细

力所能及之SSH环境搭建

时间:2015-04-26 16:47:22      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:ssh环境搭建   spring事务   

小狼最近心血来潮,搭建了一个SSH环境,跟大家分享分享

开发环境:Windows7 ,jdk1.6  myeclispe8.6  tomcat6.0.18   mysql5.0


需要jar文件:

    junit:junit.jar

    db:mysql-connector-java-5.1.10-bin.jar

      hibernate3.5.6:技术分享

     jstl:                         技术分享

      spring2.5.5:        技术分享

       struts2:                技术分享


  工程目录:

                      技术分享

配置文件:

web.xml

   

 <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<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>
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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       <import resource="applicationContext-db.xml"/>
       <import resource="applicationContext-user.xml"/>
       
</beans>
applicationContext-db.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     	<property name="configLocation">
     		<value>classpath:hibernate.cfg.xml</value>
     	</property>
     </bean> 
     
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     	<property name="sessionFactory">
     		<ref bean="sessionFactory"/> 
     	</property>
     </bean>
     <tx:advice transaction-manager="transactionManager" id="tx">
     	<tx:attributes>
     		<tx:method name="save*" read-only="false"/>
     		<tx:method name="update*" read-only="false"/>
     		<tx:method name="delete*" read-only="false"/>
     		<tx:method name="*" read-only="true"/>
     	</tx:attributes>
     </tx:advice>
     
     <aop:config>
     	<aop:pointcut expression="execution(* qh.zcy.service.*.*(..))" id="perform"/>
     	<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
     </aop:config>   
applicationContext-user.xml

 
       <bean id="userDao" class="qh.zcy.dao.UserDao">
          <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       <bean id="userService" class="qh.zcy.service.UserService">
          <property name="dao" ref="userDao"></property>
       </bean>
       <bean id="userAction" class="qh.zcy.action.UserAction" scope="prototype">
           <property name="service" ref="userService"></property>
       </bean>
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="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/zhang
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	
	<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</property>
	<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	<mapping resource="qh/zcy/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.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
   <!-- 配置文件改了以后不用重新启动 -->
   <constant name="struts.devMode" value="true"/>
   
   
   
   <package name="SSH" extends="struts-default" namespace="/">
         <action name="userAction_*" class="userAction" method="{1}User"></action>
   
   </package>
</struts>	

UserAction.jave

package qh.zcy.action;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;

import qh.zcy.entity.User;
import qh.zcy.service.UserService;

@Transactional(propagation=Propagation.REQUIRED)
public class UserAction extends ActionSupport{

	private UserService service;

	public UserService getService() {
		return service;
	}

	public void setService(UserService service) {
		this.service = service;
	}
	
	
	public String listUser() throws Exception {
		// TODO Auto-generated method stub
		System.out.println(service.getUsers());
		return null;
	}
	public String saveUser() throws Exception {
		// TODO Auto-generated method stub
		User user=new User();
		user.setPassword("zhang");
		user.setUsername("zhang");
		service.saveUser(user);
		return null;
	}

}

至此SSH环境搭建完毕,测试一下:

http://localhost:8080/SSH/userAction_save.action

控制台输出如下:

技术分享

力所能及之SSH环境搭建

标签:ssh环境搭建   spring事务   

原文地址:http://blog.csdn.net/zhanglu1236789/article/details/45288679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!