mybatis-plus根据实体和mapper生成crud方法,无需写xml文件

本文介绍了如何在Java项目中使用Mybatis-Plus进行数据库操作,包括实体类的定义、mapper接口的创建以及Service层的简化调用,展示了增删改查的基本操作方法。
摘要由CSDN通过智能技术生成

首先确保代码已经集成了mybatis-plus

实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description: 图册类型实体
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("atlas")
public class Atlas implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableId(type = IdType.AUTO)
    private Integer id;

    // 图册名称
    private String atlasName;

    // 图册类型(ls-联锁,zb自闭,jc-监测,lk-列控)
    private String atlasType;

}

mapper接口

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AtlasMapper extends BaseMapper<Atlas> {
}

不需要写任何方法,接下来直接在service中调用mapper

		//无条件查询全部数据
		List<Atlas> atlasList = atlasMapper.selectList(null);
		
		//根据id查询数据
		Atlas atlas = atlasMapper.selectById(id);
		
		//根据条件查询数据
		LambdaQueryWrapper<Command> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Command::getCommandType, command.getCommandType());
        queryWrapper.eq(Command::getCommand, command.getCommand()); //有多少个查询条件字段就写多少条
        Command one = commandMapper.selectOne(queryWrapper);

		//新增数据
		DingXingTuDataJSONDO tuDataJSONDO = new DingXingTuDataJSONDO();
        tuDataJSONDO.setTuzhiCode(code);
        tuDataJSONDO.setTuzhiName(name);
        tuDataJSONDO.setTuzhiDataJson(str);
        boolean save = commandMapper.save(tuDataJSONDO);
		
		//根据条件删除数据
		 LambdaQueryWrapper<DingXingTuDataJSONDO> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(DingXingTuDataJSONDO::getTuzhiCode, code);
         DingXingTuDataJSONDO one = commandMapper.getOne(queryWrapper);	//就是先查询出来再删除
         boolean remove= commandMapper.removeById(one);
         boolean b = dingXingTuDataJsonService.updateById(tuDataJSONDO);

		//根据id更新数据
		 tuDataJSONDO.setTuzhiCode(code);
		 tuDataJSONDO.setTuzhiName(name);
		 tuDataJSONDO.setTuzhiDataJson(str);
		 boolean b = dingXingTuDataJsonService.updateById(tuDataJSONDO);


还有一种更快捷的方式就是直接继承service,连mapper也不需要了

import com.baomidou.mybatisplus.extension.service.IService;

public interface DingXingTuDataJsonService extends IService<DingXingTuDataJSONDO> {
}

然后直接在controller里面调用,调用service和mapper方法一样

/**
     * 查询定型图数据
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult listDingXingTuDataJson() {
        List<DingXingTuDataJSONDO> list = dingXingTuDataJsonService.list();
        return AjaxResult.success("查询数据成功", list);
    }

    /**
     * 根据id查询定型图数据
     * @param id id
     * @return
     */
    @PostMapping("/getOneById")
    @ResponseBody
    public AjaxResult getOneById(@RequestParam("id") Integer id) {
        DingXingTuDataJSONDO dataJSONDO = dingXingTuDataJsonService.getById(id);
        return AjaxResult.success("查询数据成功", dataJSONDO);
    }

    /**
     * 根据code查询定型图数据
     * @param
     * @return
     */
    @PostMapping("/getOneByCode")
    @ResponseBody
    public AjaxResult getOneByCode(@RequestBody DingXingTuDataJSONDO dataJSONDO) {
        LambdaQueryWrapper<DingXingTuDataJSONDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DingXingTuDataJSONDO::getTuzhiCode, dataJSONDO.getTuzhiCode());
        DingXingTuDataJSONDO one = dingXingTuDataJsonService.getOne(queryWrapper);
        return AjaxResult.success("查询数据成功", one);
    }

    /**
     * 添加定型图图纸数据
     * @param tuDataJSONDO 定型图do
     * @return
     */
    @PostMapping("/addTuZhiData")
    @ResponseBody
    public AjaxResult addOne(@RequestBody DingXingTuDataJSONDO tuDataJSONDO) {
        boolean save = dingXingTuDataJsonService.save(tuDataJSONDO);
        return AjaxResult.success("添加成功");
    }

    /**
     * 更新图纸数据
     * @param tuDataJSONDO 图纸数据
     * @return
     */
    @PostMapping("/updateTuZhiData")
    @ResponseBody
    public AjaxResult updateOne(@RequestBody DingXingTuDataJSONDO tuDataJSONDO) {
        String tuzhiSpecialChar = dingXingTuDataJsonService.resolvingDrawDataJson(tuDataJSONDO.getTuzhiDataJson());
        tuDataJSONDO.setTuzhiSpecialChar(tuzhiSpecialChar);
        boolean b = dingXingTuDataJsonService.updateById(tuDataJSONDO);
        return AjaxResult.success("更新成功",1);
    }

    /**
     * 删除图纸数据
     * @param id 主键id
     * @return
     */
    @PostMapping("/deleteTuZhiData")
    @ResponseBody
    public AjaxResult deleteOne(@RequestParam("id")  Integer id) {
        boolean b = dingXingTuDataJsonService.removeById(id);
        return AjaxResult.success("更新成功");
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值