Mybatis-plus BaseMapper增删改查

使用BaseMapper 对数据库单表进行增删改查。

代码结构:

查看实体类

package com.example.ball.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;


@Data
@TableName("team")
public class Ball {

    private Integer id;
    private String leftName;
    private String rightName;
    private Integer leftScore;
    private Integer rightScore;
    private Date createDate;

}

查看Mapper

package com.example.ball.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ball.entity.Ball;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface BallMapper extends BaseMapper<Ball> {
}

查看service

package com.example.ball.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ball.entity.Ball;

public interface BallService extends IService<Ball> {
}

查看Impl

package com.example.ball.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ball.entity.Ball;
import com.example.ball.mapper.BallMapper;
import com.example.ball.service.BallService;
import org.springframework.stereotype.Service;

@Service
public class BallServiceImpl extends ServiceImpl<BallMapper, Ball> implements BallService {
}

controller自己编写

测试类中进行增删改查

增加:

@Test
    public void testInsert(){
        Ball ball = new Ball();
        ball.setLeftName("凯尔特人");
        ball.setRightName("老鹰");
        ball.setLeftScore(115);
        ball.setRightScore(98);
        ballMapper.insert(ball);
    }

删除:

 @Test
    public void testdelete(){
       ballMapper.deleteById(1125683202L);
        // map 集合 多个约束
        Map<String,Object>map = new HashMap<>();
        map.put("id",1125683202L);
        ballMapper.deleteByMap(map);
//       列表多个选择批量删除
       List<Integer>list=Arrays.asList(1,2);
       ballMapper.deleteBatchIds(list);

修改:

  @Test
    public void testupdate(){
        Ball ball = new Ball();
        ball.setId(3);
        ball.setRightScore(117);
        ballMapper.updateById(ball);
    }

查询:

 @Test
    public void testselect(){
        List<Integer>list=Arrays.asList(3,4);
        ballMapper.selectBatchIds(list);


        Map<String,Object>map = new HashMap<>();
        map.put("id",4);
        ballMapper.selectByMap(map);

        System.out.println("查询所有");
        ballMapper.selectList(null);


    }

完整代码:

package com.example.ball;
import com.example.ball.entity.Ball;
import com.example.ball.mapper.BallMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class BallApplicationTests {
@Autowired
    private BallMapper ballMapper;

    @Test
    void contextLoads() {
    }

//    BaseMaepper CURD 增删改查

//    增加
    @Test
    public void testInsert(){
        Ball ball = new Ball();
        ball.setLeftName("凯尔特人");
        ball.setRightName("老鹰");
        ball.setLeftScore(115);
        ball.setRightScore(98);
        ballMapper.insert(ball);
    }
    // sql INSERT INTO team ( id, left_name, right_name, left_score, right_score ) VALUES ( ?, ?, ?, ?, ? )
//   删除
    @Test
    public void testdelete(){
       ballMapper.deleteById(1125683202L);
        // map 集合 多个约束
        Map<String,Object>map = new HashMap<>();
        map.put("id",1125683202L);
        ballMapper.deleteByMap(map);
//       列表多个选择批量删除
       List<Integer>list=Arrays.asList(1,2);
       ballMapper.deleteBatchIds(list);

    }
    //修改
    @Test
    public void testupdate(){
        Ball ball = new Ball();
        ball.setId(3);
        ball.setRightScore(117);
        ballMapper.updateById(ball);
    }
    // 查询
    @Test
    public void testselect(){
        List<Integer>list=Arrays.asList(3,4);
        ballMapper.selectBatchIds(list);


        Map<String,Object>map = new HashMap<>();
        map.put("id",4);
        ballMapper.selectByMap(map);

        System.out.println("查询所有");
        ballMapper.selectList(null);


    }


}

总结:

编写过程注意按照数据库字段进行增删改查。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

湙泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值