在上一节的springboot工程中添加controller层。目录结构
1、pom.xml中添加congtroller层的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、添加controller层
在com.lingyi.mybatis.controller包下添加PersonController.java类,里面添加resful的接口
package com.lingyi.mybatis.controller;
import com.lingyi.mybatis.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
//属性注入
@Autowired
private PersonService personService;
@RequestMapping("/")
String home() {
System.out.println("hello world");
return "Hello springboot!";
}
@RequestMapping("/personcout")
public long getCount(){
return personService.count();
}
}
3、启动springboot程序
运行application.java入口程序
在浏览器中访问127.0.0.1:8081