创建Springboot工程

1.pom文件中的配置(这一个是通过maven的形式)

 1). 继承springboot的父工程 (idea必须是2020以上,并且本地仓库必须存在该依赖)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.1</version>
</parent>

2).引入web启动依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3).创建一个主启动类并加入相应注解

@SpringBootApplication //注解
public class MavenApp {
    public static void main(String[] args) {
        SpringApplication.run(MavenApp.class,args);
    }
}

2. springboot工程中常见的配置文件类型。

application.properties 和  application.yml (二者选择一种)

properties的格式

yml文件的格式 :

3. java如何读取springboot配置文件中的内

1).通过@ConfigurationProperties(prefix = "student")进行读取

2).通过@Value注解来读取

注:应用这两种方法要在配置文件中添加Lombok配置

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

注:如果通过@value读取的为properties类型,不能读取map以及自定义的类型 ;如果读取的是yml类型,那么只能读取基本数据类型以及Stringdate类型。

4. Springboot整合数据源+mybatis

注:创建的是基于Spring Initializr形式,不是Maven。

整合数据源:

Dependencies的选择:

使用了druid的数据源:

在pom文件中的配置:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.21</version>
</dependency>

之后是在properties配置文件中加入如下的配置 :

spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEnconding=utf8
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.max-active=5

然后测试是否连接数据库成功

package com.yjq.springbootdatasource;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;

@SpringBootTest
class SpringbootdatasourceApplicationTests {
    @Autowired
    private DataSource dataSource;

    @Test
    void contextLoads() throws Exception{
        System.out.println(dataSource);
    }
}

整合mybatis:

首先在pom中引入相关的依赖:

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

其次配置mybatis映射文件所在的路径 :

mybatis.mapper-locations=classpath:/mapper/*.xml

接着创建相应的entry实体以及mapper :

然后在resources下创建映射文件:

紧接着在主启动类上扫描mapper接口所在的包

@SpringBootApplication
//在主启动类上扫描mapper接口所在的包
@MapperScan(basePackages = {"com.yjq.springbootdatasource.mapper"})
public class SpringbootdatasourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootdatasourceApplication.class, args);
    }
}

最后controller层调用mapper的方法:

package com.yjq.springbootdatasource.controller;

import com.yjq.springbootdatasource.entry.Test1;
import com.yjq.springbootdatasource.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//controller调用mapper中的方法
@RestController
public class TestController {
    @Autowired
    private TestMapper testMapper;

    @GetMapping("list")
    public List<Test1> list(){
        return testMapper.selectTest();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值