• 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

  • 相关阅读:
    DataTable、GridView、DataList导出至Word或Excel
    实现字符串转换为图片
    Sina App Engine与Google App Engine区别对比
    C/C++反序输出字符串总结
    NAND flash和NOR flash详解
    [转载]看完《python简明教程》笔记及第一个python程序
    Python写爬虫——抓取网页并解析HTML
    ACM HDU 1004 Let the Balloon Rise
    有向图的强连通分量
    ACM HDU 1005 Number Sequence
  • 原文地址:https://www.cnblogs.com/bqwzy/p/12535912.html
Copyright © 2020-2023  润新知