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框架 ...
随机推荐
- 9、Redis五大数据类型---有序集合Zset(sorted set)
一.简介 zset与set异同 相同之处: 都是没有重复元素的字符串集合 不同之处: 有序集合zset的每个成员都关联了一个评分(score),这个评分(score)被用来按照从最低分到最高分的方式排 ...
- [V&N2020 公开赛]babybabypwn
写在开头,感谢"影二つ"师傅的指点. 题目的做法思路网上一搜一大把,这篇博客主要记录一下这道题用pwntools写srop的时候,为什么需要省略前面8个字节. 在看题目之前,先来学 ...
- CF127A Wasted Time 题解
Content 平面上有 \(A_1(x_1,y_1),A_2(x_2,y_2),...,A_n(x_n,y_n)\) 共计 \(n\) 个点.你需要依次将 \(A_1\) 连接至 \(A_2\),\ ...
- JAVA将Byte数组(byte[])转换成文件
/** * 将Byte数组转换成文件 * @param bytes byte数组 * @param filePath 文件路径 如 D://test/ 最后"/"结尾 * @par ...
- JAVA连接MySQ报错:Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version
Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version at sun.security.ssl.Al ...
- summernote富文本图片上传,增加视频上传功能、批量上传方法
Summernote 是一个简单灵活的所见即所得的 HTML 在线编辑器,基于 jQuery 和 Bootstrap 构建,支持快捷键操作,提供大量可定制的选项. 但是却只有图片上传功能,没有视频上传 ...
- 【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Array and Operations
A. Array and Operations Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- vue 核心加解密工具类 方法
1 /* base64 加解密 2 */ 3 export let Base64 = require('js-base64').Base64 4 5 /* md5 加解密 6 */ 7 export ...
- $\infty$-former: Infinite Memory Transformer
目录 概 主要内容 如何扩展? 实验细节 Martins P., Marinho Z. and Martins A. \(\infty\)-former: Infinite Memory Transf ...