springBoot快速入门
github详情:https://github.com/wangbowen1024/springboot-learning-quick
一、配置文件
首先使用IDEA创建spring项目,勾选Web和Mybatis以及XXX等所需要的组件,然后开始编写代码。
<?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>
<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> <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>
|
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
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;
@SpringBootApplication @MapperScan(basePackages = "com.springboot.mapper") public class SpringbootApplication {
public static void main(String[] args) throws MqttException { SpringApplication.run(SpringbootApplication.class, args); } }
|
2.2 Controller
UserController.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
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
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
# 普通数据的配置 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
@Value("${name}") private String name;
@Value("${person.addr}") private String addr;
|
3.2 获取方式2
@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; }
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; } }
|