NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 【Spring】Spring初体验心得——注解

    • 调用接口即可实现实现类的功能
      • junit4 Test类前需添加@RunWith(SpringRunner.class) @SpringBootTest
        • JDBC MySQL8注意事项及application.properties配置
        NipGeihou
        2019-03-24
        Spring
        目录

        【Spring】Spring初体验心得——注解

        在跟老师使用 Spring 做项目过程中,遇到的一些问题,一些纯 Java 中不能理解的问题,摸索了一晚上后,发现跟注解息息相关,博主水平有限,刚学 Spring 不久,不能很好的解释其中的道理,只是分享一些,可能知其然,但不知其所以然的东西,仅供临时救急。

        比如说:

        # 调用接口即可实现实现类的功能

        Test 类

        @Resource
            private AyRecordService ayRecordService;
        
        @Test
            public void add() {
                AyRecord ayRecord = new AyRecord();
                ayRecord.setId(1);
                ayRecord.setAyUserId(1);
                ayRecord.setScore(500);
                ayRecord.setCreateTime(new Date());
        
                AyRecord result = ayRecordService.add(ayRecord);//这一行代码
        
                if (result != null) {
                    System.out.println("游戏记录新建成功");
                    System.out.println(result);
                } else {
                    System.out.println("游戏记录新建失败");
                }
            }
        

        AyRecordServicel 类:

        package com.nipgeihou.game2048.service;
        
        import com.nipgeihou.game2048.domain.AyRecord;
        
        public interface AyRecordService {
            /**
             * 新增游戏记录,如果分数大于以前的记录,则更新最高分
             *
             * @param ayRecord
             * @return
             */
            public AyRecord add(AyRecord ayRecord);
        }
        
        

        AyRecordServiceImpl 类:

        package com.nipgeihou.game2048.service.impl;
        
        import com.nipgeihou.game2048.domain.AyRecord;
        import com.nipgeihou.game2048.domain.AyUser;
        import com.nipgeihou.game2048.repository.AyRecordRepository;
        import com.nipgeihou.game2048.repository.AyUserRepository;
        import com.nipgeihou.game2048.service.AyRecordService;
        import org.springframework.stereotype.Service;
        
        import javax.annotation.Resource;
        
        @Service
        public class AyRecordServiceImpl implements AyRecordService {
        
            @Resource
            private AyUserRepository ayUserRepository;
            @Resource
            private AyRecordRepository ayRecordRepository;
        
            @Override
            public AyRecord add(AyRecord ayRecord) {
        
                AyRecord result = ayRecordRepository.save(ayRecord);
        
                AyUser ayUser = ayUserRepository.getOne(ayRecord.getAyUserId());
        
                if (ayRecord.getScore()>ayUser.getMaxScore()){
                    ayUser.setMaxScore(ayRecord.getScore());
                    ayUserRepository.save(ayUser);
                }
        
                return result;
            }
        }
        
        

        在纯粹的 JAVA 代码中,我不能理解为什么使用 ayRecordService.add(ayRecord) 可以实现添加数据库,而不是 ayRecordServiceImpl.add(ayRecord)

        捣腾了一晚,得知在接口的实现类前添加 @Service ,即可调用接口方法也能实现实现类功能。

        # junit4 Test 类前需添加 @RunWith (SpringRunner.class) @SpringBootTest

        直接通过右键 Service 接口创建的 junit4 Test 方法默认没有添加这两个注解,如果不添加这两个注解就会报错 java.lang.NullPointerException

        # JDBC MySQL8 注意事项及 application.properties 配置

        #连接地址
        spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=UTC
        #数据库账户
        spring.datasource.username=root
        #数据库密码
        spring.datasource.password=
        #数据库驱动
        spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
        

        MySQL 8 需要注意的是与 MySQL 5 不同,驱动改为了 com.mysql.cj.jdbc.Driver ,连接地址后面需要添加 useSSL=false&serverTimezone=UTC ,否则报错

        #Spring
        上次更新: 2022/01/05, 22:36:44
        最近更新
        01
        磁盘管理与文件系统
        05-02
        02
        网络测试 - iperf3
        05-02
        03
        Docker Swarm
        04-18
        更多文章>
        Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
        • 跟随系统
        • 浅色模式
        • 深色模式
        • 阅读模式