• springboot学习笔记


    1.数组的创建要注意,在 - 后要有个空格

    2.在yml文件的对象" : "后面要加上空格

    3.取值方式

    @Value("${name}")
    @Value("${address[0]}")

    4.值包括在''中则不转义字符,包裹在""中则转义字符

    5.在pom.xml中添加如下依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-configuration-processorartifactId>
    4. <optional>trueoptional>
    5. dependency>

    这样在书写application.yml的时候可以按照属性提示,比如person.address等等

    6.springboot项目打包成jar包

     

    将springboot项目打包后,在打包的文件夹下启动如下命令

     

     如果要切换到测试环境则:即在上一句的后面添加spring.properties.active=prod,同时观察到图中端口号已经切换为8087

     7.整合junit

    1. package com.example.junitstudy;
    2. import com.example.service.UserService;
    3. import org.junit.jupiter.api.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.test.context.junit4.SpringRunner;
    7. @RunWith(SpringRunner.class)//junit框架整合过来
    8. @SpringBootTest(classes = JunitStudyApplication.class)//将要测试的类导入
    9. class JunitStudyApplicationTests {
    10. UserService userService = new UserService();
    11. @Test
    12. void contextLoads() {
    13. userService.add();
    14. }
    15. }

     8.springboot整合mybatis

    在pom中添加如下依赖:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.4version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.ydlgroupId>
    12. <artifactId>springboot-mybatisartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>springboot-mybatisname>
    15. <description>springboot-mybatisdescription>
    16. <properties>
    17. <java.version>17java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-webartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.projectlombokgroupId>
    26. <artifactId>lombokartifactId>
    27. <optional>trueoptional>
    28. dependency>
    29. <dependency>
    30. <groupId>org.mybatis.spring.bootgroupId>
    31. <artifactId>mybatis-spring-boot-starterartifactId>
    32. <version>1.3.1version>
    33. dependency>
    34. <dependency>
    35. <groupId>mysqlgroupId>
    36. <artifactId>mysql-connector-javaartifactId>
    37. dependency>
    38. <dependency>
    39. <groupId>org.springframework.bootgroupId>
    40. <artifactId>spring-boot-testartifactId>
    41. <scope>testscope>
    42. dependency>
    43. <dependency>
    44. <groupId>org.springframework.bootgroupId>
    45. <artifactId>spring-boot-starter-testartifactId>
    46. <scope>testscope>
    47. dependency>
    48. <dependency>
    49. <groupId>junitgroupId>
    50. <artifactId>junitartifactId>
    51. <scope>testscope>
    52. dependency>
    53. dependencies>
    54. <build>
    55. <plugins>
    56. <plugin>
    57. <groupId>org.springframework.bootgroupId>
    58. <artifactId>spring-boot-maven-pluginartifactId>
    59. <configuration>
    60. <excludes>
    61. <exclude>
    62. <groupId>org.projectlombokgroupId>
    63. <artifactId>lombokartifactId>
    64. exclude>
    65. excludes>
    66. configuration>
    67. plugin>
    68. plugins>
    69. build>
    70. project>

     然后建立实体类

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. public class User {
    5. private int id;
    6. private String user_name;
    7. private String password;
    8. }

    建立mapper类(查询语句)

    1. @Mapper
    2. public interface UserMapper {
    3. @Select("select * from user")
    4. public List findAll();
    5. }

    在测试类里配置一下

    1. package com.ydl.springbootmybatis;
    2. import com.ydl.springbootmybatis.mapper.UserMapper;
    3. import com.ydl.springbootmybatis.user.User;
    4. import org.junit.jupiter.api.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. import org.springframework.test.context.junit4.SpringRunner;
    9. import java.util.List;
    10. @SpringBootTest(classes = SpringbootMybatisApplication.class)
    11. @RunWith(SpringRunner.class)
    12. class SpringbootMybatisApplicationTests {
    13. @Autowired
    14. UserMapper userMapper;
    15. @Test
    16. void contextLoads() {
    17. List all = userMapper.findAll();
    18. System.out.println(all);
    19. }
    20. }

  • 相关阅读:
    pta第二次博客
    pointpillars--kitti训练
    深入理解synchronized关键字
    23款奔驰GLC260L升级小柏林音响 全新15个扬声器
    UE4运用C++和框架开发坦克大战教程笔记(十九)(第58~60集)完结
    Spring core项目的创建与使用
    低端电脑如何深度学习秘籍-使用mistGPU计算平台
    mongodb安装及使用
    【LeetCode】《LeetCode 101》第十三章:链表
    【软考软件评测师】第三十三章 数据库系统应用
  • 原文地址:https://blog.csdn.net/weixin_41636858/article/details/127054342