最新动态 > 详情
SpringBoot 快速入门搭建项目流程--第一个接口
发布时间:2021-01-06 11:50:13
一、在 项目目录demo/src/main/java/包名/ 下新建controller mapper model service四个package,当然也可以建立其他文件夹,由于main/resources/ 下有静态资源 static 和 模板目录templates,所以不用建立View相关的目录。
二、运行DemoApplication,查看Run控制台信息
这里注意:如果依赖中添加了数据库,则需要在run之前在application.yml中配置数据库,否则会报错
没有报错,直接在浏览器访问:localhost:8080端口,有一个报错页,其实并不是报错,说明项目已经正常运行了
三、日志
在DemoApplication类上方直接添加 @Slf4j 注解,然后在main方法中使用 log.info() 或log.Error等方法即可将日志写到控制台
上一篇我们讲到了配置日志是将日志写入到文件,这种方便上线后查看和调试
1)这里我们创建一个TestModel类,上方注解为:
二、运行DemoApplication,查看Run控制台信息
这里注意:如果依赖中添加了数据库,则需要在run之前在application.yml中配置数据库,否则会报错
没有报错,直接在浏览器访问:localhost:8080端口,有一个报错页,其实并不是报错,说明项目已经正常运行了
三、日志
在DemoApplication类上方直接添加 @Slf4j 注解,然后在main方法中使用 log.info() 或log.Error等方法即可将日志写到控制台
上一篇我们讲到了配置日志是将日志写入到文件,这种方便上线后查看和调试
四、在controller下新建 TestController类
1)在TestController类上方添加注解
2)@RestController 该注解表示是接口
3)添加日志注解 @Slf4j
4)新建方法 test()
5)在方法上方添加注解 @GetMapping("/test") 或 @PostMapping("/test")分别代码get方法和post方法
6)方法内打印一个日志
package com.example.demo.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class TestControllers { @GetMapping("/test") public Object test(){ log.info("请求了test方法"); return null; } } 7)在浏览器请求http://localhost:8080/test IntelliJ控制台显示日志:请求了test方法 接口请求成功!!!五、现在,我在model目录下新建一个testModel类
1)这里我们创建一个TestModel类,上方注解为:
@Data 这里需要加上Data注解,否则无法请求到数据
@TableName (value = "ha_user_order") 指定model对应的数据表,其中value表名 2)在类中私有成员变量上方加入
@TableId(type = IdType.AUTO) 指定自增主键
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName (value = "ha_user_order")
public class TestModel {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userid;
private String unionid;
private String orderid;
}
六、在service下新增TestService interface接口文件,定义testMethod()方法
public interface TestService { Object testMethod(); }
七、在service下新增impl package,并新增TestServiceImpl类文件(此处的快捷操作方式是在TestService接口处 使用 Alt+Enter键,选择implement interface,并选择要重写的方法)1)该类实现TestService 接口,重写testMethod()方法,注意要在类明上方加上注解否则报错:Field tsi in com.example.demo.controller.TestControllers required a bean of type 'com.example.demo.service.impl.TestServiceImpl' that could not be found. 2)利用TestServiceImpl重写的方法获取数据库数据 ,在mapper下新建一个接口TestMapper 首先,在mapper下新建一个接口TestMapper ,该接口继承 BaseMapper,映射的model为TestModel 注意: 在主方法文件类名前加上注解:@MapperScan("com.example.demo.mapper")
否则报错:Field testMapper in com.example.demo.service.impl.TestServiceImpl required a bean of type 'com.example.demo.mapper.TestMapper' that could not be found.
import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface TestMapper extends BaseMapper<TestModel> { } 然后,TestServiceImpl中声明私有变量testMapper 并自动注入TestMapper ; 然后,利用QueryWrapper 来创建数据库查询条件
QueryWrapper<TestModel> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id",350);
此时,可以利用testMapper的查询方法来执行上述查询条件
Object res = testMapper.selectList(queryWrapper); 最后,返回查询结果,自动为JSON格式
return res; 完整代码:
package com.example.demo.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.demo.mapper.TestMapper; import com.example.demo.model.TestModel; import com.example.demo.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override public Object testMethod() { QueryWrapper<TestModel> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id","350"); Object res =this.testMapper.selectList(queryWrapper); return res; } }
八、修改TestController类
通过service来使用model获取数据
import com.example.demo.service.impl.TestServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class TestControllers { @Autowired private TestServiceImpl testService; @GetMapping("/test") public Object test(){ log.info("请求了test方法"); Object o = testService.testMethod(); return o; } } 大工告成![]()