spring+hibernate

一 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>CharacterEncodingFilter</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>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

二:springmvc-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />
    <!-- scan the package and the sub package -->
    <context:component-scan base-package="com.scan" />

    <!-- don't handle the static resource <mvc:default-servlet-handler /> -->


    <!-- 配置velocity引擎 -->
    <bean id="velocityConfigurer"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/view/" />
        <property name="configLocation" value="classpath:velocity.properties" />
    </bean>
    <!-- 配置视图的显示 -->
    <bean id="ViewResolver"
        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <!-- <property name="prefix" value="" />视图文件的前缀,即存放的路径 -->
        <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
    <!-- 设置上传文件 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5400000" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="uploadTempDir" value="fileUpload/temp" />
    </bean>
</beans>

三:applicationContext-common.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
	- Application context definition for JPetStore's business layer.
	- Contains bean references to the transaction manager and to the DAOs in
	- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 配置sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="configLocation">
			<!-- 在此定义Hibernate文件路径-->
			<value>classpath:hibernate.cfg.xml</value>
		</property>
		<!-- <property name="current_session_context_class">
			<value>jta</value>
		</property> -->
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!--定义哪些方法需要加事务    配置事务传播特性 -->
	<tx:advice id="xjsmsAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="nis*" propagation="REQUIRED" /> 
			<tx:method name="nup*" propagation="REQUIRED" />
			<tx:method name="nde*" propagation="REQUIRED" />
			<tx:method name="nsa*" propagation="REQUIRED" />
			<tx:method name="nol*" propagation="REQUIRED" /> 
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
    
	<!--定义使用事务的方法的路径   那些类使用事务-->
	<aop:config>
		<aop:pointcut id="xjsms"
			expression="execution(* com.scan.action.*.*(..))" />
		<aop:advisor advice-ref="xjsmsAdvice"
			pointcut-ref="xjsms" />
	</aop:config>
</beans>

四:applicationContext-bean.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- - Application context definition for JPetStore's business layer. - Contains
    bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml
    (see web.xml's "contextConfigLocation"). -->
<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.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="loadMsg" class="com.scan.dao.work.SystemStartLoadMsg" init-method="loadSystemMsg"></bean>
    <bean id="sqlTool" class="com.util.SessionSqlTool"></bean>
</beans>
五:hibernate.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
    <session-factory>
        <!-- <property name="connection.username">sa</property> <property name="connection.url">
            jdbc:sqlserver://192.168.18.244:1433;databaseName=phone2 </property> <property
            name="connection.password">123456</property> -->

        <property name="connection.username">root</property>
        <property name="connection.url">
            jdbc:mysql://127.0.0.1:3306/xx?characterEncoding=UTF-8
        </property>
        <property name="connection.password">123456</property>
        <!-- 最大连接数 -->
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 最小连接数 -->
        <property name="hibernate.c3p0.min_size">5</property>
        <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
        <property name="hibernate.c3p0.timeout">120</property>
        <!-- 最大的PreparedStatement的数量 -->
        <property name="hibernate.c3p0.max_statements">100</property>
        <!-- 每隔180秒检查连接池里的空闲连接 ,单位是秒 -->
        <property name="hibernate.c3p0.idle_test_period">180</property>
        <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
        <property name="hibernate.c3p0.acquire_increment">3</property>
        <!-- 自动释放连接 -->
        <property name="hibernate.connection.release_mode">auto</property>
        <!-- 每次都验证连接是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>
        <property name="jdbc.use_scrollable_resultset">true</property>

        <!-- 新加 <property name="testConnectionOnCheckin">true</property> <property
            name="idleConnectionTestPeriod">60</property> <property name="connection.pool_size">20</property>
            <property name="autoReconnect">true</property> 每隔60检查连接是否可用 <property name="idleConnectionTestPeriod">60</property> -->
        <property name="dialect">
            org.hibernate.dialect.MySQL5Dialect
        </property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <!--
        <property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
         -->
        <property name="hibernate.current_session_context_class">jta</property>
        <property name="hibernate.current_session_context_class">thread</property>
        
        <property name="myeclipse.connection.profile">
            MyEclipse Derby
        </property>
        <property name="show_sql">true</property>
        <!-- save批量清空缓存数 -->
        <property name="jdbc.batch_size">20</property>
        <!-- <property name="connection.autocommit">true</property> -->
        <mapping resource="com/lj/hxml/Webcategory.hbm.xml" />
    </session-factory>

</hibernate-configuration>


六:velocity.properties

#encoding  
input.encoding=UTF-8
output.encoding=UTF-8
  
#autoreload when vm changed  
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval=2
velocimacro.library.autoreload=false  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值