Spring boot入门demo

来源:互联网 发布:c语言求出100以内素数 编辑:程序博客网 时间:2024/05/13 12:23

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@EnableAutoConfigurationpublic 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;@SpringBootApplicationpublic 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:

原创粉丝点击