一、概述:
所谓ssm框架,就是Spring、SpringMVC和Mybatis框架的集合。
二、对web.xml的配置
1、配置样式如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--Spring核心监听器--> <!--负责启动spring容器的监听器,它将引用上下文参数获得sprng配置文件applicationContext.xml的地址,--> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--配置由Spring提供的针对中文乱码的编码过滤器--> <!--编码过滤器--> <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> <!--配置SpringMVC核心控制器--> <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:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!--配置首页--> <welcome-file-list> <welcome-file>/login.jsp</welcome-file> </welcome-file-list> </web-app>
2、启动spring容器的具体步骤,如下:
- 当web容器启动时,监听器开始自动运行
- 它会根据contextConfigLocation参数获取spring配置文件,并启动spring容器
3、web.xml的加载过程
- 启动一个web项目的时候,web项目会去读取它的配置文件web.xml,读取<listener>和<context-param>两个标签节点。
- 紧接着,容器创建一个ServletContext(即Servlet上下文),这个web项目的所有代码以及配置文件都将共享这个ServletContext(可以是mybatis配置文件,也可以是servlet配置文件)
- 容器将<context-param>转换为键值对,并将其交给servletContext,即加载这个配置文件
- 容器创建<listener>中的类实例,创建监听器。
三、对ApplicationContext.xml的配置
1、对数据源以及数据源文件的配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 数据库的驱动程序 --> <property name="driverClass" value="${db.driverClass}" /> <!-- 数据库的连接地址 --> <property name="jdbcUrl" value="${db.url}" /> <!-- 数据库的用户名 --> <property name="user" value="${db.user}" /> <!-- 数据库的连接密码 --> <property name="password" value="${db.password}" /> <!-- 数据库最大的可用连接数目 --> <property name="maxPoolSize" value="1" /> <!-- 数据库最小的可用连接数目 --> <property name="minPoolSize" value="1" /> <!-- 初始化的连接数目 --> <property name="initialPoolSize" value="1" /> <!-- 连接如果已经满了之后等待空闲连接的时间 --> <property name="maxIdleTime" value="20" /> </bean> <context:property-placeholder location="classpath:database.properties" />
2、配置基于数据源的DataSourceTransactionManager事务管理器,该事务管理器负责基于注解的声明式事务的管理
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 定义所有事务的传统属性,这些方法都属于服务层的操作方法 --> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="login*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="new*" propagation="REQUIRED" /> <tx:method name="set*" propagation="REQUIRED" /> <tx:method name="change*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="exists*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <aop:pointcut expression="execution(* cn.ylcto..service..*.*(..))" id="txPointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config>
备注: 1、配置数据事务管理,还有编程式的配置方式。故事务管理作为一个切面织入目标业务方法的周围,业务方法完全从事务代码中解脱出来,代码的复杂度大大降低。
2、配置数据事务管理,Spring还提供了基于xml的事务配置。
3、@Transactional 注解一般被建议在业务实现类上使用,因为注解不能被继承。
4、基于注解的配置方式涉及到三个角色:通过这种aop/tx定义的声明式事务配置信息、业务bean和spring容器。IOC容器中的bean进行事务管理配置定义,再由spring将这些配置织入对应的bean中。
3、配置事务工厂
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean>