Spring Boot 2.0 常见问题总结(二)
使用 IDEA 生成 POJO 实体类
a. 使用 idea 连接上需要操作的数据库。
b. 选中要生成实体类的数据库表:右键 ---> Scripted Extensions ---> Generate POJOs.groovy。
c. 在弹出的窗口选择生成的位置即可。

application.properties属性自动映射实体类配置a. 在 application.properties 文件中定义的属性如下
wxpay.appid=wx5beac15ca207cdd40c
wxpay.appsecret=5548012f33417fdsdsdd6f96b382fe548215e9
b.使用
@PropertySource(value = "classpath:application.properties")即可。@Getter
@Setter
@Configuration
@PropertySource(value = "classpath:application.properties")
public class WeChatConfig {
@Value("${wxpay.appid}")
private String appId; // 公众号 appid
@Value("${wxpay.appsecret}")
private String appsecret; // 公众号密钥
} 执行以下测试代码,可以看到控制台输出的数据和配置文件一致
@RestController
@RequestMapping("/api")
public class VideoController {
@Autowired
private WeChatConfig weChatConfig; @GetMapping
public void getInfo(){
System.out.println(
weChatConfig.getAppId()+"==="+weChatConfig.getAppsecret()
);
}
} 配置文件读取参考:https://blog.csdn.net/CC1014524900/article/details/97061465
SpringBoot 2.1.6.RELEASE使用 Mybatis 访问数据库和数据源时候问题a.如果 MySQL 的版本 mysql-connector-java 用的 6.0 以上,DB 的连接信息配置如下:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/xdclass?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root com.mysql.jdbc.Driver 是 mysql-connector-java 5中的;
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的。
而且使用 com.mysql.cj.jdbc.Driver 的时候需要指定时区。
参考文章:https://blog.csdn.net/superdangbo/article/details/78732700
b. 在 application.properties 数据源的配置
如果不使用默认的数据(com.zaxxer.hikari.HikariDataSource),配置为 druid :
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
c. MyBatis相关配置
由于 DB 中配置的字段和实体类不对应(数据库有下划线,实体类是连接在一起),比如 DB 中配置 cover_Img,实体类中写为 coverImg。可以使用以下注解:
# mybatis 下划线转驼峰配置,两者都可以(下面配置二选一即可)
# mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.map-underscore-to-camel-case=true
d. 如何看到执行的 SQL 语句
# 增加打印sql语句,用于本地开发测试(配合插件 Mybatis Log plugins)
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
可以清晰看到执行的 SQL 语句。
PageHelper 分页插件的使用
a.引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
b.增加配置文件
@Configuration
public class MyBatisConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
// 设置为 true 时,会将RowBounds第一个参数offset当成pageNum页码使用
properties.setProperty("offsetAsPageNum", "true");
//设置为 true 时,使用RowBounds分页会进行count查询
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
pageHelper.setProperties(properties);
return pageHelper;
}
}
c. 分页查询代码
@RestController
@RequestMapping("/api")
public class VideoController { @Autowired
private VideoService videoService; /**
* 分页查询
*
* @param page 当前第几页 默认:1
* @param size 每次显示几条 默认:10
* @return
*/
@GetMapping("page")
public Object pageVideo(
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size)
{ PageHelper.startPage(page, size);
List<Video> videoList = videoService.findAll();
PageInfo<Video> pageInfo = new PageInfo<>(videoList);
Map<String,Object> data = new HashMap<>();
data.put("total_size",pageInfo.getTotal()); //总条数
data.put("total_page",pageInfo.getPages()); //总页数
data.put("current_page",page); //当前页
data.put("data",pageInfo.getList()); //数据
return data;
}
}
Spring Boot 2.0 常见问题总结(二)的更多相关文章
- Spring Boot 2.0 常见问题总结(一)
SpringBoot2.x 依赖环境和版本新特性说明 依赖版本 jdk8 以上, Springboot2.x 用 JDK8 , 因为底层是 Spring framework5 . jar 包方式运行 ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...
- 学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator
前言 主要是完成微服务的监控,完成监控治理.可以查看微服务间的数据处理和调用,当它们之间出现了异常,就可以快速定位到出现问题的地方. springboot - version: 2.0 正文 依赖 m ...
- spring boot 2.0(二)动态banner的支持
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...
- spring boot 2.0 源码分析(二)
在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...
- Spring Boot 2.0 教程 | @ModelAttribute 注解
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
随机推荐
- 【leetcode】662. Maximum Width of Binary Tree
题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- StackOverflowError
"Caused by: java.lang.StackOverflowError: null",当后台出现这个报错信息的时候,证明在代码模块里面出现了死循环,但是不一定是代码的问题 ...
- 【多线程】synchronized 和ReentrantLock
1. 锁的实现 synchronized 是 JVM 实现的,而 ReentrantLock 是 JDK 实现的. 2. 性能 新版本 Java 对 synchronized 进行了很多优化,例如自旋 ...
- UNP学习第五章(二)
一.POSIX信号处理 信号:告知某进程发生了某个事件的通知(软中断),通常是异步的. 信号可以:由进程发给另一个进程,由内核发给某个进程. 设置信号处理办法,有三个选择: 1.写一个函数,在信号发生 ...
- delphi文件类型
1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...
- 自己封装 vue 组件 和 插件
vue 组件 一.组件的创建,两种方法.(本质上是1.2两种,vue文件,只是创建了一个 组件选项对象,仅是一个js对象)1.定义组件:Vue.component('button-counter', ...
- 微信小程序学习笔记(二)--框架-全局及页面配置
描述和功能 框架提供了自己的视图层描述语言 WXML 和 WXSS,以及基于 JavaScript 的逻辑层框架,并在视图层与逻辑层间提供了数据传输和事件系统,让开发者能够专注于数据与逻辑. 响应的数 ...
- Vue 电影信息影评(豆瓣,猫眼)
Vue电影信息影评网站 此网站是我的毕业设计,题目是"基于HTML5的电影信息汇总弄网站",由于最近在看Vue.js,所以就想用Vue.js来构建一个前端网站,这里code就不大篇 ...
- adb 常用命令大全
adb 常用命令大全 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd ...
- MYSQL索引的深入学习
通常大型网站单日就可能会产生几十万甚至几百万的数据,对于没有索引的表,单表查询可能几十万数据就是瓶颈. 一个简单的对比测试 以我去年测试的数据作为一个简单示例,20多条数据源随机生成200万条数据,平 ...