创建好springboot项目之后
配置文件的解析
pom.xml
<!--依赖管理父节点的配置,简化maven的依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--项目编码,和jdk版本控制-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--配置启动依赖和其他配置-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--配置maven插件,也能用maven形式启动项目-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
#修改tomcat的启动端口
server.port=8080
#修改项目的部署路径
server.servlet.context-path=/a
#server.context-path=/XXXXX 格式报错
#因为spring boot版本问题导致的!!现在用的比较新的版本在2.0以上!以前是1.0-2.0之间的.server.context-path=/XXXXX
#
#把原来server.context-path=/XXXXX
#
#改成server.servlet.context-path=/XXXXX
#修改日志级别
logging.level.root=ERROR
Application.java
/*
启动类
作用:
当前类为配置类
启动自动配置
组件扫描
几乎所有ssm里面的xml的配置文件都阻碍里面
*/
@SpringBootApplication
public class Bootdemo111302Application {
public static void main(String[] args) {
SpringApplication.run(Bootdemo111302Application.class, args);
}
}
restful风格的增删改查
users.java
public class Users {
private int id;
private String name;
@Override
public String toString() {
return "Users{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
测试
在application.java文件所在目录的子目录里面,创建controller.java
@RestController
@RequestMapping("users")
public class UserController {
// @RequestMapping("showusers")
@GetMapping("showusers") //restful风格,get获取
public Users showusers (){
Users users=new Users();
users.setId(2);
users.setName("fdk");
return users;
}
@GetMapping("showint/{id}")
public int showint(@PathVariable("id") int id){//@PathVariable("id") 注解必须
return id;
}
//原子变量 模仿数据库主键自增
// AtomicInteger s=new AtomicInteger(20)//20表示自增的开始数字
// AtomicLong
@PostMapping("newuser") //post 新建
public void newuser(@RequestBody Users users){
System.out.println(users);
}
@PutMapping("updateuser/{id}")//put 修改
public Users putuser(@PathVariable("id") int id, @RequestBody Users users){
users.setId(id);
return users;
}
@DeleteMapping("deleteuser/{id}") //delete 删除
public String deleteuser(@PathVariable("id") int id){
return "删除";
}
}