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
SpringBoot学习笔记二之Spring整合Mybatis的更多相关文章
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- Spring整合MyBatis(二)Spring整合MyBatis
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 了解了MyBatis的独立使用过程后,我们再看看它与Spring整合的 ...
- SpringBoot学习笔记<二>注解
此篇为项目作结之笔记,关于注解. 项目启动入口@SpringBootApplication[必选] @ServletComponentScan[可选] 注解后: Servlet.Filter.Lis ...
- Spring学习笔记六:Spring整合Hibernate
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html 前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...
- springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)
Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...
- SpringBoot学习(二)——Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 一.@Configuration 和 @Bean Spring的Java配置方式是通过@Configuration和@Bean ...
- SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...
- Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)
http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...
随机推荐
- 万字教你如何用 Python 实现线性规划
摘要:线性规划是一组数学和计算工具,可让您找到该系统的特定解,该解对应于某些其他线性函数的最大值或最小值. 本文分享自华为云社区<实践线性规划:使用 Python 进行优化>,作者: Yu ...
- 【WP】攻防世界-杂项-Misc
长期更新一波 攻防世界 的杂项题解 这东西主要靠积累吧 攻防世界:https://adworld.xctf.org.cn 因为攻防世界的题目顺序经常变化,我也不改序号了,顺着之前写的位置往下写,推荐使 ...
- IDEA中安装SVN
下载svn和汉化安装包: (下面安装过程中,运行这两个安装包需要管理员权限:使用管理员权限运行cmd,在cmd中运行这两个安装包) 1.安装SVN 安装SVN时这里要选择[will be i ...
- 线程 TLS
TLS为什么产生呢?是软件开发中的什么问题呢? TLS 产生背景进程中的全局变量与函数内定义的静态(static)变量,是各个线程都可以访问的共享变量.在一个线程修改的内存内容,对所有线程都生效 ...
- Spring工具类 非spring管理环境中获取bean及环境配置
SpringUtils.java import org.springframework.beans.BeansException; import org.springframework.beans.f ...
- layUI中layDate控件兼容性问题(手机端没有效果,不显示)
使用layDate插件发现在PC端无问题,然而在适配移动端时,发现点击input时,laydate渲染出的时间控件有时候没有反应,后发现只需在render里加入trigger: 'click',即可以 ...
- JAVA获取昨天、今天、明天等日期
/** * 获取明天的日期字符串 * @return */ public static String tomorrowDateStr(){ Date date=new Date();//取时间 Cal ...
- 【LeetCode】1464. 数组中两元素的最大乘积 Maximum Product of Two Elements in an Array (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找最大次大 日期 题目地址:https://le ...
- 【LeetCode】52. N-Queens II 解题报告(Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...
- 【剑指Offer】合并两个排序的链表 解题报告(Python)
[剑指Offer]合并两个排序的链表 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interview ...