博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
优效学院 基于微服务的秒杀项目实战 Spring Boot 2.0基础篇01
阅读量:3926 次
发布时间:2019-05-23

本文共 3509 字,大约阅读时间需要 11 分钟。

优效学院 基于微服务的秒杀项目实战  

Spring Boot 2.0基础篇01

 

1. 介绍

 

主要解决的是在微服务的架构下简化配置(有快速配置)、前后端分离、快速开发

优点:

提供了快速启动入门

开箱即用、提供默认配置

内嵌容器化web项目

没有冗余代码生成和xml配置要求

2.运行Demo

访问 

生成下载demo.zip

Maven本地源配置

  <mirrors>

   <mirror>

        <id>nexus-aliyun</id>

        <mirrorOf>*</mirrorOf>

        <name>Nexus aliyun</name>

        <url>http://maven.aliyun.com/nexus/content/groups/public</url>

    </mirror>

  </mirrors>

Pom.xml

必须要继承的parnet

 

Pom.xml第一行报错

 

进入本地库

for /r %i in (*.lastUpdated) do del %i   

然后update

缺少或包错误

 

删掉 重新update

找不到主类

 

所有错误都解决后

Jar方式运行 首先得有这个jar

clean package 生成jar文件,然后再run main方法

 

找不到jdk

 

 

jre的路径换成jdk

 

启动后自动停止

 

sts

下载

或者在eclipse上自己安装插件 spring tools

简单使用

修改banner

resources目录下新建banner.txt

  英文

 图片

application.properties

把所有的配置全放在这个文件里,方便统一管理,maven也可以做到

修改tomcat端口

server.port=90

修改项目路径

server.servlet.context-path=/demo 

 

 

多个入口main方法,打包之后找不到入库类

<build>

   <plugins>

      <plugin>  

          <groupId>org.springframework.boot</groupId>  

          <artifactId>spring-boot-maven-plugin</artifactId>  

                <configuration>  

                    <mainClass>com.yxxy.MyApp</mainClass>  

                </configuration>  

            </plugin>  

        </plugins>

</build>

Spring Boot中多个模块使用对应的配置文件

https://blog.csdn.net/cw_hello1/article/details/79639448

 

HelloWorld

RestController

RestController = @Controller+@ResponseBody

 

一个效果

 

@RestController

 

public class MyAppController {

 

@RequestMapping("/")

public Map<String, String>  index() {

Map<String, String> map = new HashMap<>();

map.put("aaa", "bbb");

map.put("aaa", "bbb");

map.put("aaa", "bbb");

map.put("aaa", "bbb");

return map;

}

 

使用thymeleaf模板引擎

Pom.xml引用

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

Controller代码

@Controller

public class IndexController {

 

@RequestMapping("/")

public String index(ModelMap map) {

 

// 加入一个属性,用来在模板中读取

map.addAttribute("msg", "nihao~");

return模板文件的名称,对应src/main/resources/templates/index.html

 

return "index";

}

模板文件代码

<h1 th:text="${msg}">hi!</h1>

 

稍微复杂的restful api应用

UserRestfulController

@RequestMapping("/")

@RestController

public class UserRestfulController {

 

static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>());

@RequestMapping(value="/User",method=RequestMethod.GET)

public List<User> getUserList(){

ArrayList<User> list = new ArrayList<>(users.values());

return list;

}

@RequestMapping(value="User",method=RequestMethod.POST)

public String addUser(@ModelAttribute User user) {

users.put(user.getId(), user);

return "addUser Success";

}

}

User

public class User {

 

private Long id;

private String loginName;

private String password;

private String nickName;

注入Service

UserRestfulController

 

@Autowired

private UserService userSrv;

@RequestMapping(value="/User",method=RequestMethod.GET)

public List<User> getUserList(){

 

return userSrv.getUserList();

}

@RequestMapping(value="User",method=RequestMethod.POST)

public String addUser(@ModelAttribute User user) {

String msg = userSrv.addUser(user);

return msg;

}

UserService

@Service

public class UserService {

static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>());

 

public List<User> getUserList() {

ArrayList<User> list = new ArrayList<>(users.values());

return list;

}

 

public String addUser(User user) {

users.put(user.getId(), user);

return "addUser Success";

}

}

前端模板显示

  <h1>User list</h1>

 

  <table>

    <tr>

      <th>NAME</th>

      <th>loginName</th>

      <th>nickName</th>

    </tr>

    <tr th:each="user : ${list}">

      <td th:text="${user.id}">id</td>

      <td th:text="${user.loginName}">loginName</td>

      <td th:text="${user.nickName}">nickName</td>

    </tr>

  </table>

 

  <p>

    <a href="../home.html" th:href="@{/}">Return to home</a>

  </p>

 

 

你可能感兴趣的文章
Mysql 事务处理
查看>>
Linux实操--实用指令Day4
查看>>
Linux实操--实用指令Day3
查看>>
spring+springboot认识
查看>>
Leetcode 136. 只出现一次的数字
查看>>
Leetcode 11. 盛最多水的容器
查看>>
Leetcode 121. 买卖股票的最佳时机
查看>>
Leetcode 123. 买卖股票的最佳时机 III
查看>>
Leetcode 24. 两两交换链表中的节点
查看>>
Leetcode 100. 相同的树
查看>>
Leetcode 257. 二叉树的所有路径
查看>>
Leetcode 4. 寻找两个正序数组的中位数
查看>>
Leetcode 101. 对称二叉树
查看>>
Leetcode 108. 将有序数组转换为二叉搜索树
查看>>
Leetcode 303. 区域和检索 - 数组不可变
查看>>
Leetcode 110. 平衡二叉树
查看>>
Leetcode 111. 二叉树的最小深度
查看>>
Leetcode 226. 翻转二叉树
查看>>
Leetcode 617. 合并二叉树
查看>>
Leetcode 654. 最大二叉树
查看>>