SpringBoot+MybatisPlus实现根据ID查询

764 阅读1分钟

SpringBoot+MybatisPlus实现根据ID查询

entity实体类层

package com.piesat.sas.module.tdtkey.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;


@Accessors(chain = true)
@TableName("tdt_key_view")
@Data
public class TdtKey implements Serializable {
    @TableId(type = IdType.AUTO)
    private Long f_id;
    private String f_key;
}

mapper层

package com.piesat.sas.module.tdtkey.mapper;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
@Mapper
public interface TdtKeyMapper  {
    @Select("select f_key from tdt_key_view where f_id = #{f_id}")
   String selectById(Long f_id);
}

service实现类

package com.piesat.sas.module.tdtkey.service.impl;

import com.jgdf.core.web.ReturnFormat;
import com.piesat.sas.module.tdtkey.mapper.TdtKeyMapper;
import com.piesat.sas.module.tdtkey.service.TdtKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TdtKeyServiceImpl implements TdtKeyService {
   @Autowired
   private TdtKeyMapper tdtKeyMapper;
    @Override
    public String selectById(Long f_id){
        System.out.println("-------"+tdtKeyMapper.selectById(f_id));
        return ReturnFormat.retParam(tdtKeyMapper.selectById(f_id));
    }
}

接口

package com.piesat.sas.module.tdtkey.service;


public interface TdtKeyService  {
    String selectById(Long f_id);
}

controller控制器

package com.piesat.sas.module.tdtkey.controller;


import com.piesat.sas.module.tdtkey.service.TdtKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*根据用户登录的id获取天地图的key*/
@RestController
@RequestMapping("/api/tdtkey")
public class TdtKeyController {
    @Autowired
    private TdtKeyService tdtKeyService;
    @GetMapping("/select")
    public String selectById(@RequestParam(value = "f_id")  Long f_id){
        System.out.println("---------"+ tdtKeyService.selectById(f_id));
        return  tdtKeyService.selectById(f_id);
    }
}

实现结果

API测试.png