当前位置: 首页 > news >正文

MybatisPlus-CRUD,不带条件构造器的常用方法

mapper层
@Repository
public interface UserMapper extends BaseMapper<User> 

BaseMapper中封装好了增删改查的方法

后面直接调用就好了

测试类

@SpringBootTest
public class CrudTest {@Autowiredprivate UserMapper userMapper;//新增@Testpublic void insert(){//没有返回值,不需要调用User user =new User();user.setAge(23);user.setName("llx");user.setEmail("lao123@123.com");int result = userMapper.insert(user);//新增一条数据,需要创建对象,赋值System.out.println(result);}//根据id删除@Testpublic void deleteById(){userMapper.deleteById("1687729477728641025");}//根据Map类型删除@Testpublic void deleteByMap(){Map<String,Object> map = new HashMap<>();map.put("name","Billie");//设置根据哪些值删除map.put("age",24);userMapper.deleteByMap(map);//需要map作为参数}//批量删除@Testpublic void deletByCatchId(){List<Long> list = Arrays.asList(1L, 2L);//数据库是long类型的//Arrays.asList(1L, 2L)将数据转换为List集合int result = userMapper.deleteBatchIds(list);System.out.println(result);}//通过id修改@Testpublic void UpdateById(){User user = new User();user.setId(3L);//需要设置iduser.setAge(30);int result = userMapper.updateById(user);System.out.println(result);}//通过对id进行查询@Testpublic void testSelectById(){User user = userMapper.selectById(3L);System.out.println(user);}//批量查询,注意Arrays.asList(3L, 4L),这个是把要查询的id放到数组里@Testpublic void testSelectByBatchIds(){List<Long> list = Arrays.asList(3L, 4L);List<User> users = userMapper.selectBatchIds(list);System.out.println(users);}//通过map进行查询@Testpublic void testSelectBymaps(){Map<String,Object> map = new HashMap<>();map.put("name","Sandy");map.put("age",21);List<User> users = userMapper.selectByMap(map);//需要创建map对象}//查询全部,有条件构造器,查询全部可以用null@Testpublic void testSelectByAll(){List<User> users = userMapper.selectList(null);//条件构造器,没有条件的时候可以使用nullusers.forEach(System.out::println);}
}

-----------------------------------------------------------------

public interface UserService extends IService<User> {
}
@Service//标识为一个组件
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
}

ServiceImpl中封装了方法(特别注意批量添加只有serviceImpl中有)

测试类

@SpringBootTest
public class MybatisPlusServiceTest {@Autowiredprivate UserService userService;@Testpublic void testCount(){long count = userService.count();System.out.println("总记录数为:"+count);}//测试批量添加,只有service层中有批量添加//userService.saveOrUpdate();有id修改,无id添加@Testpublic void testInsertMore(){List<User> list = new ArrayList<>();for (int i = 0; i < 10; i++) {User user = new User();user.setName("abc"+i);user.setAge(20+i);list.add(user);}boolean b = userService.saveBatch(list);System.out.println(b);}
}

http://www.lryc.cn/news/110896.html

相关文章:

  • 软件测试面试【富途面经分享】
  • antd 库的 Table 组件中删除一个或多个选中的列表
  • 针对java程序员的了解细节操作系统与进程
  • 判定是否互为字符重排、回文排列
  • QT QTextCharFormat 说明和使用
  • 掌握Memory Profiler技巧:识别内存问题
  • Linux学习之正则表达式元字符和grep命令
  • 熟练掌握ChatGPT解决复杂问题——学会提问
  • JVM之类加载与字节码
  • 【博客688】如何实现keepalived vip监控与告警
  • [QT编程系列-39]:用户界面UI - 样式表QSS与样式文件快速入门
  • 机器学习和深度学习简述
  • diffusion model2 扩散模型的文本信息融合、交叉注意力机制、lora
  • 数据结构——二叉树
  • 架构训练营学习笔记:5-3接口高可用
  • 【笔记】湖仓一体架构演进与发展
  • 政务云建设与应用解决方案[42页PPT]
  • 20天突破英语四级高频词汇——第①天
  • 【网络基础实战之路】基于MGRE多点协议的实战详解
  • K8s实战入门(三)
  • Linux-centos花生壳实现内网穿透
  • Jackson类层次结构中的一些应用(Inheritance with Jackson)
  • Python求均值、方差、标准偏差SD、相对标准偏差RSD
  • SQL ASNI where from group order 顺序
  • springboot(39) : RestTemplate完全体
  • python中计算2的32次方减1,python怎么算2的3次方
  • 阿里云SLB负载均衡ALB、CLB和NLB有什么区别?
  • SynergyNet(头部姿态估计 Head Pose Estimation)复现 demo测试
  • mysql高级(尚硅谷-夏磊)
  • C++实用技术(二)std::function和bind绑定器