基础整合
导入jar包
项目目录结构
编写配置文件
因为Spring的配置文件有很多,所以根据层级编写不同的配置文件。
注意: 每个配置文件都需要引入不同的命名空间
spring-dao.xml
<!--配置db.properties-->
<context:property-placeholder location="classpath:MyBatis/db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--读取sqlMapConfig.xml配置文件-->
<property name="configLocation" value="classpath:MyBatis/sqlMapConfig.xml"></property>
<!--配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置Mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage 属性是让你为映射器接口文件设置基本的包路径-->
<property name="basePackage" value="com.dao"></property>
</bean>
spring-service.xml
<!--Spring组件扫描指定com包,进行注解解析-->
<context:component-scan base-package="com.service"></context:component-scan>
<import resource="spring-dao.xml"></import>
<!--配置核心事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
isolation:事务的隔离级别
read-only:事务是否只读
propagation:事务的传播行为
-->
<tx:method name="insert*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>
<tx:method name="update*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>
<tx:method name="select *" isolation="REPEATABLE_READ" read-only="true" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--将通知织入目标对象-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.Service.*ServiceImpl.*(..))"/>
<!--配置切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>
</aop:config>
这里的事务管理采用的是AOP模式,可以采用注解模式开启事务,但是需要在ServiceImpl中进行注解事务,然后开启事务注解扫描。具体细节参考 事务管理 的博客
<!--开启事务注解扫描-->
<tx:annotation-driven/>
springmvc.xml (即,Controller层的配置文件,MVC模块)
具体细节参考 SpringMVC博客
<!--Spring扫描指定com包,进行注解解析-->
<context:component-scan base-package="com.controller"></context:component-scan>
<!--MVC注解映射器和适配器 开启注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器(ViewResolver)-->
<mcv:view-resolvers>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--响应地址的 前缀 与 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</mcv:view-resolvers>
MyBatis的配置文件
SqlMapConfig.xml
web.xml
<!--配置监听器 加载spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/spring-dao.xml,classpath:Spring/spring-service.xml</param-value>
</context-param>
<servlet>
<servlet-name>SSM</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SSM</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Dao层
MyBatis框架,详情参照Mybatis博客
Service层
如果采用注解事务管理,则在此处进行事务注解
Controller层
SpringMVC管理,详情参照SpringMVC博客
前端页面
结果: