1.配置tomcat数据源:

#   数据源基本配置
spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#最大等待连接中的数量,设位0则没有限制.
spring.datasource.tomcat.max-idle=10
#最大连接活动数:
spring.datasource.tomcat.max-active=50
#最大等待毫秒数,单位为ms,超过时间会出错误信息:
spring.datasource.tomcat.max-wait=10000
#数据库连接池初始化连接数:
spring.datasource.tomcat.initial-size=5

  1. 配置dbcp2数据源:

#   数据源基本配置
spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#指定数据库类型:
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#最大等待连接中的数量,设位0则没有限制.
spring.datasource.dbcp2.max-idle=10
#最大连接活动数:
spring.datasource.dbcp2.max-total=50
#最大等待毫秒数,单位为ms,超过时间会出错误信息:
spring.datasource.dbcp2.max-wait-millis=10000
#数据库连接池初始化连接数:
spring.datasource.dbcp2.initial-size=5

  1. springboot整合Mybatis框架:

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

配置文件:

#mybatis整合:
# 指定全局配置文件位置
mybatis.config-location: classpath:mybatis/mybatis-config.xml
#指定sql存放文件.
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

#日志配置DEBUG,可以更好的观察测试结果
logging.level.root=DEBUG
logging.level.org.springframework=DEBUG
logging.level.org.mybatis=DEBUG

启动类上设定@MapperScan定义扫描:

@MapperScan(value = "com.account.demo.dao")

 com.account.demo.dao就是dao层

         2.配置mybatis分页插件

.引入jar包:
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.</version>
</dependency>
.配置资源文件:
#mybatis的PageHeader
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql .重点:
//设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
PageHelper.startPage(page, rows);
.例子:
@Override
public PageBean getList(Integer page, Integer rows) {
//设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
PageHelper.startPage(page, rows);
List<Item> listData = itemMapper.getList();//会加limit:
int count = itemMapper.getListCount(); //总记录数: PageBean<Item> pageData = new PageBean<>(page, rows, count,listData);
return pageData;
}
分享一个工具类:
package com.account.demo.util; import java.util.List; /**
* 分页插件的Bean:
*/
public class PageBean<T> {
// 当前页
private Integer currentPage = ;
// 每页显示的总条数
private Integer pageSize = ;
// 总条数
private Integer totalNum;
// 是否有下一页
private Integer isMore;
// 总页数
private Integer totalPage;
// 开始索引
private Integer startIndex;
// 分页结果
private List<T> items; public PageBean() {
super();
} public PageBean(Integer currentPage, Integer pageSize, Integer totalNum,List items) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalNum = totalNum;
this.totalPage = (this.totalNum+this.pageSize-)/this.pageSize;
this.startIndex = (this.currentPage-)*this.pageSize;
this.isMore = this.currentPage >= this.totalPage?:;
this.items = items;
} public Integer getCurrentPage() {
return currentPage;
} public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
} public Integer getPageSize() {
return pageSize;
} public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
} public Integer getTotalNum() {
return totalNum;
} public void setTotalNum(Integer totalNum) {
this.totalNum = totalNum;
} public Integer getIsMore() {
return isMore;
} public void setIsMore(Integer isMore) {
this.isMore = isMore;
} public Integer getTotalPage() {
return totalPage;
} public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
} public Integer getStartIndex() {
return startIndex;
} public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
} public List<T> getItems() {
return items;
} public void setItems(List<T> items) {
this.items = items;
}
}

 

  1. 其他整合JPA(Hibernate),JDBCTemplate的,等后面的篇章把SpringBoot深入浅出对自己主要内容整理完后,会整理这些知识点,发出来.

(入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)的更多相关文章

  1. Maven项目中Spring整合Mybatis

    Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...

  2. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-1.整合Mybatis访问数据库和阿里巴巴数据源

    笔记 1.整合Mybatis访问数据库和阿里巴巴数据源     简介:整合mysql 加入mybatis依赖,和加入alibaba druid数据源 1.加入依赖(可以用 http://start.s ...

  3. java框架之SpringBoot(9)-数据访问及整合MyBatis

    简介 对于数据访问层,无论是 SQL 还是 NOSQL,SpringBoot 默认采用整合 SpringData 的方式进行统一处理,添加了大量的自动配置,引入了各种 Template.Reposit ...

  4. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  5. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  6. Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!

    前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...

  7. 微信小程序初体验,入门练手项目--通讯录,部署上线(二)

    接上一篇<微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器>:https://www.cnblogs.com/chengxs/p/9898670.html 开发微信小程序最尴尬 ...

  8. SpringBoot第五篇:整合Mybatis

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10869315.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   ORM框架 ...

  9. 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题

    一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...

随机推荐

  1. Flask——基础知识

    Flask应用程序 一个简单的Flask应用程序 # 导入flask程序 from flask import Flask # 初始化flask对象 app = Flask(__name__) # 装饰 ...

  2. 268. Missing Number@python

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. c++ 当输入的数据不符合数据类型时,清理输入流

    if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input; input pr ...

  4. centOS下jenkins

    转:centos7搭建jenkins小记 转自:https://segmentfault.com/a/1190000007086764 安装java环境 1.查看服务器版本 centos7,继续. c ...

  5. (46)zabbix报警媒介:Jabber

    Jabber有第三方插件,能让Jabber用户和MSN.YahooMessager.ICQ等IM用户相互通讯.因为Google遵从Jabber协议,并且Google已经将Gtalk的服务器开放给了其它 ...

  6. Flux reference

    https://facebook.github.io/flux/docs/dispatcher.html#content 首先安装 npm install --save flux Dispatcher ...

  7. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  8. 笛卡尔&小雷:科学发展有规律,研究科学有方法

    一直在总结自己的学习和研究方法,最近在读吴军写的<文明之光> ,感觉这篇介绍笛卡尔的内容非常有价值,特此整理.最近开始在密谋自己的理论体系,低调实施中...  笛卡尔按照感知的方式,把人的 ...

  9. luogu3396 哈希冲突

    参考这里 我们先预处理模数在 \(\sqrt{n}\) 以内的询问. 要是模数在 \(\sqrt{n}\) 以外,直接暴力统计,反正这样的数又不会超过 \(\sqrt{n}\) 个. 修改的时候也是. ...

  10. IOS 自动布局-UIStackPanel和UIGridPanel(五)

    试想这样的一个需求场合,一个button靠右显示,并且距离superView的顶部和右边间距分别为10和5.如下图所示: 要实现这样的需求,如果不用自动布局技术,那么我们能想到的就是老老实实的使用绝对 ...