整合的配置
【web.xml】
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <!-- 字符编码过滤器 --> 4 <filter> 5 <filter-name>CharacterEncodingFilter</filter-name> 6 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 <init-param> 8 <param-name>encoding</param-name> 9 <param-value>UTF-8</param-value> 10 </init-param> 11 </filter> 12 <filter-mapping> 13 <filter-name>CharacterEncodingFilter</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 17 <!-- REST 过滤器 --> 18 <filter> 19 <filter-name>HiddenHttpMethodFilter</filter-name> 20 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 21 </filter> 22 <filter-mapping> 23 <filter-name>HiddenHttpMethodFilter</filter-name> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 27 <!-- 实例化SpringIOC容器的监听器 --> 28 <context-param> 29 <param-name>contextConfigLocation</param-name> 30 <param-value>classpath:applicationContext.xml</param-value> 31 </context-param> 32 33 <listener> 34 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 35 </listener> 36 37 <!-- Springmvc的前端控制器 --> 38 <servlet> 39 <servlet-name>springDispatcherServlet</servlet-name> 40 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 41 <init-param> 42 <param-name>contextConfigLocation</param-name> 43 <param-value>classpath:springmvc.xml</param-value> 44 </init-param> 45 <load-on-startup>1</load-on-startup> 46 </servlet> 47 <servlet-mapping> 48 <servlet-name>springDispatcherServlet</servlet-name> 49 <url-pattern>/</url-pattern> 50 </servlet-mapping> 51 </web-app>
Spring配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 7 xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 11 12 <!-- 组件扫描 --> 13 <context:component-scan base-package="com.atguigu.ssm"> 14 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 15 </context:component-scan> 16 17 <!-- 连接池 --> 18 <context:property-placeholder location="classpath:db.properties"/> 19 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 20 <property name="driverClass" value="${jdbc.driver}"></property> 21 <property name="jdbcUrl" value="${jdbc.url}"></property> 22 <property name="user" value="${jdbc.username}"></property> 23 <property name="password" value="${jdbc.password}"></property> 24 25 </bean> 26 27 <!-- 事务 --> 28 <bean id="dataSourceTransactionManager" 29 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 30 <property name="dataSource" ref="dataSource"></property> 31 </bean> 32 <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> 33 </beans>
SpringMVC配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 <!-- 组件扫描 --> 10 <context:component-scan base-package="com.atguigu.ssm" use-default-filters="false"> 11 <context:include-filter type="annotation" 12 expression="org.springframework.stereotype.Controller"/> 13 </context:component-scan> 14 <!--视图解析器 --> 15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 16 <property name="prefix" value="/WEB-INF/views/"></property> 17 <property name="suffix" value=".jsp"></property> 18 </bean> 19 20 <mvc:default-servlet-handler/> 21 <mvc:annotation-driven/> 22 </beans>
Mybatis配置
1.全局配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- Spring 整合 MyBatis 后, MyBatis中配置数据源,事务等一些配置都可以 7 迁移到Spring的整合配置中。MyBatis配置文件中只需要配置与MyBatis相关 8 的即可。 9 --> 10 <!-- settings: 包含很多重要的设置项 --> 11 <settings> 12 <!-- 映射下划线到驼峰命名 --> 13 <setting name="mapUnderscoreToCamelCase" value="true"/> 14 <!-- 设置Mybatis对null值的默认处理 --> 15 <setting name="jdbcTypeForNull" value="NULL"/> 16 <!-- 开启延迟加载 --> 17 <setting name="lazyLoadingEnabled" value="true"/> 18 <!-- 设置加载的数据是按需还是全部 --> 19 <setting name="aggressiveLazyLoading" value="false"/> 20 <!-- 配置开启二级缓存 --> 21 <setting name="cacheEnabled" value="true"/> 22 </settings> 23 </configuration>
2.SQL映射文件配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.atguigu.ssm.mapper.EmployeeMapper"> 7 <!-- public List<Employee> getAllEmps(); --> 8 <select id="getAllEmps" resultMap="myEmpsAndDept" > 9 select e.id eid, e.last_name,e.email,e.gender, d.id did, d.dept_name 10 from tbl_employee e ,tbl_dept d 11 where e.d_id = d.id 12 </select> 13 <resultMap type="com.atguigu.ssm.beans.Employee" id="myEmpsAndDept"> 14 <id column="eid" property="id"/> 15 <result column="last_name" property="lastName"/> 16 <result column="email" property="email"/> 17 <result column="gender" property="gender"/> 18 19 <association property="dept" javaType="com.atguigu.ssm.beans.Department"> 20 <id column="did" property="id"/> 21 <result column="dept_name" property="departmentName"/> 22 </association> 23 </resultMap> 24 </mapper>
Spring整合Mybatis配置
1 <!-- Spring 整合 Mybatis --> 2 <!--1. SqlSession --> 3 <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 4 <!-- 指定数据源 --> 5 <property name="dataSource" ref="dataSource"></property> 6 <!-- MyBatis的配置文件 --> 7 <property name="configLocation" 8 value="classpath:mybatis-config.xml"></property> 9 <!-- MyBatis的SQL映射文件 --> 10 <property name="mapperLocations" 11 value="classpath:mybatis/mapper/*.xml"></property> 12 <property name="typeAliasesPackage" 13 value="com.atguigu.ssm.beans"></property> 14 </bean> 15 <!-- Mapper接口 16 MapperScannerConfigurer 为指定包下的Mapper接口批量生成代理实现类.bean的默认id是接口名首字母小写. 17 --> 18 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 19 <property name="basePackage" value="com.atguigu.ssm.mapper"></property> 20 </bean> 21 <!-- <mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/> -->
每接触一个新领域,我就像一块掉进水里的海绵,四面八方的养分都让我不断充实。O(∩_∩)O~