Java知识点总结:想看的可以从这里进入

目录

3、相关方法3.1、BaseMapper接口3.1.1、新增3.1.2、删除3.1.3、修改3.1.4、查询

3、相关方法

3.1、BaseMapper接口

MyBatis-Plus中的基本CRUD在内置的 BaseMapper 中都已得到了实现,我们可以直接使用,这样大大的简化了开发效率。

/**

* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能

*/

public interface BaseMapper extends Mapper {

//插入一条记录

int insert(T entity);

//根据 ID 删除

int deleteById(Serializable id);

//根据实体(ID)删除

int deleteById(T entity);

//根据 columnMap 条件,删除记录

int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

// 删除记录,条件生成器根据entity生成where后的条件

int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper);

//删除(根据ID或实体 批量删除)

int deleteBatchIds(@Param(Constants.COLL) Collection idList);

// 根据 ID 修改

int updateById(@Param(Constants.ENTITY) T entity);

// 根据 whereEntity 条件,更新记录

int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);

//根据 ID 查询

T selectById(Serializable id);

//查询(根据ID 批量查询)

List selectBatchIds(@Param(Constants.COLL) Collection idList);

// 查询(根据 columnMap 条件)

List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

//查询一条记录

default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) {

.......

}

//根据 Wrapper 条件,判断是否存在记录

default boolean exists(Wrapper queryWrapper) {

.....

}

// 根据 Wrapper 条件,查询总记录数

Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);

//根据 entity 条件,查询全部记录

List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

//根据 Wrapper 条件,查询全部记录

List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);

//根据 Wrapper 条件,查询全部记录

List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)

> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

//根据 Wrapper 条件,查询全部记录(并翻页)

>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

}

3.1.1、新增

在封装的 BaseMapper 中只有一个插入语句,因为海量数据插入单条SQL无法实行,所以mybatis-plus 把批量插入放在了Service 层对应的封装接口里了。

@Resource

private UserMapper userMapper;

@Test

public void test2(){

//新增数据

User user1 = new User();

user1.setUsername("mybatisplus测试");

user1.setPassword("12312312");

int insert = userMapper.insert(user1);

System.out.println("受影响行数:"+insert);

//自动获取id(默认使用雪花算法生成的id)

Integer id = user1.getUserId();

//获取数据

User user = userMapper.selectById(id);

System.out.println(user);

}

这个ID是MyBatis-Plus基于雪花算法生成的一个ID。之所以会为负数,是因为数据库设置的是自增,而在User实体类中没有进行设置

//IdType.ID_WORKER_STR 默认的;底层使用了雪花算法;类型为Integer

//IdType.AUTO 数据库自增;数据库上必须设置为自增

//IdType.NONE 没有设置主键类型;跟随全局;全局的主键策略如果没有设置,默认是雪花算法

//IdType.INPUT 手动输入;必须手动输入,数据库自增也没用;

//IdType.UUID 全局唯一id;无序;字符串;

//ID_WORKER_STR 全局唯一(idWorker的字符串表示);

@TableId(value = "user_id",type = IdType.AUTO)

private Integer userId;

设置完成后,将数据库之前测试的数据删除,重新运行一下

3.1.2、删除

数据 删除一行数据 //通过id删除一行数据

int i = userMapper.deleteById(15);

System.out.println("受影响行数:"+i);

批量删除 //批量删除

List idList = Arrays.asList(16,17, 18);

int i1 = userMapper.deleteBatchIds(idList);

System.out.println("受影响行数:"+i1 );

通过Map设置条件 //根据条件删除

Map map = new HashMap<>();

map.put("username","新增");

map.put("password","456454");

//删除username为新增,且password为456454的数据

int i = userMapper.deleteByMap(map);

System.out.println("受影响行数:"+ i );

3.1.3、修改

User user = userMapper.selectById(11);

System.out.println(user);

//对user进行修改

user.setUsername("修改");

user.setPassword("2131278");

int i = userMapper.updateById(user);

System.out.println("受影响行数:"+ i );

//修改后的User

User user1 = userMapper.selectById(11);

System.out.println(user1);

3.1.4、查询

查询一条数据 //根据ID查询

User user = userMapper.selectById(1);

System.out.println(user);

//根据 LambdaQueryWrapper 的条件查询

User user1 = userMapper.selectOne(new LambdaQueryWrapper().like(User::getUsername,"root"));

System.out.println(user1);

根据ID批量查询 //根据ID批量查询

List list = Arrays.asList(1, 2, 3);

List users = userMapper.selectBatchIds(list);

users.forEach(System.out::println);

通过map条件查询,键值对应数据的字段和值。 //通过map条件查询

Map map = new HashMap<>();

map.put("username","root");

List users = userMapper.selectByMap(map);

users.forEach(System.out::println);

查询所有数据 //查询所有数据,参数为null

List users = userMapper.selectList(null);

users.forEach(System.out::println);

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 

发表评论

返回顶部暗黑模式