• 使用 IDEA 生成 POJO 实体类

    a. 使用 idea 连接上需要操作的数据库。

    b. 选中要生成实体类的数据库表:右键 ---> Scripted Extensions ---> Generate POJOs.groovy。

    c. 在弹出的窗口选择生成的位置即可。

  • application.properties 属性自动映射实体类配置

    a. 在 application.properties 文件中定义的属性如下

    1. wxpay.appid=wx5beac15ca207cdd40c
    2. wxpay.appsecret=5548012f33417fdsdsdd6f96b382fe548215e9

    b.使用 @PropertySource(value = "classpath:application.properties") 即可。

    1. @Getter
    2. @Setter
    3. @Configuration
    4. @PropertySource(value = "classpath:application.properties")
    5. public class WeChatConfig {
    6. @Value("${wxpay.appid}")
    7. private String appId; // 公众号 appid
    8. @Value("${wxpay.appsecret}")
    9. private String appsecret; // 公众号密钥
    10. }
    11. 执行以下测试代码,可以看到控制台输出的数据和配置文件一致
    12. @RestController
    13. @RequestMapping("/api")
    14. public class VideoController {
    15. @Autowired
    16. private WeChatConfig weChatConfig;
    17. @GetMapping
    18. public void getInfo(){
    19. System.out.println(
    20. weChatConfig.getAppId()+"==="+weChatConfig.getAppsecret()
    21. );
    22. }
    23. }
    24. 配置文件读取参考:https://blog.csdn.net/CC1014524900/article/details/97061465
  • SpringBoot 2.1.6.RELEASE 使用 Mybatis 访问数据库和数据源时候问题

    a.如果 MySQL 的版本 mysql-connector-java 用的 6.0 以上,DB 的连接信息配置如下:

    1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    2. spring.datasource.url=jdbc:mysql://localhost:3306/xdclass?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    3. spring.datasource.username=root
    4. spring.datasource.password=root
    5. com.mysql.jdbc.Driver mysql-connector-java 5中的;
    6. com.mysql.cj.jdbc.Driver mysql-connector-java 6中的。
    7. 而且使用 com.mysql.cj.jdbc.Driver 的时候需要指定时区。
    8. 参考文章:https://blog.csdn.net/superdangbo/article/details/78732700

    b. 在 application.properties 数据源的配置

    1. 如果不使用默认的数据(com.zaxxer.hikari.HikariDataSource),配置为 druid
    2. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

    c. MyBatis相关配置

    1. 由于 DB 中配置的字段和实体类不对应(数据库有下划线,实体类是连接在一起),比如 DB 中配置 cover_Img,实体类中写为 coverImg。可以使用以下注解:
    2. # mybatis 下划线转驼峰配置,两者都可以(下面配置二选一即可)
    3. # mybatis.configuration.mapUnderscoreToCamelCase=true
    4. mybatis.configuration.map-underscore-to-camel-case=true

    d. 如何看到执行的 SQL 语句

    1. # 增加打印sql语句,用于本地开发测试(配合插件 Mybatis Log plugins)
    2. mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    3. 可以清晰看到执行的 SQL 语句。
  • PageHelper 分页插件的使用

    1. a.引入依赖
    2. <dependency>
    3. <groupId>com.github.pagehelper</groupId>
    4. <artifactId>pagehelper</artifactId>
    5. <version>4.1.0</version>
    6. </dependency>
    7. b.增加配置文件
    8. @Configuration
    9. public class MyBatisConfig {
    10. @Bean
    11. public PageHelper pageHelper() {
    12. PageHelper pageHelper = new PageHelper();
    13. Properties properties = new Properties();
    14. // 设置为 true 时,会将RowBounds第一个参数offset当成pageNum页码使用
    15. properties.setProperty("offsetAsPageNum", "true");
    16. //设置为 true 时,使用RowBounds分页会进行count查询
    17. properties.setProperty("rowBoundsWithCount", "true");
    18. properties.setProperty("reasonable", "true");
    19. pageHelper.setProperties(properties);
    20. return pageHelper;
    21. }
    22. }
    23. c. 分页查询代码
    24. @RestController
    25. @RequestMapping("/api")
    26. public class VideoController {
    27. @Autowired
    28. private VideoService videoService;
    29. /**
    30. * 分页查询
    31. *
    32. * @param page 当前第几页 默认:1
    33. * @param size 每次显示几条 默认:10
    34. * @return
    35. */
    36. @GetMapping("page")
    37. public Object pageVideo(
    38. @RequestParam(value = "page", defaultValue = "1") int page,
    39. @RequestParam(value = "size", defaultValue = "10") int size)
    40. {
    41. PageHelper.startPage(page, size);
    42. List<Video> videoList = videoService.findAll();
    43. PageInfo<Video> pageInfo = new PageInfo<>(videoList);
    44. Map<String,Object> data = new HashMap<>();
    45. data.put("total_size",pageInfo.getTotal()); //总条数
    46. data.put("total_page",pageInfo.getPages()); //总页数
    47. data.put("current_page",page); //当前页
    48. data.put("data",pageInfo.getList()); //数据
    49. return data;
    50. }
    51. }

Spring Boot 2.0 常见问题总结(二)的更多相关文章

  1. Spring Boot 2.0 常见问题总结(一)

    SpringBoot2.x 依赖环境和版本新特性说明 依赖版本 jdk8 以上, Springboot2.x 用 JDK8 , 因为底层是 Spring framework5 . jar 包方式运行 ...

  2. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  3. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

  4. Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  5. 学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator

    前言 主要是完成微服务的监控,完成监控治理.可以查看微服务间的数据处理和调用,当它们之间出现了异常,就可以快速定位到出现问题的地方. springboot - version: 2.0 正文 依赖 m ...

  6. spring boot 2.0(二)动态banner的支持

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  7. spring boot 2.0 源码分析(二)

    在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...

  8. Spring Boot 2.0 教程 | @ModelAttribute 注解

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...

  9. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

随机推荐

  1. 力扣—Reorder List(重排链表)python实现

    题目描述: 中文: 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点 ...

  2. svn解决方案

    1.svn回退 revert to this version 2.不同svn路径拉倒一个本地文件夹,报错"已经指向不同的url工作副本":  删除文件夹中的.svn文件夹 3.sv ...

  3. oracle死锁查询

    select sess.sid ||','|| sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...

  4. 12.Unsafe原子性操作

    import sun.misc.Unsafe; /** * JDK8 * JDK 的此jar 包中的 Unsafe 类提供了硬件级别的原子性操作 */ public class UnsafeTest ...

  5. string.format()详解

    java中: 从 Java 5.0 开始,String 类新增了一个强大的字符串格式化方法 format().这个方法到现在用的人还是不多,实在是一种浪费.本文带你快速过一遍这个方法的功能,将来你要用 ...

  6. vue.js 分页

    <template> <div class="index"> <el-pagination background :hide-on-single-pa ...

  7. tp框架基础控制器调用方法

    public function indd(){ //调用该控制器下的某个方法 $this ->index(); //跨控制器调用 $k = A("index");// 创建控 ...

  8. Redis入门很简单之四【初识Jedis】

    Redis入门很简单之四[初识Jedis] 博客分类: NoSQL/Redis/MongoDB redisnosql缓存jedis  使用Jedis提供的Java API对Redis进行操作,是Red ...

  9. Sublime text 3 3103 注册码(2016.2.9更新)

    Sublime text 3 (Build 3103) license key,these all tested available on 2016/02/10 .Feel free to enjoy ...

  10. spring+freemarker 乱码解决办法

    这样应该可以了~ <!-- freemarker config --> <bean id="freemarkerConfig" class="org.s ...