Spring Boot项目创建(约定大于配置)
2.1.3.RELEASE版本示例
- idea创建
- 从官网下载(https://start.spring.io/)
- 单元测试默认依赖不对时,直接删除即可
Web支持(SpringMVC)
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
模板引擎Thymleaf 整合
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
spring:thymeleaf:cache: false # 关闭缓存,默认开启prefix: classpath:/pages/ #修改默认路径 classpath:/templates/
Mybatis 整合
添加依赖【刷新Maven】
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>
配置文件修改=> yml(yaml) / properties
spring:datasource:url: jdbc:mysql://localhost:3306/qcby_db?useUnicode=true&characterEncoding=utf-8username: rootpassword:driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:mapper-locations: classpath:mapper/*.xml #对应mapper映射xml文件所在路径type-aliases-package: com.xxxx.entity #对应实体类路径
启动类修改
// 启动类修改
@MapperScan("com.xxxx.mapper")
@SpringBootApplication
测试
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yglh.mapper.TestMapper"><resultMap id="BaseResultMap" type="com.qcbt.lxt.byg0417.entity.Test"><id column="id" jdbcType="BIGINT" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /></resultMap><sql id="Base_Column_List">id,name</sql>
</mapper>
PageHelper 分页插件整合
需要注意和mybatis-spring-boot-starter的版本关系。
添加依赖
<!--pageHelper分页插件-->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.3.0</version>
</dependency>
配置 => properties / yml
pagehelper:helperDialect: mysqlreasonable: true # 修改默认值
- reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNumpages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
示例
Page<Object> page = PageHelper.startPage(logInfoVo.getPage(), logInfoVo.getLimit());