Mybatis-plus的基本Crud操作

BaseMapper

1、测试BaseMapper的新增功能

@Test
    public void testInsert(){
    	//实现新增数据的功能
        //INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        User user=new User();
        user.setName("张三");
        user.setAge(18);
        user.setEmail("zhangsan@qq.com");
        int result = userMapper.insert(user);
        System.out.println("result:"+result);
        System.out.println("id"+user.getId());
    }

在这里插入图片描述

Ps:这个id是通过雪花算法算出来的id

2、测试BaseMapper的删除功能

在BaseMapper中有三种删除方式
1、根据id删除

@Test
    public void testDelete(){
        int i = userMapper.deleteById(1503267791384346626L);
        System.out.println("result"+i);
    }

如果单纯加入id则会保错,原因是数字过大,需要在数字后面添加L,表示为Long类型
在这里插入图片描述
2、根据集合删除

@Test
    public void testDelete2(){
        //通过map删除用户信息
        //DELETE FROM user WHERE name = ? AND age = ?
        Map<String,Object> map=new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        int result=userMapper.deleteByMap(map);
        System.out.println("result:"+result);
    }

在这里插入图片描述
3、根据多个id进行删除

@Test
    public void testDelete3(){
        //通过多个ID进行删除
        //DELETE FROM user WHERE id IN ( ? , ? , ? )
        List<Long> list = Arrays.asList(1L, 2L, 3L);
        int result = userMapper.deleteBatchIds(list);
        System.out.println("result:"+result);
    }

1L:数字后面带个L没问题,因为本身id就定义为Long类型
在这里插入图片描述

3、测试BaseMapper的更改功能

@Test
    public void testUpdate(){
        //根据id进行修改
        //UPDATE user SET name=?, email=? WHERE id=?
        User user=new User();
        user.setId(4L);
        user.setName("李四");
        user.setEmail("lisi@lisi.com");
        int result = userMapper.updateById(user);
        System.out.println("result:"+result);
    }

age我们没有设置,所以age属性不会被修改
在这里插入图片描述

4、测试BaseMapper的查询功能

把数据库信息添加代码重新运行下(可看上一篇博客)
1、根据id进行查询操作

@Test
    public void testselect1(){
        //根据id进行查询操作
        //SELECT id,name,age,email FROM user WHERE id=?
        User user = userMapper.selectById(1L);
        System.out.println(user);
    }

在这里插入图片描述
2、根据多个id进行查询

@Test
    public void testselect2(){
        //根据多个id进行查询操作
        //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
        List<Long> list = Arrays.asList(1L, 2L, 3L);
        List<User> users = userMapper.selectBatchIds(list);
        users.forEach(System.out::println);
    }

在这里插入图片描述

3、根据map集合的条件查询

@Test
    public void testselect3(){
        //根据map集合条件进行查询操作
        //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        Map<String,Object> map=new HashMap<>();
        map.put("name","jone");
        map.put("age","18");
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }

在这里插入图片描述
4、查询所有数据

@Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        //SELECT id,name,age,email FROM user
        //查询所有的用户
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

5、自定义功能

有时候这些基本的crud如法满足我们的需求,需要我们自己自定义功能
先自定义模板,在file–>setting然后按照以下操作即可

在这里插入图片描述

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="">
    
</mapper>

最后在resource包右键即可找到,创建文件UserMapper
在这里插入图片描述
修改UserMapper接口

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;

import java.util.Map;

public interface UserMapper extends BaseMapper<User> {
    //根据id查询用户信息为map集合
    Map<String,Object> selectMapById(Long id);
}

修改UserMapper.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.mapper.UserMapper">
    <!--Map<String,Object> selectMapById(Long id);-->
    <select id="selectMapById" resultType="map">
        select * from user where id=#{id}
    </select>
</mapper>

测试

@Test
    public void test2(){
        //测试自定义功能
        //select * from user where id=?
        Map<String, Object> map = userMapper.selectMapById(1L);
        System.out.println(map);
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值