首页 > 编程语言 > 详细

SpringBoot学习笔记二之Spring整合Mybatis

时间:2020-03-21 00:28:34      阅读:63      评论:0      收藏:0      [点我收藏+]

原文链接:

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

原文:https://www.cnblogs.com/bqwzy/p/12535912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!