spring boot 整合 druid + Mysql + Mybatis

来源:互联网 发布:开淘宝店的准备工作 编辑:程序博客网 时间:2024/05/31 04:03
本章主要介绍以下几点:
1: spring boot 整合 durid + mysql 数据连接池
2:  spring boot 整合 Mybatis 

3: spring boot 配置 Mybatis pagehelper 分页插件

整备工作:

1: 数据库使用Mysql (没有Mysql 的可以到官网下载)
2: Maven  

一:开发流程:

1: spring boot 项目搭建我在这就不做介绍了,不懂得朋友可以到官网上下个demo 很简单。

2: spring 整合 durid 数据库连接池 需要添加对 durid 的支持

<!-- druid数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.26</version></dependency>
3: spring boot 整合 Mysql 需要添加的Mysql 驱动 

<!-- MySql数据库驱动 --><dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId></dependency>

4: spring boot 整合 MyBatis 和添加对Mybatis 的支持

<!-- springboot的mybatis --><dependency>    <groupId>org.mybatis.spring.boot</groupId>    <artifactId>mybatis-spring-boot-starter</artifactId>    <version>1.1.1</version></dependency>

5: spring boot 配置Mybatis Pagehelper 需要添加对其的支持

<!--分页插件 --><dependency>    <groupId>com.github.pagehelper</groupId>    <artifactId>pagehelper-spring-boot-starter</artifactId>    <version>1.0.0</version></dependency>
完成上面5步 准备工作基本完成了

二:配置:

sping boot 支持两种配置文件格式,一种.perproties 文件, 一种是.yml 文件, 个人喜欢用yml 文件

配置都在application.yml文件中

1: 配置Mysql 的连接

#数据库设置spring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://127.0.0.1:3306/vport_oneway?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false     username: root    password: root

2: 配置durid 数据库连接池

#下面为连接池补充设置    initialSize: 5    # 配置获取连接等待超时的时间       maxWait: 60000    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒    timeBetweenEvictionRunsMillis: 60000    # 配置一个连接在池中最小生存的时间,单位是毫秒     minEvictableIdleTimeMillis: 300000    validationQuery: SELECT 1 FROM DUAL    testWhileIdle: true    testOnBorrow: false    testOnReturn: false    # 打开PSCache,并且指定每个连接上PSCache的大小    poolPreparedStatements: true    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000    # 合并多个DruidDataSource的监控数据     useGlobalDataSourceStat: true
3:配置 Mybatis 支持

mybatis:
  # 配置xml 的扫描路径  mapper-locations: classpath*:org/spring/boot/mapper/*.xml  check-config-location: true  type-aliases-package: org.spring.boot.po
Mybatis 的配置就是这么简单, 在这可能有朋友会有疑问?这么没有配置Mapper 的也就是(dao)的扫面路劲? 因为在soring boot 中为我们提供了 @Mapper 注解, 只需要在Mapper 接口上添加@Mpper注解就搞定, 是不是比以前一对的xml文件简单多了。

4:配置分页插件

pagehelper:   autoDialect: true  closeConn: false  reasonable: true
没有看错就只这么简单,

三: 开发

1: 构造 durid 连接池:

import javax.sql.DataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import com.alibaba.druid.pool.DruidDataSource;@Configurationpublic class DruidDataSourceConfiguration {@Bean  @Primary    @ConfigurationProperties(prefix = "spring.datasource")     public DataSource druidDataSource() {          DruidDataSource druidDataSource = new DruidDataSource();          return druidDataSource;      }  }
住: 这里用到四个注解: 又不懂的朋友可以看下spring boot 的文档, 我在这就不多做解释了

2: @Mapper 注解的使用,上文提到Mapper 接口只需要使用@Mapper注解就行,不需要额外的配置,

import java.util.List;import org.apache.ibatis.annotations.Mapper;import org.spring.boot.po.UserInfo;import org.spring.boot.po.UserInfoExample;@Mapperpublic interface UserInfoMapper {    int countByExample(UserInfoExample example);    int deleteByPrimaryKey(Integer id);    int insert(UserInfo record);    int insertSelective(UserInfo record);    List<UserInfo> selectByExample(UserInfoExample example);    UserInfo selectByPrimaryKey(Integer id);    int updateByPrimaryKeySelective(UserInfo record);    int updateByPrimaryKey(UserInfo record);    int deleteByUserCode(String userCode);    int deleteByGovCode(String govCode);}
3: 分页介绍

@Servicepublic class UserInfoServiceImpl implements IUserInfoService {@Autowiredprivate UserInfoMapper userInfoMapper;@SuppressWarnings("rawtypes")@Overridepublic PagehelperUtils listUser() {PageHelper.startPage(1, 2, true);UserInfoExample example = new UserInfoExample();List<UserInfo> list = this.userInfoMapper.selectByExample(example);@SuppressWarnings("unchecked")PageInfo pageInfo = new PageInfo(list); return new PagehelperUtils(pageInfo);}
分页插件使用起来很简单: 只需要在需要分页的查询前加上  PageHelper.startPage(pageNum,  pageSize, true); pageNum 是当前页, pageSize 是每页显示多少条,boolean 标识是否计算totalSize 

好了这样spring boot + durid + mysql + mybatis  基本就可以搞定了:

如有不明白的同学可以到git 上下载我做的一个spring-boot-example 项目,里面对spring boot 在项目中常用的功能做了一个实现。

git:  https://github.com/yuelicn/spring-boot-example

嗯, 就到这了,如有明白和不对的地方欢迎大家一起探讨。


0 1