SSM:框架整合 Spring SpringMVC Mybatis
1)Spring整合Mybatisa.配置数据源对象
b.配置SqlSessionFactory对象
注入configLocation(指定SqlMapConfig.xml路径)
注入数据源对象
c.配置Dao对象
注入SqlSessionFactory(SqlSessionDaoSupport需要)
d.配置Service对象
注入Dao对象
e.aop思想 配置事务管理
a.在web.xml配置前端控制器
b.编写Handler类(注解开发)
c.在springmvc.xml 配置 启用注解扫描
context:component-scan base-package="com.zrkc.ssm.controller"></context:component-scan>
d.web.xml中配置监听器 加载spring容器对象
框架整合编程:
配置application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载mysql.properties属性文件 -->
<context:property-placeholder location="classpath:mysql.properties"/>
<!-- 配置数据源 对象 从Datasource对象只能获取原始的Connection对象 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--配置数据库-->
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>${jdbc.maxActive}</value>
</property>
<property name="maxIdle">
<value>${jdbc.maxIdle}</value>
</property>
</bean>
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置SqlMapConfig.xml路径 -->
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
<!-- 注入数据源DataSource对象 -->
<property name="dataSource" ref="dataSource">
</property>
</bean>
<bean class="com.ssm.dao.UserDaoImpl" id="userDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean class="com.ssm.service.UserServiceImpl" id="userService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 通过Spring aop的思想 事务管理 -->
<!-- 配置共同切面对象 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入Datasource对象 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut id="myPointCut" expression="execution(public * com.ssm.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"></aop:advisor>
</aop:config>
</beans>
配置sqlMapConfig
<?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>
<!--Spring整合Mybatis后
有数据源连接池配置数据库的连接环境,
所以在这里不需要配置.
-->有数据源连接池配置数据库的连接环境,
所以在这里不需要配置.
-->
<typeAliases>
<package name="com.ssm.entity"/>
</typeAliases>
<mappers>
<mapper resource="com/ssm/mapper/UserMapper.xml"/>
</mappers>
</configuration>
配置springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 启用注解扫描 -->
<context:component-scan base-package="com.zrkc.ssm.controller"></context:component-scan>
<!-- 等价于: 引入注解的映射器 和注解适配器
默认自带了参数绑定.
-->默认自带了参数绑定.
-->
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/">
</property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置web.xml
<!-- spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器
监听到服务器一启动 马上就创建ApplicationContext对象
--><listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springmvc的前端控制器 -->
<servlet>
<servlet-name>springmvc1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc1</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
SpringMVC和Struts2的区别:
SpringMVC是 基于方法开发的
url-----方法对应
将页面的参数值传递给对应的方法形式参数.
多用户(多线程)访问下,Handler可以单例开发,
多线程访问方法 内存区域的是独立的,不会有并发访问问题.
无缝兼容整合的
Struts是 基于类的开发 多例开发
将页面的请求参数 传递给Action类的属性
这样如果在单例开发下,有可能会有并发访问问题.
Struts 标签 性能慢
低版本存在漏洞的
需要中间件整合(struts-spring-plugin.jar包)