Sping Boot 基础知识

基础篇:

一、REST风格----RESETful

Http://localhost/users        查询全部用户信息 GET(查询)

Http://localhost/users/1      查询指定用户信息 GET(查询)

Http://localhost/users        添加用户信息     POST(新增、保存)

Http://localhost/users        修改用户信息     PUT(修改、更新)

Http://localhost/users/1      删除用户信息     DELETE(删除)

二、配置优先级

Properties>yml>yaml

Yaml:注重数据

三、yaml文件信息读取:

①属性名引用方式 ${},yaml文件中也可使用

②使用引号包裹的字符串能够实现转义字符。\t

③ server servlet context-path: /text  配置访问路径与@RequestMapping("/books")

country: china

user1:
  name: c
  age: 20

users:
  -
    name: a
    age: 12
  -
    name: b
    age: 20

likes:
  - basketball
  - game
  - music

likes2: [game,music,basketball]

调用方式:

Controller:

@RestController
//@RequestMapping("/books")//通用访问前缀路径

Public class controller{

  1. Value(单一变量依次创建变量读取)

@RestController

Public class text{

@Value("${country}")//value中的属性值名称需与yaml中一致
private String country;//定义的属性名可与yaml中不同
@Value("${user.name}")
private String name1;

  1. 初始化springboot时注入环境对象,最后直接通过环境对象进行调用(封装数据)
    @Autowired//自动装配
    private Environment env;
    3.通过@ConfigurationProperties注入对象,需要创建一个与yml中元素属性相同的类

①在新创建的类中【

//1.定义数据模型封装yml文件中对应的数据

创建类
//2.定义为Spring管控的bean
@Component
//3.指定加载数据
@ConfigurationProperties(prefix="datasource")

@Autowired
private MyDataSource myDataSource;

@GetMapping
public String getById(){
    System.out.println("Springboot is running");

//1.
    System.out.println(country);
    System.out.println(name1);//打印当前页的属性

//2.
System.out.println(env.getProperty("users[1].name"));

//3.
System.out.println(myDataSource);

return "springboot is running";//返回页面显示内容

}

}

四、整合第三方技术

1.Test测试项目(结合junit)

①创建一个Dao接口

②编写接口的实现类

//数据层
@Repository也可以使用@Componment

③在测试类中编写测试对象

//1.注入需要测试的对象
   @Autowired
   private BookDao bookdao;
//2.执行要测试对象对应的方法
   @Test
   void contextLoads() {
      bookdao.save();
   }

测试类错误包不匹配的情况:

@SpringBootTest(classes = SpbJunitApplication.class)用classes指定相对应的类

@RunWith(设置运行器)

@ContextConfiguration(指定配置文件配置类)

2.整合mybatis

①在xml文件中导入相应的starter

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

②在yml文件中配置相关信息,设置数据源参数

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver ---(com.mysql.cj.jdbc.Driver)
    url: jdbc:mysql://localhost:3306/myspb?serverTimezone=UTC(服务时区,版本不同)
    username: root
    password: 123

#设置Mp相关的配置

mybatis-plus:

   global-config:

     db-config:

       Table-prefix:前缀

③定义数据层接口与映射配置

@Mapper
public interface StudentDao {
    @Select("select * from student where id = #{id}")
    public Student getById(Integer id);

(plus版本:public interface StudentDao extends BaseMapper<Student>{})
}

④在测试类中注入dao接口,测试功能

//使用自动配置

@Autowired
private StudentDao studentDao;

测试类全部查询:

System.out.println(studentDao.selectList(queryWrapper:null));

3.整合Druid

①配置yml

spring:
 datasource:
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3306/myspb?serverTimezone=UTC
   username: root
   password: 123
   type: com.alibaba.druid.pool.DruidDataSource

其余与mybaits相同

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值