首页 > 学院 > 开发设计 > 正文

springBoot入门笔记

2019-11-08 18:31:17
字体:
来源:转载
供稿:网友

项目目录结构:

com     +- xx         +- application.java         |         +- controller         |   +- SimpleController .java         |         +- entity             +- User.java

1. Eclipse创建一个简单的Maven工程:

com +- example     +- myPRoject         +- Application.java         |         +- domain         |   +- Customer.java         |   +- CustomerRepository.java         |         +- service         |   +- CustomerService.java         |         +- web             +- CustomerController.java

2.修改pop.xml

把下面代码添加进去

    <parent>	<groupId>org.springframework.boot</groupId>	<artifactId>spring-boot-starter-parent</artifactId>	<version>1.5.1.RELEASE</version>    </parent>    <dependencies>	<dependency>	   <groupId>org.springframework.boot</groupId>	   <artifactId>spring-boot-starter-web</artifactId>	</dependency>    </dependencies>3.实体类

package com.xx.entity;public class User {	private int id;	private String name;	public User() {	}	public User(int id, String name) {		this.id = id;		this.name = name;	}	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;	}}4.Controller类

package com.xx.controller;import org.springframework.web.bind.annotation.*;import com.xx.entity.User;@RestControllerpublic class SimpleController {	@RequestMapping("/hello")	String hello() {		return "Hello World!";	}	@RequestMapping("/user/{id}")	User user(@PathVariable("id") int id) {		User user = new User(id, "yourName");		return user;	}}5.Application类

package com.xx;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application {	public static void main(String[] args) throws Exception {        SpringApplication.run(Application.class);    }}

6.Spring Boot建议将我们main方法所在的主要的配置类配置在根包名下。

在这个工程里“main方法所在的主要的配置类”为Application类。(见顶部目录图)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表