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分页插件
1.引入jar包: <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> 2.配置资源文件: #mybatis的PageHeader pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql 3.重点: //设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】 PageHelper.startPage(page, rows); 4.例子: @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 = 1; // 每页显示的总条数 private Integer pageSize = 10; // 总条数 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-1)/this.pageSize; this.startIndex = (this.currentPage-1)*this.pageSize; this.isMore = this.currentPage >= this.totalPage?0:1; 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深入浅出对自己主要内容整理完后,会整理这些知识点,发出来.