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框架 ...
随机推荐
- [BUUCTF]PWN17——[HarekazeCTF2019]baby_rop
[BUUCTF]PWN17--[HarekazeCTF2019]baby_rop 附件 步骤: 例行检查,64位,开启了NX保护 试运行一下程序,看这个情况,当我们输入太长字符串的时候会报错 64位i ...
- CF433B Kuriyama Mirai's Stones 题解
Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\).有 \(m\) 次询问,询问有以下两种: \(1~l~r\),求 \(\sum\limits_{i=l ...
- LuoguP7259 [COCI2009-2010#3] SORT 题解
Content 请编写一个"频率排序器".输入一个 长度为 \(n\) 的数列 \(A=\{a_1,a_2,\dots,a_n\}\),要求: 按照每个数的出现次数降序排列. 如果 ...
- CF1141C Polycarp Restores Permutation 题解
Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{ ...
- 访问struts2的action页面出现白板问题
访问struts2的action页面出现白板问题 故需要设置拦截此action的拦截栈, <bean id="authenticationInterceptor" class ...
- Centos7查看防火墙对应的开放端口以及进行端口操作
1.查看开放端口列表 [root@host bin]# firewall-cmd --list-ports 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 12888/t ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- light oj -1245 - Harmonic Number (II)
先举个例子,假如给你的数是100的话,将100/2=50;是不是就是100除100-51之间的数取整为1: 100/3=33;100除50到34之间的数为2,那么这样下去到sqrt(100);就可以求 ...
- 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...