Springboot演示小Demo

模拟数据库演示springboot小测试

1、编写一个实体类:user

 1 package com.wisezone.test;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     
 7     private Integer userId;//用户id
 8     private String userName;//用户名
 9     private String age;//用户年龄
10     private String address;//用户地址
11     
12     public Integer getUserId() {
13         return userId;
14     }
15     public void setUserId(Integer userId) {
16         this.userId = userId;
17     }
18     
19     public String getUserName() {
20         return userName;
21     }
22     public void setUserName(String userName) {
23         this.userName = userName;
24     }
25     
26     public String getAge() {
27         return age;
28     }
29     public void setAge(String age) {
30         this.age = age;
31     }
32     
33     public String getAddress() {
34         return address;
35     }
36     public void setAddress(String address) {
37         this.address = address;
38     }
39 
40 }

2、编写一个dao:userDao

 1 package com.wisezone.test;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Repository;
 6 
 7 @Repository
 8 public class UserDao {
 9     private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
10     
11     public User findUser(String userName) {
12         logger.info("根据用户名查询:{}",userName);
13         User user = new User();
14         user.setUserId(10);
15         user.setUserName("上海上海");
16         user.setAge("20");
17         user.setAddress("上海市浦东新区上浦路888弄888号888室");
18         return user;
19     }
20 }

3、编写一个service:userService

 1 package com.wisezone.test;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 public class UserService {
 8     
 9     @Autowired
10     private UserDao userDao;
11     
12     public User findUserByUserName(String userName) {
13         return userDao.findUser(userName);
14     }
15 }

4、编写一个controller:userController

 1 package com.wisezone.test;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 
 9 @RequestMapping("user")
10 @Controller
11 public class UserController
12 {
13     @Autowired
14     private UserService userService;
15     
16     @RequestMapping("find")
17     @ResponseBody
18     public User findUser(String userName) {
19         
20         return userService.findUserByUserName(userName);
21     }
22 }

5、main方法测试

 1 package com.wisezone.test;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Application {
 8     
 9     public static void main(String[] args) {
10         SpringApplication.run(Application.class, args);
11     }
12 }

Console:

浏览器:http://localhost:8080/user/find/?userName=hello

 

posted @ 2017-07-06 23:44  程序员的世界。。。  阅读(517)  评论(0编辑  收藏  举报