• Spring boot入门demo


    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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.8.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--热启动:每自修改后, 程序自动启动spring Application上下文。  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <!--热启动 -->
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    项目文件目录
    项目文件目录

    模拟控制层代码

    package com.example.controller;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.example.pojo.Student;
    /**
     * spring boot demo
     * @author XSWL pengfei.xiong
     * @date 2017年11月17日
     */
    @RestController //相当于@Controller和@RequestBody 返回json
    @EnableAutoConfiguration
    public class HelloSpringBoot {
        @RequestMapping("/hello")
        public String showHello(){
            return "Hello SpringBoot!";
        }
    
    
        @RequestMapping("/pojo")
        public Student showStudent(){
            Student student = new Student(1, "show", 20);
            return student;
        }
    
        @RequestMapping("/map")
        public Map<String, Object> showMap(){
            Map<String, Object> map = new HashMap<String,Object>();
            map.put("username", "张无忌");
            map.put("age", 15);
            return map;
        }
    
        @RequestMapping("/list")
        public List<Student> showList(){
            List<Student> list = new ArrayList<Student>();
            Student student01 = new Student(1, "show", 20);
            Student student02 = new Student(1, "show", 20);
            list.add(student01);
            list.add(student02);
            return list;
        }
    
    }
    

    如果对于上面的注解有疑问就看下这个博客: https://www.cnblogs.com/woms/p/5754200.html

    实体类 就不用说了啊

    package com.example.pojo;
    
    import java.io.Serializable;
    
    public class Student implements Serializable{
        private int id;
        private String name;
        private int age;
    
        public Student() {
            super();
        }
    
    
        public Student(int id, String name, int age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
    
        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 getAge() {
            return age;
        }
    
    
        public void setAge(int age) {
            this.age = age;
        }
    
    
    }
    

    最后是程序的入口

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.example.controller.HelloSpringBoot;
    
    @SpringBootApplication
    public class DemoApplication{
    
    
        // 项目入口
        public static void main(String[] args) {
               SpringApplication.run(HelloSpringBoot.class, args);
                //运行之后在浏览器中访问:http://localhost:11086/hello 默认端口8080,我这是修改后的路径
        }
    
    
    }
    

    补充下:修改端口在application.properties文件中加一句话: server.port=11086
    demo源码下载 http://blog.csdn.net/xpf_user/article/details/78557782
    整合mybatis三层文章 http://blog.csdn.net/xpf_user/article/details/78557782:

    勿忘初心 得过且过
  • 相关阅读:
    VC.【转】采用_beginthread/_beginthreadex函数创建多线程
    Qt532.【转】Qt在pro中设置运行时库MT、MTd、MD、MDd,只适合VS版本的Qt
    Qt532界面.ZC测试
    Qt532.【转】Qt创建鼠标右键菜单
    VC.【转】窗口置于前台并激活的方法
    VS2010.STL::list的一个bug
    STL_map.VC6简单使用例子
    LeetCode题解-----Maximum Gap
    CEPH块设备创建及快照
    Ubuntu 14.04 部署 CEPH集群
  • 原文地址:https://www.cnblogs.com/xpf1009/p/9227299.html
Copyright © 2020-2023  润新知