🍁整合SpringMVC
- 1.先创建个SpringBoot项目,导入依赖
- <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 创建个启动类
- @SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 创建实体类User
- public class User {
private String username ;
private String password ;
private Integer age ;
private String sex ;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
- 编写Controller类
- @Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/findAll")
@ResponseBody
public List<User> findAll() {
//查询所有
List<User> users = new ArrayList<User>();
User user1 = new User();
user1.setUsername("xfx");
user1.setPassword("123456");
user1.setAge(20);
user1.setSex("男");
User user2 = new User();
user2.setUsername("mks");
user2.setPassword("234567");
user2.setAge(34);
user2.setSex("女");
User user3 = new User();
user3.setUsername("ls");
user3.setPassword("34567");
user3.setAge(34);
user3.setSex("女");
users.add(user1);
users.add(user2);
users.add(user3);
return users;
}
}