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

 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;
    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot项目中整合MyBatis框架实现增删改查操作,首先需要在项目的pom文件中添加MyBatis Spring Boot Starter依赖。可以通过以下代码将依赖添加到pom文件中: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> ``` 接下来,我们需要创建一个Spring Boot项目。可以使用Spring Initializr或其他方式创建一个基本的Spring Boot项目。 在创建完项目后,需要进行一些配置。可以在application.properties或application.yml文件中配置数据库连接信息,如数据库URL、用户名、密码等。 接下来,需要创建实体类和Mapper接口。实体类对应数据库表的字段,Mapper接口定义了对应实体类的增删改查方法。 在Mapper接口中,可以使用MyBatis注解或XML文件配置SQL语句与方法的映射关系。可以使用@Insert、@Select、@Update、@Delete等注解定义SQL语句,并在方法中调用这些注解。 最后,在Service层或Controller层中调用Mapper接口的方法实现相应的增删改查操作。 这样就完成了在Spring Boot项目中整合MyBatis框架实现增删改查操作的过程。通过配置依赖、数据库连接信息、创建实体类和Mapper接口、配置SQL语句,最后在Service或Controller层中调用Mapper接口的方法,可以实现对数据库的增删改查操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring Boot整合Mybatis实现增删改查](https://blog.csdn.net/qq_45764233/article/details/127064407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Spring boot整合Mybatis实现增删改查](https://download.csdn.net/download/weixin_42673574/87953723)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值