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

小白学习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>

相关文章:

  • css 中 content: “\e6d0“ 怎么变成图标的?
  • 实验八 基于Python的数字图像问题处理
  • JDBC实现模糊、动态与分页查询的详解
  • 论文解读:ICLR2025 | D-FINE
  • LangGraph(四)——加入人机交互控制
  • 开源项目实战学习之YOLO11:12.3 ultralytics-models-sam-encoders.py源码分析
  • DeepSeek源码深度解析 × 华为仓颉语言编程精粹——从MoE架构到全场景开发生态
  • Vue3——父子组件通信
  • Android7 Input(七)App与input系统服务建立连接
  • 灵光一现的问题和常见错误1
  • 搭建基于Windows平台的http文件服务(miniserve+filebrowser+nssm)
  • Datawhale PyPOTS时间序列5月第3次笔记
  • 湖北理元理律师事务所:债务优化中的双维支持实践解析
  • 一分钟用 MCP 上线一个 贪吃蛇 小游戏(CodeBuddy版)
  • java中的运算符
  • 多线程(4)——线程安全,锁
  • 数学复习笔记 16
  • 在 Linux 上安装 MATLAB:完整指南与疑难解决方案
  • 交流学习 | 江西同为科技有限公司赴海尔总部考察交流
  • Spring源码之解决循环依赖 三级缓存
  • 广西鹿寨一水文站“倒刺扶手”存安全隐患,官方通报处理情况
  • 中国进出口银行:1-4月投放制造业中长期贷款超1800亿元
  • 贵州省委军民融合发展委员会办公室副主任李刚接受审查调查
  • 江西3人拟提名为县(市、区)长候选人
  • KPL“王朝”诞生背后:AG和联赛一起迈向成熟
  • 上海市重大工程一季度开局良好,崇明线等按既定计划加快建设