SpringBoot学习笔记二之Spring整合Mybatis
原文链接:
https://www.toutiao.com/i6803235766274097678/
在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置信息放到文档后面)
在learn-admin-webui配置jdbc.propertis
配置内容
jdbc.user=root
jdbc.password=
jdbc.url=jdbc:mysql://localhost:3306/project_learn?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver
在learn-admin-webui中配置mybatis-config.xml(注意是在mybatis目录中)
配置内容
在learn-admin-webui中配置spring-persist-mybatis.xml(resources目录下)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
继续在 spring-persist-mybatis.xml 配置文件进行配置
配置内容
写一个测试类,测试下配置是否正确
准备依赖
测试类添加配置
导入的包
完成代码
运行下查看
然后配置继续在 spring-persist-mybatis.xml 配置SqlSessionFactoryBean
配置内容
继续在 spring-persist-mybatis.xml 配置 MapperScannerConfigurer
配置内容
继续测试
运行时报了一个错误,排查是配置文件这里写错了
再次运行查看结果
查看数据库是有数据的
更换框架的日志系统
在learn-admin-webui和learn-admin-component中排除 commons-logging
commons-logging
commons-logging
在learn-admin-component加入转换包
org.slf4j
slf4j-api
1.7.7
ch.qos.logback
logback-classic
1.2.3
org.slf4j
jcl-over-slf4j
1.7.25
运行下之前的测试方法可以看到日志
可以使用logback 配置文件,格式化日志
配置内容
[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger]
[%msg]%n
可以再看下日志
配置事务
单独配置一个文件spring-persist-tx.xml用来配置事务
配置的内容
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
配置自动扫描的包 将Service扫描到IOC中
在learn-admin-component中创建对应的包结构
其中com.xlgl.wzy.service.api是接口
其中com.xlgl.wzy.service.impl是实现类
继续配置事务管理器
配置 AOP
配置事务通知
配置事务属性
查询方法
配置增删改
propagation 属性配置事务方法的传播行为
默认值:REQUIRED 表示:当前方法必须运行在事务中,
如果没有事务,则开启事务,在自己的事务中运行。
如果已经有了已开启的事务,则在当前事务中运行。
有可能和其他方法共用同一个事务
建议值:REQUIRES_NEW 表示:当前方法必须运行在事务中,
如果没有事务, 则开启事务,在自己的事务中运行。
和 REQUIRED 的区别是就算现在已经有了已开启的事务,也一定要开启自己的事务,
避免和其他方法共用同一个事务。
rollback-for 属性配置回滚的异常
默认值:运行时异常
建议值:编译时异常+运行时异常
在learn-admin-component中的service包创建类和接口
接口
实现类
编写方法
在接口中添加方法
实现类
准备测试
错误一
运行代码
出现错误
之前配置文件的配置放错地方,将attribute放到advice中
重新放置
错误二:
修改包结构,因为和xml中配置不一样,这个地方就不更改配置文件
重新运行成功
查看数据库
learn-admin-component添加的依赖
org.springframework
spring-orm
org.springframework
spring-webmvc
org.aspectj
aspectjweaver
cglib
cglib
mysql
mysql-connector-java
com.alibaba
druid
org.mybatis
mybatis
org.mybatis
mybatis-spring
com.github.pagehelper
pagehelper
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
jstl
jstl
com.google.code.gson
gson
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构