博客目标
springboot入门
按照自己的需求创建第一个springboot项目(第一次比较慢)需要疯狂下jar包
搭建成功的项目目录
所有的java文件都需要写入com.onlyk.springboot01下面
Springboot01Application类不能删除,自带tomcat启动
测试创建出来的项目是否可以使用
启动自带的服务器Springboot01Application
接着上代码
HelloController
package com.onlyk.springboot01.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author only老K
* @create 2019-12-26 19:45
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/say1")
public String say1(){
return "onlyK你好,springboot搭建测试";
}
}
springmvc的对JSON格式的处理
HelloController
package com.onlyk.springboot01.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author only老K
* @create 2019-12-26 19:45
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/say1")
public String say1(){
return "onlyK你好,springboot搭建测试";
}
@RequestMapping("/say2")
public Map say2(){
Map map=new HashMap();
map.put("code",1);
map.put("msg","JSON格式处理讲解");
return map;
}
}
配置文件讲解
内置属性的修改
修改配置文件
重新启动内置服务器
自定义属性
重新启动服务器
上代码
HelloController
package com.onlyk.springboot01.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author only老K
* @create 2019-12-26 19:45
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Value("${user.uname}")
private String uname;
@Value("${user.pwd}")
private String pwd;
@RequestMapping("/say1")
public String say1(){
return "onlyK你好,springboot搭建测试";
}
@RequestMapping("/say2")
public Map say2(){
Map map=new HashMap();
map.put("code",1);
map.put("msg","JSON格式处理讲解");
return map;
}
@RequestMapping("/say3")
public Map say3(){
Map map=new HashMap();
map.put("uname",uname);
map.put("pwd",pwd);
return map;
}
}
存在问题:当属性一多是代码很臃肿
例如:
server.port=80
server.servlet.context-path=/
user.uname=onlyK
user.pwd=123
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
mysql.username=root
mysql.password=123456
解决方案:写属性封装类
MysqlEntity
package com.onlyk.springboot01.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author only老K
* @create 2019-12-26 20:09
*/
@Component
@ConfigurationProperties(prefix="mysql")
@Data
public class MysqlEntity {
private String driver;
private String url;
private String username;
private String password;
}
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
HelloController
package com.onlyk.springboot01.controller;
import com.onlyk.springboot01.entity.MysqlEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author only老K
* @create 2019-12-26 19:45
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Value("${user.uname}")
private String uname;
@Value("${user.pwd}")
private String pwd;
@Autowired
private MysqlEntity mysqlEntity;
@RequestMapping("/say1")
public String say1(){
return "onlyK你好,springboot搭建测试";
}
@RequestMapping("/say2")
public Map say2(){
Map map=new HashMap();
map.put("code",1);
map.put("msg","JSON格式处理讲解");
return map;
}
@RequestMapping("/say3")
public Map say3(){
Map map=new HashMap();
map.put("uname",uname);
map.put("pwd",pwd);
return map;
}
@RequestMapping("/say4")
public MysqlEntity say4(){
return mysqlEntity;
}
}