ssm整合

ssm整合

spring 整合mybatis

spring 整合 mybatis,就是把mybatis的事务交给spring aop

ssm -----------> spring mybatis springmvc

spring -------> mybatis

1.mybatis工作原理

	映射接口去执行sql语句
CustomerMapper mapper = sqlsesssion.getMapper(CusttomerMapper.class);
		
		mapper.insertxx(Customer customer);
		
		sqlsesssion.getMapper(CusttomerMapper.class);
		得到映射接口的实现类
		mybatis框架利用了动态代理的技术
		sqlsesssion.getMapper(CusttomerMapper.class);
		会帮我们动态的产生一个映射接口的实现类
		所以我们并不是利用映射接口去执行sql
		而是利用映射接口的实现类所产生的对象去执行方法

2.pring aop 中的事务

1.配置spring aop中的事务
	1.事务隔离级别
	2.事务的是否只读
	3.事务的传播行为
		service --->  service
		Requered
2.事务的实现方式
	1. 编码式:
		利用spring容器提供的事务模板对象去进行管理事务
		这种方式不满足aop核心思想 -----> 代理
		这种方式仅用作吹牛
		
	2. xml 方式  spring aop 的方式
		servcieImpl   target
		method 		join point
		transcode   advice
		这个事务通知spring已经帮我们写好了
		我们只需要配置即可

spring 整合 mybatis

1.让spring aop 帮我们去创建映射接口的实现类
2.需要让spring aop 替我们去管理mybatis事务

1.准备jar
 spring:
	1.core beans context spel
	2.aop aspect aoplixx aspectweaver
	3.tx
	4.logging
	5.test
mybatis:
	mybatis.jar
	mybatis-spring.jar
database:
	mysql
	datasource
2.辅助文件
	datasouce.properties
	log4j.properties
	applicationContext.xml

配置mybatis
applicationContext-dao.xml
扫面service注解
applicationContext-service.xml
配置事务
applicationContext-trans.xml

文件applicationContext-dao.xml

<!-- 导入外部properties文件 -->
	<context:property-placeholder location="classpath:datasouce.properties"/>
	
	<!-- 创建数据库来连接池对象 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
<!-- mybatis配置类 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 告诉mybatis操作哪一个数据库 -->
	<property name="dataSource" ref="dataSource" />
	<!-- 告诉mybatis映射文件在哪个位置 -->
	<property name="mapperLocations" value="classpath:com/briup/mapper/*Mapper.xml" />
</bean>

<!-- 让spring帮助我们去创建映射接口的实现类 -->
<bean id="sc" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 告诉这个映射工具的映射文件在哪 -->
	<property name="sqlSessionFactoryBeanName" value="factory" />
	<!-- 告诉这个工具类 映射接口的包名是什么 -->
	<property name="basePackage" value="com.briup.mapper" />
</bean>

文件applicationContext-trans.xml

<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>

<tx:advice id="tx" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="delect*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" />
		<tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED"
			read-only="true" />
		<tx:method name="query*" isolation="DEFAULT" propagation="REQUIRED"
			read-only="true" />
		<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED"
			read-only="true" />

	</tx:attributes>
</tx:advice>

<aop:config>
	<aop:advisor advice-ref="tx"
		pointcut="execution(* com.briup.service..*.*(..))" />
</aop:config>

文件applicationContext-service.xml

<context:component-scan base-package="com.briup.service" />

文件springmvc.xml

<context:component-scan base-package="com.briup"></context:component-scan>
<mvc:default-servlet-handler/>
	<!-- 将逻辑视图转换为物理视图 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<import resource="classpath:applicationContext*.xml"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值