SSM(SpringMVC+Spring+MyBatis) 配置文件

来源:互联网 发布:算法c语言实现 编辑:程序博客网 时间:2024/05/16 10:56

一、spingmvc

1.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>IntimateCarCare</display-name>  <welcome-file-list>    <welcome-file>Login.jsp</welcome-file>  </welcome-file-list>  <!-- 配置一个Filter来解决请求,响应乱码 (POST请求)GET请求乱码在Servles中server.xml中修改-->  <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>    <!-- 是否覆盖代码中字符编码的设置 true为覆盖 -->    <init-param>        <param-name>forceEncoding</param-name>        <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>  <filter-name>encodingFilter</filter-name>  <url-pattern>/*</url-pattern> </filter-mapping>   <!-- spring配置文件加载 -->  <context-param>      <!-- 指定Spring上下文配置文件 -->    <param-name>contextConfigLocation</param-name>    <param-value>classpath:config/spring-*.xml</param-value>  </context-param>  <!-- Spring ApplicationContext 载入 -->  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 使用SpringMVC框架,配置一个前端控制器来拦截所有请求 -->  <servlet>    <servlet-name>springDispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <!--设置springMVC配置文件的位置-->        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>     </init-param>     <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springDispatcherServlet</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>

2.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: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.2.xsd        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">    <!-- 1.springmvc 注解驱动 -->    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>    <bean id="contentNegotiationManager"     class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">        <property name="favorPathExtension" value="false" />    </bean><!-- json转换 -->    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>      <bean id="stringConverter"          class="org.springframework.http.converter.StringHttpMessageConverter">          <property name="supportedMediaTypes">              <list>                  <value>application/json;charset=UTF-8</value>              </list>          </property>      </bean>      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">          <property name="messageConverters">              <list>                  <ref bean="stringConverter" />                  <ref bean="jsonConverter" />              </list>          </property>      </bean>     <!-- 2.当这个解析器解析不了的时候使用默认的servlet -->    <mvc:default-servlet-handler/>    <!--3.配置视图解析器  -->   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">     <!-- 前缀  -->     <property name="prefix" value="/WEB-INF/jsp/"></property>     <!--后缀 -->     <property name="suffix" value=".jsp"></property>   </bean>    <!--4. 扫描器 -->    <context:component-scan base-package="com.carcare.controller"/></beans>

该文件放在WEB-INF文件下面

二、mybatis

这里mybatis实现dao接口使用Mapper机制自动实现DAO接口

1.log4j.properties 日志文件

# Global logging configuration#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216errorlog4j.rootLogger=DEBUG, Console# Console output...log4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d [%t]%-5p [%c] - %m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG

此文件放在src根目录中

2.MyBatisConfig.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>    <settings>       <setting name="useGeneratedKeys" value="true"/>    </settings>    <plugins>    <plugin interceptor="com.carcare.interceptor.PageInterceptor">    </plugin>  </plugins></configuration>

此文件放在config包下

3.c3p0-config.properties

jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql://localhost:3306/intimatecarcare?useUnicode=true&characterEncoding=utf-8jdbc.user=rootjdbc.password=123456<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->   c3p0.acquireIncrement=3   <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->   c3p0.initialPoolSize=4  <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> c3p0.idleConnectionTestPeriod=60  c3p0.minPoolSize=3  c3p0.maxPoolSize=100  # JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements   #属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。   #如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0#c3p0.maxStatements=100  #c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过  #多线程实现多个操作同时被执行。Default: 3c3p0.numHelperThreads=10  <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0    c3p0.maxIdleTime=60  <!-- 超时时间设置 -->c3p0.checkoutTimeout=8000<!-- 重试次数 -->c3p0.acquireRetryAttempts=2

4.service-dao.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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">    <description>Spring dao配置</description>    <!-- 1.数据源配置, 使用c3p0 -->    <context:property-placeholder location="classpath:/config/c3p0-config.properties"/>    <!-- 2. -->     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- Connection Info -->        <property name="driverClass" value="${jdbc.driverClass}" />        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />        <property name="user" value="${jdbc.user}" />        <property name="password" value="${jdbc.password}" />          <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->          <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>         <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->           <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>          <property name="minPoolSize" value="${c3p0.minPoolSize}"/>          <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>          <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->           <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->            <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>         <!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements                               属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。                               如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->           <!-- <property name="maxStatements" value="${c3p0.maxStatements}"/>    -->        <!-- c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过                                    多线程实现多个操作同时被执行。Default: 3-->           <property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>         <!-- 超时时间设置 -->        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"></property>        <!-- 重试次数 -->        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"></property>    </bean>    <!-- 3.使用MyBatis操作数据库,替代以前使用的JdbcTemplate          spring与myBatis整合,给myBatis框架配置执行环境                            创建SqlSessionFactory,同时指定数据源     -->     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">         <!-- 使用ioc容器管理的数据源 -->        <property name="dataSource" ref="dataSource"></property>        <!-- 设置MyBatis全局配置文件所在的位置 -->        <property name="configLocation" value="classpath:config/mybatisConfig.xml"></property>        <property name="typeAliasesPackage" value="com.carcare.entity"></property>        <property name="mapperLocations" value="classpath:com/carcare/mapper/*.xml"></property>     </bean>     <!--  4.配置扫描Dao接口,动态实现Dao接口,注入到spring容器中 -->     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 注入sqlSessionFactory -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>        <!-- 给出需要扫描Dao接口包 -->        <property name="basePackage" value="com.carcare.dao" />    </bean> </beans>

三、Spring

1.spring-service.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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task"    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">    <description>Spring service配置</description>    <!-- 开启定时任务 -->    <task:annotation-driven/>    <!-- 扫描service下所有使用注解的类型 -->    <context:component-scan base-package="com.carcare.service"/>    <context:component-scan base-package="com.carcare.task" />      <!--使用Spring完成事务控制 -->    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <!-- 开启基于注解的事务管理默认使用注解来管理事物行为-->    <tx:annotation-driven transaction-manager="transactionManager"/>    <!-- 要想让Spring完成事务控制,必须使用Spring能操作的SqlSession,因此配置下面的SqlSessionTemplate                并使用上边配置好的工厂来获取SqlSessionTemplate对象                这个对象就是openSession拿到的那个对象,可以与数据库建立会话                  SqlSessionTemplate和我们openSession拿到的对象功能类似                 可以声明为属性,线程安全     -->    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />       <!-- 这个执行器会批量执行更新语句, 还有SIMPLE 和 REUSE -->       <!-- <constructor-arg index="1" value="BATCH" /> -->    </bean></beans>

注意springmvc和spring直接的版本,最好要一样的,否则可能会不兼容

另外还有springmvc接受和传输json数据,需要jackson jar包,jar包和spring的jar包也需要版本兼容,否则也会导致页面接受数据失败

下面这里就是我项目中的jar包,注意版本搭配
这里写图片描述这里写图片描述

这是项目结构
这里写图片描述这里写图片描述

0 0
原创粉丝点击