1. 程式人生 > >Springboot演示小Demo

Springboot演示小Demo

set ble cnblogs dem 技術分享 tor rep tap mapping

模擬數據庫演示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

技術分享

技術分享

Springboot演示小Demo