avatar

目录
SpringBoot快速入门

springBoot快速入门

github详情:https://github.com/wangbowen1024/springboot-learning-quick

一、配置文件

首先使用IDEA创建spring项目,勾选Web和Mybatis以及XXX等所需要的组件,然后开始编写代码。

  • pom.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- 1.所有springboot工程都必须要继承spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>

<groupId>com.springboot</groupId>
<artifactId>springboot-learning</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- 2.web功能的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--热部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
  • application.yml
yml
# 数据库连接信息(MySql5.7)
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
username: root
password: Bow1024

在SpringbootApplication类同目录下都可以扫包读到注解,所以mybatis、controller等可以直接写。

二、编写类

2.1 启动类

SpringbootApplication.java

java
package com.springboot;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 基于注解的mybatis-boot整合
*/
@SpringBootApplication
@MapperScan(basePackages = "com.springboot.mapper") // mapper注解扫包
public class SpringbootApplication {

public static void main(String[] args) throws MqttException {
SpringApplication.run(SpringbootApplication.class, args);
}
}

2.2 Controller

UserController.java

java
package com.springboot.controller;

import com.springboot.domain.User;
import com.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
@Autowired
private UserMapper userMapper;

@RequestMapping("/queryUser")
@ResponseBody
public List<User> queryUser(){
List<User> users = userMapper.getAllUsers();
return users;
}
}

2.3 JavaBean

User.java

java
package com.springboot.domain;

public class User {
private Integer id;
private String username;
private String password;
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
}

2.4 Mapper接口

UserMapper.java

java
package com.springboot.mapper;

import com.springboot.domain.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

@Select("select * from user")
List<User> getAllUsers();
}

三、获取.yml文件中的键值对

application.yml

Code
# 普通数据的配置
name: zhangsan

# 对象的配置
person:
name: zhangsan
age: 18
addr: beijing

# 行内对象配置(类json)
server: {port: 8082}

# 配置数组、集合(普通字符串)
city:
- beijing
- tianjin

# 或city: [beijing,tianjin]

# 配置数组、集合(对象数据)
student:
- name: tom
age: 18
addr: beijing
- name: lucy
age: 17
addr: tianjin

# 或student: [{name: tom,age: 18,addr: beijing},{name: lucy,age: 17,addr: tianjin}]

# Map配置
map:
key1: value1
key2: value2

3.1 获取方式1

java
// 取配置文件信息
@Value("${name}")
private String name;

@Value("${person.addr}")
private String addr;

3.2 获取方式2

java
@Controller
@ConfigurationProperties(prefix = "person") // 从配置文件中,前缀
public class Quick3Controller {

private String name;
private String addr;
private Integer age;

@RequestMapping("/quick3")
@ResponseBody
public String quick2() {
// 获得配置文件的信息
return "name: " + name + ",addr: " + addr;
}

// 需要get/set方法
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddr() {
return addr;
}

public void setAddr(String addr) {
this.addr = addr;
}
}
文章作者: IT小王
文章链接: https://wangbowen.cn/2019/10/20/SpringBoot%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 IT小王

评论