SpringBoot学习笔记

快速开始

使用IDEA自带的向导生成项目结构

application.yaml配置文件修改

Common Application Properties (spring.io)

server:
  port: 80   # 设置端口80
  servlet:
    context-path: /start    # 设置虚拟目录

application.yaml配置文件读取

package com.tansor.springboot.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@ConfigurationProperties(prefix = "email")      // 设置前缀,来自application.yaml文件中的email
@Repository("emailProperties")
@Scope("singleton")
public class EmailProperties {
    public String user;     // 由于有了email前缀,只要此处字段名和application下email.字段名相等即可自动加载参数

    public String code;

    public String host;

    private boolean auth;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public boolean isAuth() {
        return auth;
    }

    public void setAuth(boolean auth) {
        this.auth = auth;
    }
}

SpringBoot整合MyBatis

Maven导坐标

先把spring-boot换成3.1.5,否则与mybatis冲突
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
导入以下
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
</dependency>

配置文件application.yaml增加行

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring
    username: spring
    password: spring_password

创建用户信息类com.tansor.pojo.User.java

package com.tansor.springboot.pojo;

public class User {
    private String userId;
    private String userName;
    private int userAge;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public User(String userId, String userName, int userAge) {
        this.userId = userId;
        this.userName = userName;
        this.userAge = userAge;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", userAge=" + userAge +
                '}';
    }
}

创建映射接口com.tansor.mapper.UserMapper.java

package com.tansor.springboot.mapper;

import com.tansor.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper//注意这个Mapper------------------------------------------------------------------------------《
public interface UserMapper {
    @Select("select * from userinfo where user_id = #{userId}")     //此处写SQL语句
    User selectById(String userId);
}

创建业务类com.tansor.service.impl.UserServiceImpl.java以及其接口com.tansor.service.UserService.java

package com.tansor.springboot.service.impl;

import com.tansor.springboot.mapper.UserMapper;
import com.tansor.springboot.pojo.User;
import com.tansor.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;      //自动装配一个mapper映射接口

    @Override
    public User findById(String userId) {
        return userMapper.selectById(userId);
    }
}
package com.tansor.springboot.service;

import com.tansor.springboot.pojo.User;

public interface UserService {
    User findById(String userId);
}

创建控制器com.tansor.controller.UserController.java响应web请求

package com.tansor.springboot.controller;

import com.tansor.springboot.pojo.User;
import com.tansor.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;        //自动装配一个userService

    @RequestMapping("/findById/{userId}")
    public User findById(@PathVariable String userId){
        System.out.println(userId);
        User temp = userService.findById(userId);
        System.out.println(temp);
        return temp;
    }
}

SpringBoot Bean自动扫描

启动类 Application.java上的注解@SpringBootApplication包含了@ComponentScan

SpringBoot Bean注册

注解 说明 位置
@Component 声明bean的基础注解 不输于以下三类时,用此注解
@Controller @Component的衍生注解 标注在控制器类上
@Service @Component的衍生注解 标注在业务类上
@Respository @Component的衍生注解 标注在数据访问上(由于与MyBatis整合,用的少)
@Bean

@Bean——引入第三方Bean

在SpringBoot中引入第三方Bean时,先创建一个配置类(com.tansor.config.SpringConfig.java),然后像下面这样创建一个方法返回一个对象作为Bean,默认的beanName是方法名(例如:resolver)

若方法内部需要使用到IoC内部已经存在的bean对象,那么直接在方法上声明即可,spring回自动注入

package com.tansor.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.yaml.snakeyaml.resolver.Resolver;

@Configuration
public class SpringConfig {
    @Bean
    public Resolver resolver(Country country){
        return new Resolver();
    }
}

@Import——手动扫描导入第三方Bean

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇