学习大数据——SpringBoot项目整合案例

集成Spring & SpringMVC

  • 基本的Spring Boot环境已经构建好了(上一篇博客),现在需要配置Spring框架及SpringMVC框架的业务环境

@ComponentScan注解

SpringBootSelfApplication.java:

package com.learn.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/*
 * @ComponentScan注解
 * 	-用来设置自动扫描的包,如果没有指定basePackages属性,默认扫描当前类所在的包及其子包
 */
@ComponentScan(basePackages="com.learn.springboot")
@SpringBootApplication
public class SpringBootSelfApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootSelfApplication.class, args);
	}

}

增加控制器代码、@Controller和@RestController区别

EmployeeHandler.java:

package com.learn.springboot.handler;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.learn.springboot.entities.Department;
import com.learn.springboot.entities.Employee;
import com.learn.springboot.service.EmployeeService;

/*
 * @RestController = @Controller + @ResponseBody
 */
@RestController
//@Controller
public class EmployeeHandler {
	
	@Autowired
	private EmployeeService employeeService;
	
//	@ResponseBody
	@RequestMapping("/getEmp")
	public Object getEmployee() {
		//常见一个map
		Map<String , Object> map = new HashMap();
		//调用EmployeeService获取员工信息
		Employee employee = employeeService.getEmployee();
		//向map中放一个Employee对象
		map.put("emp", new Employee(1, "张三", "[email protected]", new Department(1001,"开发部")));
		return map;
	}
	
	@RequestMapping("/getEmp2")
	public Object getEmployee2() {
		//常见一个map
		Map<String , Object> map = new HashMap();
		//调用EmployeeService获取员工信息
		Employee employee = employeeService.getEmployee();
		//向map中放一个Employee对象
		map.put("emp",employee);
		return map;
	}
}

用到的实体类:

Department.java:

package com.learn.springboot.entities;

public class Department {

	private Integer id;
	private String name;

	public Department() {
		super();
	}

	public Department(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}

}

Employee.java:

package com.learn.springboot.entities;

public class Employee {

	private Integer id;
	private String lastName;
	private String email;
	private Department dept;

	public Employee() {
		super();
	}

	public Employee(Integer id, String lastName, String email, Department dept) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.email = email;
		this.dept = dept;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", dept=" + dept + "]";
	}

}

测试getEmp:
学习大数据——SpringBoot项目整合案例_第1张图片

增加服务层代码

Service接口,ServiceImpl实现类的使用和SSM架构中的使用方式完全相同

Service接口:

package com.learn.springboot.service;

import com.learn.springboot.entities.Employee;

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

Service接口的实现:

package com.learn.springboot.service.impl;

import org.springframework.stereotype.Service;

import com.learn.springboot.entities.Department;
import com.learn.springboot.entities.Employee;
import com.learn.springboot.service.EmployeeService;

@Service
public class EmployeeServiceImpl implements EmployeeService{

	@Override
	public Employee getEmployee() {
		return new Employee(2,"李四","[email protected]",new Department(2,"产品部"));
	}
	
}

测试handler中的getEmp2:
学习大数据——SpringBoot项目整合案例_第2张图片

你可能感兴趣的:(大数据学习,springboot)