(入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
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
- 配置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
- 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;
}
}
- 其他整合JPA(Hibernate),JDBCTemplate的,等后面的篇章把SpringBoot深入浅出对自己主要内容整理完后,会整理这些知识点,发出来.
(入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)的更多相关文章
- Maven项目中Spring整合Mybatis
Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-1.整合Mybatis访问数据库和阿里巴巴数据源
笔记 1.整合Mybatis访问数据库和阿里巴巴数据源 简介:整合mysql 加入mybatis依赖,和加入alibaba druid数据源 1.加入依赖(可以用 http://start.s ...
- java框架之SpringBoot(9)-数据访问及整合MyBatis
简介 对于数据访问层,无论是 SQL 还是 NOSQL,SpringBoot 默认采用整合 SpringData 的方式进行统一处理,添加了大量的自动配置,引入了各种 Template.Reposit ...
- Spring Boot入门系列(六)如何整合Mybatis实现增删改查
前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!
前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...
- 微信小程序初体验,入门练手项目--通讯录,部署上线(二)
接上一篇<微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器>:https://www.cnblogs.com/chengxs/p/9898670.html 开发微信小程序最尴尬 ...
- SpringBoot第五篇:整合Mybatis
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10869315.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 ORM框架 ...
- 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题
一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...
随机推荐
- 【求助】NdisSend,自定义数据包发送失败?
做ndis hook的时候,自定义了一个数据包,包结构应该没有问题,填充NDIS_PACKET结构是这样的,先初始化: NdisAllocatePacketPool(&nStat ...
- 调用 C 动态库
调用 C 动态库 由 王巍 (@ONEVCAT) 发布于 2015/11/04 C 是程序世界的宝库,在我们面向的设备系统中,也内置了大量的 C 动态库帮助我们完成各种任务.比如涉及到压缩的话我们很可 ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- HDU-1217-Arbitrage(SPFA)
这题和以往的求最短路的题目略微有点不一样,以往求的都是最小的,这题求的是大的,而且还是乘法. 我们求的时候初始化的时候就要进行相反的初始化了,把它们初始化为0,然后比较大的就更新. 因为这题的点少边多 ...
- RestTemplate进行表单请求,注意要使用MultiValueMap
在对接API的时候,有时候文档中会说,表单提交,这时候就需要用到 MultiValueMap来操作,下面给大家展示一个简单的demo. MultiValueMap<Object, Object& ...
- BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)
Othello Othello is a game played by two people on an 8 x 8 board, using disks that are white on one ...
- js/jquery判断浏览器 & 停止加载
JS获取浏览器信息 复制代码代码如下: 浏览器代码名称:navigator.appCodeName浏览器名称:navigator.appName浏览器版本号:navigator.appVersion对 ...
- Silverlight客户端加载DWG图纸方案
前段时间一直再研究怎么才能在Silverlight客户端加载 DWG图纸,ArcGIS API For Silverlight中可以加载Shp文件,但是不能加载DWG,最后想出了一个方法步骤如下: 1 ...
- python 配置opencv-python 接口
anaconda2下配置opencv-python 接口,import cv2遇到no cv2 模块问题,解决办法是将cv2.so放到anaconda2/lib/python2.7/site-pack ...
- 【Luogu】P2158仪仗队(欧拉函数)
题目链接 首先来介绍欧拉函数. 设欧拉函数为f(n),则f(n)=1~n中与n互质的数的个数. 欧拉函数有三条引论: 1.若n为素数,则f(n)=n-1; 2.若n为pa,则f(n)=(p-1)*(p ...