使用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);
}
}
总结:
编写过程注意按照数据库字段进行增删改查。