在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置信息放到文档后面)
![SpringBoot学习笔记二之Spring整合Mybatis](http://p2.ttnews.xyz/loading.gif)
在learn-admin-webui配置jdbc.propertis
配置内容
<code>jdbc.user=root
jdbc.password=
jdbc.url=jdbc:mysql://localhost:3306/project_learn?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver/<code>
![SpringBoot学习笔记二之Spring整合Mybatis](http://p2.ttnews.xyz/loading.gif)
在learn-admin-webui中配置mybatis-config.xml(注意是在mybatis目录中)
配置内容
<code>
<configuration>
/<configuration>/<code>
在learn-admin-webui中配置spring-persist-mybatis.xml(resources目录下)
<code>
<beans>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">
/<beans>/<code>
继续在 spring-persist-mybatis.xml 配置文件进行配置
配置内容
<code>
<property-placeholder>
<bean>
<property>
<property>
<property>
<property>
/<bean>/<code>
写一个测试类,测试下配置是否正确
准备依赖
测试类添加配置
导入的包
完成代码
运行下查看
然后配置继续在 spring-persist-mybatis.xml 配置SqlSessionFactoryBean
配置内容
<code>
<bean>
<property>
<property>
<property>
/<bean>/<code>
继续在 spring-persist-mybatis.xml 配置 MapperScannerConfigurer
配置内容
<code>
<bean>
<property>
/<bean>/<code>
继续测试
运行时报了一个错误,排查是配置文件这里写错了
再次运行查看结果
查看数据库是有数据的
更换框架的日志系统
在learn-admin-webui和learn-admin-component中排除 commons-logging
<code><exclusions>
<exclusion>
<groupid>commons-logging/<groupid>
<artifactid>commons-logging/<artifactid>
/<exclusion>
/<exclusions>/<code>
在learn-admin-component加入转换包
<code><dependency>
<groupid>org.slf4j/<groupid>
<artifactid>slf4j-api/<artifactid>
<version>1.7.7/<version>
/<dependency>
<dependency>
<groupid>ch.qos.logback/<groupid>
<artifactid>logback-classic/<artifactid>
<version>1.2.3/<version>
/<dependency>
<dependency>
<groupid>org.slf4j/<groupid>
<artifactid>jcl-over-slf4j/<artifactid>
<version>1.7.25/<version>
/<dependency>/<code>
运行下之前的测试方法可以看到日志
可以使用logback 配置文件,格式化日志
配置内容
<code>
<configuration>
<appender>
<encoder>
<pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger]
[%msg]%n/<pattern>
/<encoder>
/<appender>
<root>
<appender-ref>
/<root>
<logger>
/<configuration>/<code>
可以再看下日志
配置事务
单独配置一个文件spring-persist-tx.xml用来配置事务
配置的内容
<code>
<beans>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">/<beans>/<code>
配置自动扫描的包 将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添加的依赖
<code>
<dependency>
<groupid>org.springframework/<groupid>
<artifactid>spring-orm/<artifactid>
/<dependency>
<dependency>
<groupid>org.springframework/<groupid>
<artifactid>spring-webmvc/<artifactid>
/<dependency>
<dependency>
<groupid>org.aspectj/<groupid>
<artifactid>aspectjweaver/<artifactid>
/<dependency>
<dependency>
<groupid>cglib/<groupid>
<artifactid>cglib/<artifactid>
/<dependency>
<dependency>
<groupid>mysql/<groupid>
<artifactid>mysql-connector-java/<artifactid>
/<dependency>
<dependency>
<groupid>com.alibaba/<groupid>
<artifactid>druid/<artifactid>
/<dependency>
<dependency>
<groupid>org.mybatis/<groupid>
<artifactid>mybatis/<artifactid>
/<dependency>
<dependency>
<groupid>org.mybatis/<groupid>
<artifactid>mybatis-spring/<artifactid>
/<dependency>
<dependency>
<groupid>com.github.pagehelper/<groupid>
<artifactid>pagehelper/<artifactid>
/<dependency>
<dependency>
<groupid>com.fasterxml.jackson.core/<groupid>
<artifactid>jackson-core/<artifactid>
/<dependency>
<dependency>
<groupid>com.fasterxml.jackson.core/<groupid>
<artifactid>jackson-databind/<artifactid>
/<dependency>
<dependency>
<groupid>jstl/<groupid>
<artifactid>jstl/<artifactid>
/<dependency>
<dependency>
<groupid>com.google.code.gson/<groupid>
<artifactid>gson/<artifactid>
/<dependency>/<code>
閱讀更多 分享電腦學習 的文章