SSM配置文件整合(Spring-SpringMVC-MyBatis)
2018-09-12 14:13
495 查看
mybatis-config.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver <!-- useOldAliasMetadataBehavior=true代表支持获取列别名获取 --> <!-- autoReconnect=true支持数据库中断后,自动重连 --> <!-- failOverReadOnly=false自动重连成功后,连接是否设置为只读? --> jdbc.url=jdbc:mysql://localhost:3306/person?useOldAliasMetadataBehavior=true&autoReconnect=true& failOverReadOnly=false&characterEncoding=utf-8 jdbc.username=admin jdbc.password=admin
spring-mybatis.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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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/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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 自动扫描 加载service --> <context:component-scan base-package="com.wescr.service.*" /> <!-- 加载配置文件,多个配置文件,使用list --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:project.properties</value> </list> </property> </bean> <!-- 数据库连接及配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="filters" value="config" /> <!-- <property name="connectionProperties" value="config.decrypt=true" /> --> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="20" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="proxyFilters"> <list> <ref bean="stat-filter" /> </list> </property> </bean> <!-- SQL监控信息 --> <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"> <property name="slowSqlMillis" value="10000" /> <property name="logSlowSql" value="true" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描配置的包下的文件 --> <property name="mapperLocations" value="classpath:com/wescr/mybatis/mapper/*.xml"/> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wescr.mybatis.dao" /> </bean> <!-- 注解式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <!-- XML配置事务 --> <!-- 定义事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 定义方法的过滤规则 --> <tx:attributes> <!-- 所有方法都使用事务 --> <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <!-- 定义所有get开头的方法都是只读的 --> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 定义AOP配置 --> <aop:config> <!-- 定义一个切入点 --> <aop:pointcut expression="execution (* com.wescr.ser 20000 vice.impl.*.*(..))" id="services"/> <!-- 对切入点和事务的通知,进行适配 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="services"/> </aop:config> </beans>
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-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 "> <!-- 自动扫描 加载controller --> <context:component-scan base-package="com.wescr.controller.*" /> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 指定所上传文件的总大小不能超过5000KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="52428800"/> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="2048"/> <property name="defaultEncoding" value="utf-8" /> </bean> <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException --> <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop> </props> </property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- servlet的描述信息,在应用服务器控制台上可以作为标志 --> <display-name>com-hurongclub-credit-api</display-name> <!-- 欢迎页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 设置Spring容器加载配置文件路径 (主要配置都在这里面) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.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>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Druid监控的Servlet --> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- 错误页面 --> <error-page> <error-code>404</error-code> <location>/error404.jsp</location> </error-page> </web-app>阅读更多
相关文章推荐
- SSM (IDEA) —— Spring,MyBatis和SpringMVC整合配置文件
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合 -- Spring和Mybatis的配置文件详解
- Spring详解之四:SpringMVC整合Mybatis相关配置文件信息
- spring、springMVC、mybatis整合配置文件问题
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SpringMVC中spring整合mybatis配置文件 applicationContext-mybatis.xml
- SSM整合(三) -- Spring和Mybatis的配置文件详解
- Spring ,Springmvc,mybatis三大框架整合的配置文件支持
- Mybatis整合Spring和SpringMVC配置文件详解
- spring、springmvc、mybatis整合文件配置详解
- SSM简单整合(Mybatis,Spring,SpringMVC,基于注解和xml文件配置)
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM整合配置文件(Springmvc+Spring+Mybatis)