当前位置: 首页 > ds >正文

小白学习java第19天(下):spring

1.spring-mybatis整合

因此我们下面主要就是讲解,spring-mybatis怎么整合?(那么为什么需要整合,最主要就是事务这方面,因为在mybatis里面我们进行操作的时候都需要进行操作,我们根据spring的特性AOP,我们可以横向切面将其进行插入!!!)

要了解整合我们就需要回顾一下我们的mybatis是什么结构,有了结构之后我们才能知道是怎么进行整合的!!!

mybatis框架:

Spring-mybatis整合框架:

整合上图中的2:

spring对于new出的东西可以进行bean容器存储的,因此我们可以对mybatisConfig.xml这个文件处理,然后我希望就是mybatis原有的别名以及设置我是不希望进行变化的!!!

还有就是简化关系dataSource进行简化

以及绑定

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--DataSource,使用spring的数据源替换mybatis配置--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/account?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true&amp;useSSL=false&amp;serverTimezone=GMT%2b8&amp;allowPublicKeyRetrieval=true
"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!--  sqlSessionFactory创建配置省略--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/>
<!-- 绑定原有mybatis的配置文件
其实可以全部省略,但是我们还是希望在原有的mybatis里面进行一些别名,设置等配置
--><property name="configLocation" value="classpath:Mybatis-config.xml"/><property name="mapperLocations" value="classpath:com/xcl/dao/*.xml"/></bean><!-- sqlSessionTemplate配置,其实就是我们的sqlSession一样只是在spring里面换了一个名字!--><bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory"/></bean></beans>

因为我们需要使用注入bean,因此我们是需要创建一个实体类,去实现接口,然后注入我们sqlSessionTemplate,然后再xml里面进行什么声明!

package com.xcl.dao;import com.xcl.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;import java.util.List;public class UserMapperImp implements UserMapper{//我们所有操作。都是使用sqlSession来执行,现在都是使用SqlSessionTemplateprivate SqlSessionTemplate sqlSessionTemplate;//set方法,注入sqlsessionTemplatepublic void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic List<User> findAll() {UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);return mapper.findAll();}
}

自此我们spring-mybatis整合已经弄完了!

2.使用spring中的AOP对事务进行处理

下面我们举个例子:

我故意把delete写成deletee,使得我们插入了但是并没有对其进行删除,但是结果是报错了但是我们也同样进行插入!

因此我们需要对这些操作进行事务的管理,其实就是加一下配置就行!!!


<!--结合AOP实现事务的织入-->
<!-- 配置事务的通知--><tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性--><tx:attributes><tx:method name="addUser" propagation="REQUIRED"/><tx:method name="deleteUser" propagation="REQUIRED"/><tx:method name="updateUser" propagation="REQUIRED"/><tx:method name="find" propagation="SUPPORTS" read-only="true"/><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 事务的切入点--><aop:config><aop:pointcut id="txPointcut" expression="execution(* com.xcl.dao.UserMapper.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>

http://www.xdnf.cn/news/7087.html

相关文章:

  • Buildroot 移植MiniGUI: 编写简单示例(基于君正X2000)
  • STM32 | FreeRTOS 消息队列
  • Vue:数据双向绑定
  • 强化学习笔记(一)基本概念
  • 存储系统01——存储系统框架
  • C++ 蓝桥 STEMA 真题模拟测试卷一(选择题)
  • Diffusion模型入门指南:从零开始理解AI图像生成
  • 数字化转型- 数字化转型路线和推进
  • 刘家祎双剧收官见证蜕变,诠释多面人生
  • 记录:echarts实现tooltip的某个数据常显和恢复
  • 哈希表(2):
  • 校园导游大模型介绍
  • 微积分基本规则及示例解析
  • 【爬虫】DrissionPage-5
  • 第九届御网杯大赛赛题资源加解题思路
  • 手撕I2C和SPI协议实现
  • 豆粕ETF投资逻辑整理归纳-20250511
  • Centos7.9同步外网yum源至内网
  • 山东大学计算机图形学期末复习9——CG12上
  • 【部署】读取excel批量导入dify的QA知识库
  • 【Changer解码头详解及融入neck层数据的实验设计】
  • Fidder基本操作
  • Spring Initializr快速创建项目案例
  • Spark,连接MySQL数据库,添加数据,读取数据
  • Foupk3systemX5OS邮箱上线通知
  • Cadence Allegro安装教程及指导
  • Almalinux中出现ens33 ethernet 未托管 -- lo loopback 未托管 --如何处理:
  • JWT令牌验证
  • 45、简述web.config⽂件中的重要节点
  • Leaflet使用SVG创建动态Legend