测试BaseMapper的功能
添加功能
/*** 实现新增用户信息*/@Testpublic void testInsert(){User user = new User();user.setName("张三");user.setAge(23);user.setEmail("zhangsan@xx.com");int result = userMapper.insert(user);System.out.println("result = "+result);System.out.println("id = "+user.getId());}
删除功能
/*** 通过id删除用户信息*/@Testpublic void testDelete(){int result = userMapper.deleteById(1499367002800951298L);System.out.println("result = "+result);}
/*** 通过map集合中设置的条件删除用户信息*/@Testpublic void testDelete01(){Map<String,Object> map = new HashMap<>();map.put("name","张三");map.put("age",23);int result = userMapper.deleteByMap(map);System.out.println("result = "+result);}
/*** 通过多个id实现批量删除*/@Testpublic void testDelete02(){List<Long> list = Arrays.asList(1L, 2L, 3L);int result = userMapper.deleteBatchIds(list);System.out.println("result = "+result);}
修改功能
/*** 修改用户信息* UPDATE user SET name=?, email=? WHERE id=?*/@Testpublic void testUpdate(){User user = new User();user.setId(4L);user.setName("李四");user.setEmail("lisi@xxx.com");int result = userMapper.updateById(user);System.out.println("result = "+result);}
查询功能
/*** 通过id查询用户信息*/@Testpublic void testSelect(){User user = userMapper.selectById(1L);System.out.println(user);}
/*** 通过多个id查询多个用户信息*/@Testpublic void testSelect01(){List<Long> list = Arrays.asList(1L, 2L, 3L);List<User> users = userMapper.selectBatchIds(list);users.forEach(System.out::println);}
/*** 通过map中设置的条件查询*/@Testpublic void testSelect02(){Map<String,Object> map = new HashMap<>();map.put("name","Jack");map.put("age",20);List<User> users = userMapper.selectByMap(map);}
/*** 查询所有数据*/@Testpublic void testSelect02(){List<User> users = userMapper.selectList(null);users.forEach(System.out::println);}
自定义功能
不修改的话就采用mybatisplus的默认路径
UserMapper
package com.xxxx.mybatisplus.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xxxx.mybatisplus.pojo.User;
import org.springframework.stereotype.Repository;import java.util.Map;// 将类或者接口标记为持久层组件 这样就不会看见test类中的红线警告了
@Repository
public interface UserMapper extends BaseMapper<User> {/*** 根据id查询用户信息为map集合* @param id* @return*/Map<String,Object> selectMapById(Long id);}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.mybatisplus.mapper.UserMapper"><!-- Map<String,Object> selectMapById(Long id);--><select id="selectMapById" resultType="map">select id,name,age,email from user where id = #{id}</select></mapper>
测试
@Testpublic void testSelect03(){Map<String,Object> map =userMapper.selectMapById(1L);System.out.println(map);}