基于springboot和mybatis的RealWorld后端项目实战二之实现tag接口

修改pom.xml

在这里插入图片描述
在这里插入图片描述

新增tag数据表

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tags
-- ----------------------------
DROP TABLE IF EXISTS `tags`;
CREATE TABLE `tags` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of tags
-- ----------------------------
INSERT INTO `tags` VALUES ('14', 'java');
INSERT INTO `tags` VALUES ('15', 'javascript');
INSERT INTO `tags` VALUES ('16', 'python');
INSERT INTO `tags` VALUES ('17', 'golang');
INSERT INTO `tags` VALUES ('18', 'php');
INSERT INTO `tags` VALUES ('19', 'dfdsfsd');
INSERT INTO `tags` VALUES ('20', 'fafdaf');
INSERT INTO `tags` VALUES ('21', 'zzzzzz');

src/main下新增resources目录

在这里插入图片描述

resources目录下新增application.yml

server:
  port: 8008
spring:
  profiles:
    active: dev

resources目录下新增application-dev.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3307/my_realworld?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
    type: com.zaxxer.hikari.HikariDataSource

新增以下包和文件

在这里插入图片描述

Tag.java

package org.example.domain;

import lombok.Data;
@Data
public class Tag {
	private Long id;
	private String name;
}

TagRsp.java

package org.example.domain;

import java.util.List;

public class TagRsp {
    public List<String> tags;
}

TagMapper.java

package org.example.mapper;

import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface TagMapper {
	@ResultType(String.class)
	@Select("select name from tags")
	List<String> allTags();

}

TagController.java

package org.example.controller;

import org.example.domain.TagRsp;
import org.example.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api")
public class TagController {
    private final TagMapper tagMapper;

    @Autowired
    public TagController(TagMapper tagMapper) {
        this.tagMapper = tagMapper;
    }

    @GetMapping("/tags")
    public ResponseEntity<?> getTags() {
        TagRsp tagRsp = new TagRsp();
        tagRsp.tags = tagMapper.allTags();
        return ResponseEntity.status(HttpStatus.OK).body(tagRsp);
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 12:44:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 12:44:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 12:44:04       45 阅读
  4. Python语言-面向对象

    2024-07-16 12:44:04       55 阅读

热门阅读

  1. HDMI Retimer

    2024-07-16 12:44:04       17 阅读
  2. Web开发:<p>标签作用

    2024-07-16 12:44:04       20 阅读
  3. 相交线和平行线

    2024-07-16 12:44:04       19 阅读
  4. 微服务框架乾坤沙箱机制实现的原理

    2024-07-16 12:44:04       23 阅读
  5. 力扣题解(交错字符串)

    2024-07-16 12:44:04       23 阅读
  6. 排序-归并排序

    2024-07-16 12:44:04       27 阅读
  7. C#中Dapper的使用教程

    2024-07-16 12:44:04       21 阅读
  8. 运行时动态调整 Pod 的 CPU 及 Memory 资源

    2024-07-16 12:44:04       24 阅读
  9. Python面经

    2024-07-16 12:44:04       22 阅读
  10. Etcd-v3.4.27集群部署

    2024-07-16 12:44:04       19 阅读