(入门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方面 ...
随机推荐
- Multi Paxos
Multi Paxos [2] 通过basic paxos 以上步骤分布式系统已经能确定一个值,“只确定一个值有什么用?这可解决不了我面临的问题.” 你心中可能有这样的疑问. 原simple paxo ...
- HTML防止重复提交
1 在你的表单页里HEAD区加入这段代码: <META HTTP-EQUIV="pragma" CONTENT="no-cache"> <ME ...
- vue-cli3.0 生产包去除console.log
目前负责的公众号又迭代了一个版本,之前打生产包,配置总是和测试包搞混,所以使用了vue-cli3.0的环境变量来控制配置. 但是又发现了一个新问题,写代码的过程中写了很多console.log 来调试 ...
- mysql 导入数据库
1:创建数据库 dos 进入 xxx\MySQL5.7\bin 目录(很多人喜欢把这个路径配置在环境变量path中,这样在dos敲命令时,就直接是mysql......) mysql -uroot - ...
- 常用c++函数
strrev(str) (str为字符串)倒序输出字符串 floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整 ...
- 非memory空间有地址分配
对于非memory空间有地址分配,是由于有寄存器配置,比如AHB.APB.一些外设.
- 递归函数&二分查找
一.递归函数 1)定义 在函数中调用函数本身,就是递归 在python中递归的深度最大为1000,但实际达不到1000 def func(): print("-----func-----&q ...
- Python学习网站推荐
B站是目前本人看到的最好的免费学习Python的网站 黑马程序员- https://space.bilibili.com/37974444?spm_id_from=333.338.viewbox_re ...
- Linux服务器硬件设备信息查看
一.cpu信息 cpu信息存储在/proc文件系统的cpuinfo(/proc/cpuinfo)文件里,可以直接查看这个文件以获得cpu信息,所列字段解释如下: processor : 核心编号,如: ...
- Google 超分辨率技术 RAISR
每天都有数以百万计的图片在网络上被分享.储存,用户借此探索世界,研究感兴趣的话题,或者与朋友家人分享假期照片.问题是,大量的图片要嘛被照相设备的像素所限制,要嘛在手机.平板或网络限制下被人为压缩,降低 ...