zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【BZOJ-1060】时态同步 树形DP (DFS爆搜)
    【BZOJ-1468】Tree 树分治
    【BZOJ-1097】旅游景点atr SPFA + 状压DP
    【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用流)
    【BZOJ-2502】清理雪道 有上下界的网络流(有下界的最小流)
    【BZOJ-2055】80人环游世界 上下界费用流 (无源无汇最小费用流)
    【BZOJ-3275&3158】Number&千钧一发 最小割
    【BZOJ-4562】食物链 记忆化搜索(拓扑序 + DP)
    【BZOJ-1367】sequence 可并堆+中位数
    【BZOJ-1455】罗马游戏 可并堆 (左偏树)
  • 原文地址:https://www.cnblogs.com/wdh1995/p/7129103.html
Copyright © 2011-2022 走看看