springMVC+spring+hibernate整合(1)

以前一直用ssh2做东西,最近一阵子忽然想学习一下springMVC。据说设计理念也很不错的。我是个实用主义者,啥也不说,看看书,立马开始动手。

1、web.xml

     这个是大家一开始都需要配置的

     

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
这个是我的初始化类,用来自动建立数据库的,可以不需要
   <servlet> <servlet-name>initialServlet</servlet-name> <servlet-class>com.news.servlet.InitialServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
//spring配置文件加载
  <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/applicationContext.xml,classpath:/applicationContext-*.xml </param-value> </context-param>
//spring监听的配置
  <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
//spring框架自带的字符集过滤(比自己写应该能省不少事)
  <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter>
//springMVC控制器的配置,初始化值中有配置文件路径
  <servlet> <servlet-name>SpringController</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/*-servlet.xml</param-value> </init-param> </servlet> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>SpringController</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> </jsp-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

 ok,web.xml完成了,我们来接着配置spring的一系列配置文件。

 2、applicationContext系列配置,我个人的风格喜欢将不同作用的配置分门别类的放到不同的配置文件中

       (1)applicationContext.xml,这个文件我仅做了一项配置,用于加载properties文件

      

<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
	<property name="ignoreInvalidKeys"  value="true"></property>
//如果没有匹配的属性,我们忽略它
 <property name="locations"> <list> <value> /WEB-INF/customerConfigs/applicationContext-*.properties </value> </list> </property> </bean>
//使用properties的方式来完成一些配置是为了方便,采用属性重写的方式来使用properties文件,如果properties文件中存在对应的属性,则采用properties文件中的值,否则,仍然使用xml中的值
  </beans>

    properties文件

   

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/filesystem
dataSource.user=root
dataSource.password=root
//前缀一定要对应bean 的 id 属性,是依靠ID来定位的
 jdbcUrlBase=jdbc:mysql://localhost:3306/?
这个就是不存在的属性了,我把它当一个值来用
 

    (2)和hibernate的整合

<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	<property name="driverClass" value="com.mysql.jdbc.Driver" />
	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/news"/>
	<property name="user" value="root"/>
	<property name="password" value="root"/>
	<property name="minPoolSize" value="5"/>
	<property name="maxPoolSize" value="60"/>
	<property name="initialPoolSize" value="3"/>
	<property name="maxIdleTime" value="60"/>
	<property name="acquireIncrement" value="10"/>
	<property name="idleConnectionTestPeriod" value="60"/>
	<property name="acquireRetryAttempts" value="30"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				org.hibernate.dialect.MySQLInnoDBDialect
			</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.jdbc.batch_size">30</prop>
			<prop key="hibernate.default_batch_fetch_size">30</prop>
			</props>
	</property>
	<property name="packagesToScan" value="com.news.po"></property>
</bean>
//使用注解方式来配置hibernate的,hbm文件实在有点多,不大方便,因为是初步配置,并没有加入2级缓存等一些功能,以后再补上
 <bean id="transactionManagerDAO" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>

    3、事务的代理,因为我个人觉得事务的管理是一个比较独立的东西,是基于AOP的,所以把一些AOP的配置放一块了

     

<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="logInterceptor" class="com.news.advice.LogInterceptor">
	</bean>
	
	<bean id="exceptionInterceptor" class="com.news.advice.ExceptionInterceptor"></bean>
                
//上面两个是做日志处理的,把日志分成error和info,分别记录,方便查看,下次再介绍
//下面就是事务的声明式管理了,事务的管理还是应该配置在service层的,很多初步教材都喜欢把这个写在DAO层,日志的处理究竟应该放在控制层还是service层,还在考虑中,暂时放在serivce层中了
<bean id="transactionInterceporDAO" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManagerDAO"></property> <property name="transactionAttributes"> <props> <prop key="get*,find*,check*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="daoAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>logInterceptor</value> <value>exceptionInterceptor</value> <value>transactionInterceporDAO</value> </list> </property> <property name="beanNames"> <list> <value>*Service</value> </list> </property> </bean> </beans>

    4、以下是spring对DAO和serivce的管理,这个比较简单,随便贴个代码

   

<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="baseDAO" class="com.news.dao.impl.BaseDAOImpl" abstract="true">
	<property name="hibTemplate" ref="hibernateTemplate"></property>
</bean>

//如果想把抽象类也交给spring管理,别忘了加上abstract属性
 <bean id="userDAO" class="com.news.dao.impl.UserDAOImpl"> <property name="hibTemplate" ref="hibernateTemplate"></property> </bean> </beans>

   

<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="userService" class="com.news.service.impl.UserServiceImpl">
		<property name="userDAO" ref="userDAO"></property>
		<property name="roleDAO" ref="roleDAO"></property>
	</bean>

</beans>

 

   5、加入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: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-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
	
//Ajax上传用,下次介绍
 <bean id="ajaxViewResolver" class="com.news.util.AjaxViewResolver"> <property name="ajaxPrefix" value="ajax_"></property> <property name="ajaxView"> <bean class="com.news.util.AjaxView"></bean> </property> </bean> <bean id="multipartResolver" class="com.news.util.AjaxFileUploadResolver"> <property name="maxUploadSize" value="1000000000"/> </bean>
//springMVC的视图解析,可以指定前缀和后缀
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> </bean>
//spring的异常处理,提供对用户比较友好的错误界面,可以指定不同的异常类型
  <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception"> jsp/errors/exception </prop> </props> </property> </bean>
//这种URL-mapping方式是spring的默认匹配方式,很直观
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="order" value="2"></property> </bean>
//springmvc中最常用的url-mapping方式,因为比较习惯通配符的做法,因此依照本人习惯,还是通配符,将匹配顺序置为第一。同时还可以给springMVC指定方法的解析模式,一种是采用指定参数的形式,另一种是根据url直接指定方法(所谓的属性匹配),我不用,写出来参考一下
  <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="order" value="1"/> <property name="mappings"> <props> <prop key="index.htm">indexInit</prop> <prop key="*SysAdmin.htm">sysAdminManage</prop> <prop key="login*.htm">login</prop> </props> </property> </bean> <bean id="methodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="method"></property> </bean> <bean id="propetyResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/jcrsSearch.htm">jcrsSearch</prop> <prop key="/jcrsSearch2.htm">jcrsSearch2</prop> </props> </property> </bean>
//下面就是控制器的配置了,上面的映射分别将路径对应到控制器中,由控制器来处理请求,在下面我也写了方法名解析的方式的使用,如果想使用这种方式的话,可以参考。因为我觉得一个请求就对应一个类在实际使用中其实很少用,所以,本着实用的想法,将一类请求对应相同的类,将contral全部继承MultiActionController
  <bean id="sysAdminManage" class="com.news.controller.SysAdminManageController"> <property name="userService" ref="userService"></property> </bean> <bean id="jcrs" class="com.news.controller.JcrTestTwoController"> <!-- <property name="methodNameResolver" ref="propetyResolver"></property> --> </bean> <bean id="indexInit" class="com.news.controller.MainPageController"> </bean> <bean id="upload" class="com.news.controller.JcrTestController"> <property name="commandClass" value="com.news.po.UpFile"></property> <property name="formView" value="jsp/jcrTest"></property> <property name="successView" value="jsp/jcrList"></property> <!-- <property name="jcrService" ref="jcrService"></property> --> </bean> <!-- login bean --> <bean id="login" class="com.news.controller.LoginController"> <property name="userService" ref="userService"></property> </bean> <!-- web control <bean id="indexControl" class="com.pjj.news.controller.MainPageController"> </bean>--> </beans>

 6、这样的话可以在你的index.jsp中写下

      <jsp:forward page="checkSysAdmin.htm"></jsp:forward>

     那么springmvc的映射将会将路径映射到你的控制器中,进行请求处理了

暂时就写这么多,如果大家有好的学习经验,可以多多交流啊

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值