SSH框架搭建

1.spring + hibernateORM

application-data.xml 配置文件

1.jar包的引入

2.扫描dao, service层

   将数据源(连接池)和sessionFactory放入springIOC容器

3.将sessionFactory委托给我们的spring进行事务管理

<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- 从配置文件当中获取链接数据库的相关信息 
		 需要使用${key}的形式来进行获取
	-->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 使用spring的扫描 对service和dao进行扫描
		 contorller交给SpringMVC管理,所以将contorller层剔除
	 -->
	<context:component-scan base-package="com.lanou">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 从连接池获取数据源 -->
	<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.user}"></property>
		<property name="password" value="${jdbc.password}"></property>	
	</bean>
	
	<!-- 使用spring来管理sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 获取数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置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="mappingLocations" value="classpath:com/lanou/entity/*.hbm.xml"></property>
	</bean>
	
	<!-- 事务管理 -->
	<!-- 1.创建事务管理者,将sessionFactory交给他管理 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 事务驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>	
	<!-- aop注解 -->
	<aop:aspectj-autoproxy />
</beans>

2.springMVC整合进来

springMVC-servlet.xml

1.导入springMVC所用的包

2.编写springMVC的配置文件 扫描controller

3.注意: 在web.xml当中 设置DispatcherServlet这个servlet

   来保证springMVC可以运行工作

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	
													<!-- 用include时 将默认扫描所有文件改为false -->
	<!-- <context:component-scan base-package="com.lanou" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan> -->
	<context:component-scan base-package="com.lanou.controller"></context:component-scan>
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    	<!-- prefix的value + return的字符串 + suffix的value -->
	    	<!-- /resource/views/index.jsp -->
	    <property name = "prefix" value="/resource/views/"></property><!--视图的路径 -->
	    <property name = "suffix" value = ".jsp"></property><!-- 视图的格式 -->
    </bean>
    <!-- 静态资源的处理 js css img -->
    <mvc:resources location="/resource/" mapping="/resource/**"></mvc:resources>
	<!-- 启动注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 拦截器 -->
	<mvc:interceptors>
		<bean class="com.lanou.interceptor.LoginInterceptor"></bean>
		<mvc:interceptor>
			<mvc:mapping path="/user/*"/>
			<mvc:exclude-mapping path="/user/login"/>
			<bean class="com.lanou.interceptor.SecondInterceptor"></bean>
		</mvc:interceptor>
		
	</mvc:interceptors>
	
	<!-- 文件上传配置 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<property name="maxUploadSize" value="5242880"></property>
    		<property name="defaultEncoding" value="UTF-8"></property>
    		<property name="resolveLazily" value="true" ></property>
    	</bean>
	
</beans>

3.配置web.xml   设置Spring的监听器

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>SpringMVCsh</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>
  
  <!-- 设置供Spring读取的配置文件 -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application*.xml</param-value>
  </context-param>
  <!-- 监听器 帮我们进行配置文件的读取
  	   向IOC容器注入bean	
   -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 只有设置了DispatcherServlet
  才能让SringMVC工作 帮我们管理请求 -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

SSH框架搭建用到的所有包:

链接: https://pan.baidu.com/s/1uGucDHW5An-utVE7-LsCTA 密码: jrgr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值