web-springMVC+Hibernate配置

配置文件有四个
web.xml(在WEB-INF目录下)、
jci-servlet(在WEB-INF目录下,jci是你的项目名字)、
applicationContext.xml(在src目录下)、
jdbc.properties(在src目录下)

—————–web.xml—————————

<?xml version="1.0" encoding="UTF-8"?><!-- 具体配置可参考《Spring.3.x企业应用开发实战》书的第15章 -->
<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>jci</display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    <!-- 1处从此开始 。1处是 业务层和持久层的Spring配置文件,这些文件被父容器所使用-->
<!-- 从类路径下加载Spring配置文件,classpath关键字特指类路径下加载 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
<!--    负责启动Spring容器的监听器,他将引用上面的上下文参数获得Spring配置文件的地址 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!-- /1处到此截止 -->
<!--    SpringMVC的主控servlet -->
    <servlet><!-- 此处声明DispatcherServlet(即前置控制器) -->
        <servlet-name>jci</servlet-name><!-- 此处配置了名为jci的DispatcherServlet,
        它默认自动加载WEB-INF/jci-servlet.xml 下的spring配置文件,启动web层的Spring容器-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- SpringMVC处理的URL -->
    <servlet-mapping><!-- servlet-mapping指定了DispatcherServlet处理的所有URL都是url-pattern里的类型,
    如以html为后缀的HTTP请求都会被DispatcherServlet截获-->
        <servlet-name>jci</servlet-name>
        <url-pattern>/</url-pattern><!--  <url-pattern>*.html</url-pattern> -->
    </servlet-mapping>
</web-app>

—————-jci-servlet———————————

<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 扫描 base-package里的所有类,让标注spring 注解的类生效
    base-package里的类是需要扫描的ctrl层的名字,因为ctrl层就是处理前端action的类 -->
    <context:component-scan base-package="com.ctrl,com.sys.ctrl" />
    <!-- 视图解析器,把视图逻辑名解析为/JSP/xxx.jsp的ModelAndView视图对象,
    如你的ctrl层只需写上mav.setViewName("login");那么此处就会自动解析为返回/JSP/login.jsp页面-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/JSP/" p:suffix=".jsp" />

    <mvc:annotation-driven></mvc:annotation-driven>

    <mvc:default-servlet-handler />
    <!-- 实现SpringMVC的注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 用于文件上传 设置最大支持200M-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
        p:defaultEncoding="UTF-8"
        p:maxUploadSize="209715200"/><!-- 单位是字节 -->
</beans>

—————–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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
    default-autowire="byName">

    <context:annotation-config />

    <!-- 扫描类包以启动注解驱动的Bean -->
    <context:component-scan base-package="com.biz,com.ctrl,com.dao,com.po,com.sys"/>
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 1、数据源,使用C3P0数据源实现 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close"
        p:driverClass="${jdbc.driverClassName}"
        p:jdbcUrl="${jdbc.url}"
        p:user="${jdbc.username}"
        p:password="${jdbc.password}"
        p:initialPoolSize="1"
        p:maxPoolSize="50"
        p:maxIdleTime="3600"
        p:idleConnectionTestPeriod="3600"/>

    <!-- 2、配置SessionFactory、映射信息、Hibernate属性 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:packagesToScan="com.po,com.sys.po">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
                <prop key="hibernate.jdbc.batch_size">30</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            </props>
        </property>
    </bean>

    <!-- 3、基于数据源的事务管理器 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory"/>

    <!-- 4、事务的驱动注解,使程序能根据@Transactional来生成事务代理 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

———————jdbc.properties———————————–

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.1.30.207:3306/jci?characterEncoding=utf8
jdbc.username=guest
jdbc.password=guest

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值