springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

1.druid介绍(https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

1.1   引入依赖

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.10</version>
  </dependency>

 1.2   application.yml配置druid

 1.3    druid作用:直接查看项目里面所有执行sql语句的信息。

 

1.2   启动SpringBoot项目访问druid
  
  http://localhost:tomcat端口号/项目名称/druid/

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

 

2.springboot整合mybatis

2.1  引入依赖

<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.3.2</version>
  </dependency>

2.1.1   显示日志:

在application.yml 配置:

logging:
     level: 
       com.zking.springboot01.mapper: debug

 

 2.2   使用Mybatis-Generator插件生成代码

      2.3.1 导入并修改generatorConfig.xml和jdbc.properties(resources下)

      2.3.2 配置pom.xml文件

      2.3.3 配置EditConfiguations的Maven启动方式

      命令:mybatis-generator:generate -e

注:bookMapper中加 @Repository它不会帮你管理,

[email protected]标签改为@Mapper标签(添加@Mapper注解之后,这个接口在编译时会生成相应的实现类。但请注意,这个接口中不可以定义同名的方法,因为会生成相同的id,因此这个接口不支持重载),要为每个Dao层的接口添加@Mapper注解

2.要在 Springboot03Application 中添加:@MapperScan("com.zking.springboot03.mapper")  用于扫描Mapper类的包

 

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

 

2.3     测试类中:

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

 

2.4   启用事物管理器    @EnableTransactionManagement

用处:一个方法调了两个mapper,保持数据的一致性,一旦报错,所有数据回滚

Springboot03Application 中添加 @EnableTransactionManagement   ,然后在BookServiceImpl层配合@Transactional注解使用即可。

 

2.5  整合分页插件

2.5.1   导入依赖


  <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.2.3</version>
  </dependency>

 

2.5.2   创建分页AOP
      注:必须开启动态代理
      @EnableAspectJAutoProxy

 

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

 

2.5.3  配置application.yml

 #pagehelper分页插件配置
  pagehelper:
     helperDialect: mysql
     reasonable: true
     supportMethodsArguments: true
     params: count=countSql

 

注:

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)

 

springboot整合mybatis (druid介绍 springboot整合mybatis 以及分页插件)