Spring Boot MyBatis Postgres 实现对数据表增删改查操作(采用注解方式)

本文详细介绍了使用Java和MyBatis实现服务管理系统的全过程,包括实体类定义、Mapper接口创建及控制类Controller的设计。通过RESTful API实现了服务的增删改查功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.创建Java实体类

package com.example.server;

public class SceneService {

    private  String servicename;
    private  ServiceTypeEnum servicetype;
    private  String serviceurl;

    public String getServicename() {
        return servicename;
    }

    public void setServicename(String servicename) {
        this.servicename = servicename;
    }

    public ServiceTypeEnum getServicetype() {
        return servicetype;
    }

    public void setServicetype(ServiceTypeEnum servicetype) {
        this.servicetype = servicetype;
    }

    public String getServiceurl() {
        return serviceurl;
    }

    public void setServiceurl(String serviceurl) {
        this.serviceurl = serviceurl;
    }
}

2.创建Mapper接口

package com.example.server; 

import org.apache.ibatis.annotations.*;

import java.util.Map;

@Mapper
public interface SceneServicesMapper {


    //按名称查询
    @Select("select * from sys_scene_services_tb where servicename=#{name}")
    public SceneService selectServiceByName(String servicename);

    //查询服务列表
    @Select("select * from sys_scene_services_tb")
    @MapKey("servicename")
    public Map<String,SceneService> selectServices();

    //删除
    @Delete("delete from sys_scene_services_tb where servicename=#{name}")

    public int deleteService(String servicename);

    //修改
    @Update("update sys_scene_services_tb set servicetype=#{servicetype}, serviceurl=#{serviceurl} where servicename=#{servicename}")

    public int updateService(SceneService sceneService);

    //插入
    @Insert("insert into sys_scene_services_tb(servicename,servicetype,serviceurl) values(#{servicename},#{servicetype},#{serviceurl})")
    public int insertService(SceneService sceneService);

}

3. 创建控制类Controller 

package com.example.server.controller;

import com.example.server.SceneService;
import com.example.server.SceneServicesMapper;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController

public class SceneServicesController {

    @Autowired
    SceneServicesMapper sceneServicesMapper;

    //查询
    @RequestMapping("/get/{name}")

    public SceneService getServiceByName(@PathVariable("name") String name) {

        return  sceneServicesMapper.selectServiceByName(name);
    }

   // 查询服务列表
    @RequestMapping("/services")
    public Map<String,SceneService> getServices(){

        return (Map<String, SceneService>) sceneServicesMapper.selectServices();
    }

    //删除
    @RequestMapping("/delete/{name}")
    public String deleteService(@PathVariable("name") String name){

        sceneServicesMapper.deleteService(name);
        return "删除成功!";
    }

    //插入
    @RequestMapping("/insert")

    public SceneService insertService(SceneService sceneService){

        sceneServicesMapper.insertService(sceneService);
        return sceneService;
    }

    //修改
    @RequestMapping("/update")
    public SceneService updateService(SceneService sceneService){

        sceneServicesMapper.updateService(sceneService);
        return sceneService;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值