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;
            }
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值