Spring笔记为三篇: ①Spring框架搭建、IOC实现原理、IOC(控制反转)、DI(依赖注入)
https://blog.csdn.net/sugar_map/article/details/80541924
②AOP实现原理、AOP(面向切面编程)、基于Spring的事务管理
https://blog.csdn.net/sugar_map/article/details/80543024
③Spring与其它框架的整合
本文核心内容:Spring+MyBatis+Struts2整合
Spring + MyBatis+Struts2整合
1. 搭建开发环境:
I. 引入Jar包:
1). Spring相关Jar包:
2). MyBatis相关Jar包:
3) . Struts2相关Jar包:
4) . MyBatis和Spring整合Jar包:
5) . Struts2和Spring整合Jar包:
6) . DBCP连接池Jar包:
7). 数据库驱动Jar包:
II. 引入配置文件:
1). applicationContext.xml
2). struts.xml
3). log4j.properties
4). jdbc.proerties(可选)
5). xxxMapper.xml
III. Spring配置MyBatis
首先引入局部配置:jdbc.properties
<context:property-placeholer />配置文件参数化
1. 作用:将经常需要修改的配置信息,转移到局部配置文件中.properties中,易于修改和维护。
2. 步骤:
I. 引入context的命名空间和xsd文件:
II. 引入局部配置
配置DataSource
配置SqlSessionFactory
Mapper扫描仪
III. Spring配置Struts2
Action对象的创建 、依赖的Service注入,ServiceImpl对象的创建、依赖的Dao注入。
Struts2.xml
配置web.xml文件。让Spring生成Action对象
ApplicationContext.xml源代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 "> <!--引入局部配置--> <context:property-placeholder location="classpath:/jdbc.properties"/> <!--配置数据源 (连接池)--> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${pwd}"/> </bean> <!--配置SqlSessionFacrtory--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--配置连接池--> <property name="dataSource" ref="ds"/> <!--实体类别名--> <property name="typeAliasesPackage" value="com.ssm.entity"/> <!--Mapper文件的位置--> <property name="mapperLocations" > <list > <value> classpath:/com/ssm/dao/impl/*.xml </value> </list> </property> </bean> <!--Mapper扫描仪--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" /> <property name="basePackage" value="com.ssm.dao"/> </bean> <!--为Service注入基于Mapper扫描生成的 XXXXDao--> <bean id="userService" class="com.ssm.service.impl.UserServiceImpl" scope="singleton"> <property name="userDao" ref="userDao"/> </bean> <!--为Action注入 service--> <bean id="userAction" class="com.ssm.action.UserAction" scope="prototype"> <property name="service" ref="userService"/> </bean> </beans>