zoukankan      html  css  js  c++  java
  • 一个简单的springboot项目

    项目整体包结构

    步骤1:创建

      这个地址可以查看如何创建一个空白项目:https://www.cnblogs.com/HelloM/p/14241721.html。

    步骤2:pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <groupId>cn.howj</groupId>
        <artifactId>product-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <modelVersion>4.0.0</modelVersion>
        <packaging>war</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/>
        </parent>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
                    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>        
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
            </dependency>    
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>4.3.1</version>
            </dependency>                
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>        
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    步骤3:启动类ProductServiceApplication 

    package cn.how2j.springboot;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    
    import cn.hutool.core.util.NetUtil;
    @SpringBootApplication
    public class ProductServiceApplication {
        public static void main(String[] args) {
            int port = 8080;
            if(!NetUtil.isUsableLocalPort(port)) {
                System.err.printf("端口%d被占用了,无法启动%n", port );
                System.exit(1);
            }
            new SpringApplicationBuilder(ProductServiceApplication.class).properties("server.port=" + port).run(args);
        }
    }

    步骤4:实体类product

    package cn.how2j.springboot.pojo;
    
    public class Product {
    
        private int id;
        private String name;
        private int price;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public void setPrice(int price) {
            this.price = price;
        }
        public Product(int id, String name, int price) {
            super();
            this.id = id;
            this.name = name;
            this.price = price;
        }
    }

    步骤5:service业务处理

    package cn.how2j.springboot.service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Service;
    
    import cn.how2j.springboot.pojo.Product;
    
    @Service
    public class ProductService {
        public List<Product> listProducts(){
            List<Product> ps = new ArrayList<>();
            ps.add(new Product(1,"product a", 50));
            ps.add(new Product(2,"product b", 100));
            ps.add(new Product(3,"product c", 150));
            return ps;
        }
    }

    步骤6:controller访问

    package cn.how2j.springboot.web;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import cn.how2j.springboot.pojo.Product;
    import cn.how2j.springboot.service.ProductService;
     
    @Controller
    public class ProductController {
        @Autowired ProductService productService
        @RequestMapping("/products")
        public Object products(Model m) {
            List<Product> ps = productService.listProducts();
            m.addAttribute("ps", ps);
            return "products";
        }
    }

    步骤7:视图资源配置product.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>products</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style>
    
            table {
                border-collapse:collapse;
                width:400px;
                margin:20px auto;
            }
            td,th{
                border:1px solid gray;
            }
        </style>        
    </head>
    <body>
    <div class="workingArea">
        <table>
            <thead>
                <tr>
                    <th>id</th>
                    <th>产品名称</th>
                    <th>价格</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="p: ${ps}">
                    <td th:text="${p.id}"></td>
                    <td th:text="${p.name}"></td>
                    <td th:text="${p.price}"></td>
                </tr>
            </tbody>
        </table>
    </div>
    </body>  
    </html>

    步骤8:配置文件properties

    #thymeleaf 配置
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.content-type=text/html
    #缓存设置为false, 这样修改之后马上生效,便于调试
    spring.thymeleaf.cache=false
    #上下文
    server.context-path=/

    步骤9:启动Application,访问测试项目是否运行成功

      访问地址:http://localhost:8080/products

    总结

    这个项目很简单,就做了两件事:1.提供数据 2.展示数据。这就是典型的单体架构。
    项目的缺点:1.如果想要修改数据的代码,就必须把整个项目都重新打包,部署。维护不方便
    2.如果提供数据部分出现了问题,比如有的人把代码一不小心写错了,或者代码内部也抛出了异常,那整个项目都不能使用,展示数据的服务也宕掉了。
    3.单体服务,性能瓶颈突破不了,当高并发访问量来的时候,只能有一个访问端口,服务器容器崩溃。
  • 相关阅读:
    pickle模块
    json模块
    sys模块的介绍
    Java中char和String的处理问题
    javaweb学习 基础(三)Cookie Session
    javaweb学习 基础(二)httpservlet
    javaweb学习 基础(一)servlet day09-day10
    java学习 网络编程 tcp
    java学习 网络编程 ip,udp
    了解数据仓库
  • 原文地址:https://www.cnblogs.com/HelloM/p/14312596.html
Copyright © 2011-2022 走看看