SpringBoot-手动创建

1、创建maven项目

2、修改pom.xml文件

<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>afei.springboot</groupId>
	<artifactId>SpringBoot_Self</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.19.RELEASE</version>
		<relativePath></relativePath>
	</parent>

	<dependencies>
		<!-- 导入web模块 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

</project>

3、service包

接口EmployeeService

package afei.springboot.service;

import afei.springboot.entities.Employee;

public interface EmployeeService {
	/**
	 * 根据员工id查询员工
	 * @return
	 */
	Employee getEmployee();
}

子包impl,实现类EmployeeServiceImpl

package afei.springboot.service.impl;

import org.springframework.stereotype.Service;

import afei.springboot.entities.Department;
import afei.springboot.entities.Employee;
import afei.springboot.service.EmployeeService;

@Service
public class EmployeeServiceImpl implements EmployeeService {
	
	@Override
	public Employee getEmployee() {
		return new Employee("afei", 2, new Department("dev",2));
	}

}

4、handler包

EmployeeHandler

package afei.springboot.handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import afei.springboot.entities.Department;
import afei.springboot.entities.Employee;
import afei.springboot.service.EmployeeService;

/*
 * @RestController=@Controller+@ResponseBody
 */
@RestController
public class EmployeeHandler {
	@Autowired
	private EmployeeService employeeService;
	/*
	 * 1、@RestController=@Controller+@ResponseBody
	 * 2、由于加了注解@ResponseBody,所以服务器会以字符串的方式直接返回对象
	 * 但是SpringBoot集成了jackson模块,会将字符串以json串的形式返回
	 */

	@RequestMapping("/getEmp")
	public Object getEmployee() {
		//create object
		Employee emp = new Employee("afei",1,new Department("dev",1));
		//create a map,put the object into it
		Map<String,Object> map = new HashMap<>();
		map.put("emp", emp);
		return map;
	}
	@RequestMapping("/getEmp1")
	public Object getEmployee1() {
		//get a Employee object by Autowired
		Employee emp = employeeService.getEmployee();
		//create a map,put the object into it
		Map<String,Object> map = new HashMap<>();
		map.put("emp", emp);
		return map;
	}
}

 

5、springboot/SpringBootSelfApplication.java

package afei.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/*
 * @ComponentScan
 * 设置自动扫描的包,如果不设置默认扫描当前类所在的包及其子包
 */
@ComponentScan(basePackages="afei.springboot")
@SpringBootApplication
public class SpringBootSelfApplication {
	
	/*
	 *	resources/application.properties里面的server.context-path=/
	 *	即在浏览器端发送请求的时候不用再加项目名称,直接发送请求名称即可
	 *	eg:http://localhost:8080/getEmp
	 */
	public static void main(String[] args) {
		SpringApplication.run(SpringBootSelfApplication.class, args);
	}

}

6、resources/application.properties

server.context-path=/
server.port=8080
server.session.timeout=60
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongfeicao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值