1.配置Spring
applicationContext.xml中:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:util="http://www.springframework.org/schema/util" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 8 xsi:schemaLocation= 9 "http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.2.xsd 13 http://www.springframework.org/schema/util 14 http://www.springframework.org/schema/util/spring-util-3.2.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 17 http://www.springframework.org/schema/tx 18 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 19 20 <!-- 开启包扫描 --> 21 <context:component-scan base-package="cn.tedu.service"/> 22 23 <!--引入外部配置文件 --> 24 <context:property-placeholder location="classpath:/jdbc.properties"/> 25 26 <!--配置数据源 --> 27 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 28 <property name="driverClass" value="${jdbcDriver}"/> 29 <property name="jdbcUrl" value="${jdbcUrl}"/> 30 <property name="user" value="${jdbcUser}"/> 31 <property name="password" value="${jdbcPassword}"/> 32 </bean> 33 34 <!--配置事务管理器 --> 35 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 36 <property name="dataSource" ref="dataSource"></property> 37 </bean> 38 39 <!--配置事务通知 --> 40 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 41 <tx:attributes> 42 <tx:method name="add*" propagation="REQUIRED"/> 43 <tx:method name="delete*" propagation="REQUIRED"/> 44 <tx:method name="update*" propagation="REQUIRED"/> 45 <tx:method name="find*" propagation="SUPPORTS"/> 46 <tx:method name="*" read-only="true"/> 47 </tx:attributes> 48 </tx:advice> 49 50 <!--配置事务切面 --> 51 <aop:config> 52 <aop:pointcut expression="execution(* cn.tedu.service..*.*(..))" id="pc"/> 53 <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/> 54 </aop:config> 55 <beans>
2.配置SpringMVC
配置applicationContext-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation= 7 "http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 13 <!--开启包扫描 --> 14 <context:component-scan base-package="cn.tedu.controller"/> 15 <!--开启MVC注解 --> 16 <mvc:annotation-driven/> 17 <!--配置视图解析器 --> 18 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix" value="/WEB-INF/"></property> 20 <property name="suffix" value=".jsp"></property> 21 </bean> 22 </beans>
配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name></display-name> 8 9 <!--配置前端控制器 --> 10 <servlet> 11 <servlet-name>springmvc</servlet-name> 12 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 13 <init-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:/applicationContext*.xml</param-value> 16 </init-param> 17 </servlet> 18 19 <servlet-mapping> 20 <servlet-name>springmvc</servlet-name> 21 <url-pattern>*.action</url-pattern> 22 </servlet-mapping> 23 24 25 <!--中文乱码解决 --> 26 <filter> 27 <filter-name>characterEncoding</filter-name> 28 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 29 <init-param> 30 <param-name>encoding</param-name> 31 <param-value>utf-8</param-value> 32 </init-param> 33 </filter> 34 35 <filter-mapping> 36 <filter-name>characterEncoding</filter-name> 37 <url-pattern>/*</url-pattern> 38 </filter-mapping> 39 40 41 <welcome-file-list> 42 <welcome-file>index.jsp</welcome-file> 43 </welcome-file-list> 44 </web-app>
3.配置Mybatis
配置sqlMapConfig,xml
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 <!--添加缓存 引入第三方二级缓存 --> 7 8 </configuration>
配置applicationContext.xml
1 <!--整合Mybaits --> 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 3 <!--引入数据源 --> 4 <property name="dataSource" ref="dataSource"></property> 5 6 <!--引入核心配置文件 --> 7 <property name="configLocation" value="classpath:/sqlMapConfig.xml"/> 8 9 <!--引入映射文件 --> 10 <property name="mapperLocations" value="classpath:/cn/tedu/pojo/*.xml"/> 11 </bean>
创建bean,并同时创建bean的映射文件:
1 User类: 2 public class User { 3 private Integer id; 4 private String name; 5 private Integer age; 6 private String sex; 7 … 8 } 9 UserMapper.xml 10 <?xml version="1.0" encoding="UTF-8" ?> 11 <!DOCTYPE mapper 12 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 13 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 14 <mapper namespace="cn.tedu.mapper.UserMapper"> 15 16 17 <select id="findAll" resultType="cn.tedu.pojo.User"> 18 select * from user 19 </select> 20 <select id="findOne" resultType="cn.tedu.pojo.User"> 21 select * from user where id = #{id} 22 </select> 23 24 25 </mapper>
创建bean的mapper接口:
1 package cn.tedu.mapper; 2 3 import java.util.List; 4 5 import cn.tedu.pojo.User; 6 7 public interface UserMapper { 8 public List<User> findAll(); 9 public List<User> findOne(int id); 10 }
为了能让Mybatis扫描识别bean的Mapper类,需要在Spring再注册一个扫描器bean,扫描指定包:
在applicationContext.xml中:
SSM框架搭建完成