Skip to content

Commit

Permalink
增加 springmvc 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 15, 2019
1 parent 1d03c0d commit d14162e
Show file tree
Hide file tree
Showing 24 changed files with 948 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lab-22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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">
<parent>
<artifactId>labs-parent</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-22</artifactId>


</project>
31 changes: 31 additions & 0 deletions lab-23/lab-springmvc-23-01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-springmvc-23-01</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
<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>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab21.springmvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cn.iocoder.springboot.lab21.springmvc.controller;

import cn.iocoder.springboot.lab21.springmvc.dto.UserAddDTO;
import cn.iocoder.springboot.lab21.springmvc.dto.UserUpdateDTO;
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
* 用户 Controller
*/
@RestController
@RequestMapping("/users")
public class UserController {

/**
* 查询用户列表
*
* @return 用户列表
*/
@GetMapping("")
public List<UserVO> list() {
// 查询列表
List<UserVO> result = new ArrayList<>();
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
// 返回列表
return result;
}

/**
* 获得指定用户编号的用户
*
* @param id 用户编号
* @return 用户
*/
@GetMapping("/{id}")
public UserVO get(@PathVariable("id") Integer id) {
// 查询并返回用户
return new UserVO().setId(id).setUsername("username:" + id);
}

/**
* 添加用户
*
* @param addDTO 添加用户信息 DTO
* @return 添加成功的用户编号
*/
@PostMapping("")
public Integer add(UserAddDTO addDTO) {
// 插入用户记录,返回编号
Integer returnId = 1;
// 返回用户编号
return returnId;
}

/**
* 更新指定用户编号的用户
*
* @param id 用户编号
* @param updateDTO 更新用户信息 DTO
* @return 是否修改成功
*/
@PutMapping("/{id}")
public Boolean update(@PathVariable("id") Integer id, UserUpdateDTO updateDTO) {
// 将 id 设置到 updateDTO 中
updateDTO.setId(id);
// 更新用户记录
Boolean success = true;
// 返回更新是否成功
return success;
}

/**
* 删除指定用户编号的用户
*
* @param id 用户编号
* @return 是否删除成功
*/
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable("id") Integer id) {
// 删除用户记录
Boolean success = false;
// 返回是否更新成功
return success;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cn.iocoder.springboot.lab21.springmvc.controller;

import cn.iocoder.springboot.lab21.springmvc.dto.UserAddDTO;
import cn.iocoder.springboot.lab21.springmvc.dto.UserUpdateDTO;
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* 用户2 Controller
*/
@RestController
@RequestMapping("/users2")
public class UserController2 {

/**
* 查询用户列表
*
* @return 用户列表
*/
@GetMapping("/list") // URL 修改成 /list
public List<UserVO> list() {
// 查询列表
List<UserVO> result = new ArrayList<>();
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
// 返回列表
return result;
}

/**
* 获得指定用户编号的用户
*
* @param id 用户编号
* @return 用户
*/
@GetMapping("/get") // URL 修改成 /get
public UserVO get(@RequestParam("id") Integer id) {
// 查询并返回用户
return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
}

/**
* 添加用户
*
* @param addDTO 添加用户信息 DTO
* @return 添加成功的用户编号
*/
@PostMapping("add") // URL 修改成 /add
public Integer add(UserAddDTO addDTO) {
// 插入用户记录,返回编号
Integer returnId = UUID.randomUUID().hashCode();
// 返回用户编号
return returnId;
}

/**
* 更新指定用户编号的用户
*
* @param updateDTO 更新用户信息 DTO
* @return 是否修改成功
*/
@PostMapping("/update") // URL 修改成 /update ,RequestMethod 改成 POST
public Boolean update(UserUpdateDTO updateDTO) {
// 更新用户记录
Boolean success = true;
// 返回更新是否成功
return success;
}

/**
* 删除指定用户编号的用户
*
* @param id 用户编号
* @return 是否删除成功
*/
@DeleteMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE
public Boolean delete(@RequestParam("id") Integer id) {
// 删除用户记录
Boolean success = false;
// 返回是否更新成功
return success;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.iocoder.springboot.lab21.springmvc.dto;

/**
* 用户添加 DTO
*/
public class UserAddDTO {

/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;

public String getUsername() {
return username;
}

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

public String getPassword() {
return password;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.iocoder.springboot.lab21.springmvc.dto;

public class UserUpdateDTO {

/**
* 编号
*/
private Integer id;
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;

public Integer getId() {
return id;
}

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

public String getUsername() {
return username;
}

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

public String getPassword() {
return password;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.iocoder.springboot.lab21.springmvc.vo;

/**
* 用户 VO
*/
public class UserVO {

/**
* 编号
*/
private Integer id;
/**
* 账号
*/
private String username;

public Integer getId() {
return id;
}

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

public String getUsername() {
return username;
}

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

}
Loading

0 comments on commit d14162e

Please sign in to comment.