Spring+Struts2+Hibernate

SSH项目环境搭建

资源包路径(自取)https://pan.baidu.com/s/1oXahoke596ymCQQKQHgnvw  提取码: myep 

一、HIbernate环境+Spring的环境搭建(笔者将放到同一个xml中 有注释)

1、applicationContext.xml(Hibernate负责数据库的交接  Spring负责使用基本的JavaBean代替EJB)

<?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 ">
	
	<!-- 开启属性注入的注解 -->
	<context:annotation-config/>
	
	<!-- 第二种方式通过context标签来引入properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置c3p0连接池 -->	 
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		 	<property name="driverClass" value="${jdbc.driverClass}"></property>
		 	<property name="jdbcUrl" value="${jdbc.url}"></property>
		 	<property name="user" value="${jdbc.username}"></property>
		 	<property name="password" value="${jdbc.password}"></property>
	</bean>	 
	<!-- 配置sessionFactory -->
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource"/> 
		<!-- 配置Hibernate的属性 -->
		<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>
		
		<!-- 引入映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/itheima/crm/domain/User.hbm.xml</value>
				<value>com/itheima/crm/domain/Customer.hbm.xml</value>
				<value>com/itheima/crm/domain/BaseDict.hbm.xml</value>
				<value>com/itheima/crm/domain/LinkMan.hbm.xml</value>
				<value>com/itheima/crm/domain/SaleVisit.hbm.xml</value>
			</list>
		</property>
	</bean>		 



	<!-- 配置UserAction --   开始Spring环境配置>
	<bean id="userAction" class="com.itheima.crm.web.action.UserAction">
		<property name="userService" ref="userService"></property>
	</bean>
	<!-- 配置Service -->
	<bean id="userService" class="com.itheima.crm.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- 配置Dao -->
	<bean id="userDao" class="com.itheima.crm.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- Customer的配置 -->
	<bean id="customerAction" class="com.itheima.crm.web.action.CustomerAction">
		<property name="customerService" ref="customerService"></property>
	</bean>
	<bean id="customerService" class="com.itheima.crm.service.impl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"></property>
	</bean>
	<bean id="customerDao" class="com.itheima.crm.dao.impl.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置字典管理相关类 -->
	<bean id="baseDictAction" class="com.itheima.crm.web.action.BaseDictAction">
		<property name="baseDictService" ref="baseDictService"></property>
	</bean>
	<bean id="baseDictService" class="com.itheima.crm.service.impl.BaseDictServiceImpl">
		<property name="baseDictDao" ref="baseDictDao"></property>
	</bean>
	<bean id="baseDictDao" class="com.itheima.crm.dao.impl.BaseDictDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置联系人的相关类 -->
	<bean id="linkManAction" class="com.itheima.crm.web.action.LinkManAction">
		<property name="linkManService" ref="linkManService"></property>
		<property name="customerService" ref="customerService"></property>
	</bean>
	<bean id="linkManService" class="com.itheima.crm.service.impl.LinkManServiceImpl">
		<property name="linkManDao" ref="linkManDao"></property>
	</bean>
	<bean id="linkManDao" class="com.itheima.crm.dao.impl.LinkManDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置客户拜访记录 -->
	<bean id="saleVisitAction" class="com.itheima.crm.web.action.SaleVisitAction">
	
	</bean>
	
	<bean id="saleVisitService" class="com.itheima.crm.service.impl.SaleVisitServiceImpl">
	</bean>
	
	<bean id="saleVisitDao" class="com.itheima.crm.dao.impl.SaleVisitDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置核心事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///crm
jdbc.username=root               //本地数据库用户名
jdbc.password=123456            //本地数据库的密码  

 web.xml

<!-- 一直保持这个Session -->
  <filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSessionInView</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  <!-- Spring的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 加载Spring的配置文件的路径 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>

二、struts2的配置

<!-- 设置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>

struts.xml笔者可以自己设置,博主就不在这展示了

三、书写过程中会遇到的

1、Spring中的属性注入的注解

@Service() 相当于<bean></bean>

@Controller :web层 相当于<bean></bean>

@Reponsitory :dao层 相当于<bean></bean>

 

注释内部对象的两个形式

@Autowired @Qualifier("userDao")

@Resource(name="userDao")

 

Bean的生命周期

@PostConstruct //相当于init_method

@PreDestroy //相当于destory-method

 

Bean的作用范围的注解

@Scope :作用范围

singleton :默认单例

prototype :多例

request

session

globalsession

 

2、在处理事务层的时候需要在类中加入@Transactional

事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃货智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值