您的位置:首页 > 其它

ssm整合

2020-02-03 00:32 127 查看

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"/>
  • 点赞
  • 收藏
  • 分享
  • 文章举报
weixin_45553582 发布了5 篇原创文章 · 获赞 0 · 访问量 270 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: