MyBatis-Plus之BaseMapper

MyBatis-Plus之BaseMapper 博主 默语带您 Go to New World.

✍ 个人主页—— 默语 的博客👦🏻

《java 面试题大全》

🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭

《MYSQL从入门到精通》数据库是开发者必会基础之一~

🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄之助。苟未尽善尽美,敬请批评指正,以资改进。!💻⌨

文章顺序及整体目录可查看(点我即可)

1.0 BaseMapper我们先来查看下 BaseMapper 接口中方法有哪些?(大家也可以查看自己调用的mapper里面方法是一样的 我贴出来是为了方便大家查看)

代码语言:javascript代码运行次数:0运行复制/*

* Copyright (c) 2011-2020, baomidou (jobob@qq.com).

*

* Licensed under the Apache License, Version 2.0 (the "License"); you may not

* use this file except in compliance with the License. You may obtain a copy of

* the License at

*

* https://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

* License for the specific language governing permissions and limitations under

* the License.

*/

package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Constants;

import org.apache.ibatis.annotations.Param;

import java.io.Serializable;

import java.util.Collection;

import java.util.List;

import java.util.Map;

/*

:`

.:,

:::,,.

:: `::::::

::` `,:,` .:`

`:: `::::::::.:` `:';,`

::::, .:::` `@++++++++:

`` :::` @+++++++++++#

:::, #++++++++++++++`

,: `::::::;'##++++++++++

.@#@;` ::::::::::::::::::::;

#@####@, :::::::::::::::+#;::.

@@######+@:::::::::::::. #@:;

, @@########':::::::::::: .#''':`

;##@@@+:##########@::::::::::: @#;.,:.

#@@@######++++#####'::::::::: .##+,:#`

@@@@@#####+++++'#####+::::::::` ,`::@#:`

`@@@@#####++++++'#####+#':::::::::::@.

@@@@######+++++''#######+##';::::;':,`

@@@@#####+++++'''#######++++++++++`

#@@#####++++++''########++++++++'

`#@######+++++''+########+++++++;

`@@#####+++++''##########++++++,

@@######+++++'##########+++++#`

@@@@#####+++++############++++;

;#@@@@@####++++##############+++,

@@@@@@@@@@@###@###############++'

@#@@@@@@@@@@@@###################+:

`@#@@@@@@@@@@@@@@###################'`

:@#@@@@@@@@@@@@@@@@@##################,

,@@@@@@@@@@@@@@@@@@@@################;

,#@@@@@@@@@@@@@@@@@@@##############+`

.#@@@@@@@@@@@@@@@@@@#############@,

@@@@@@@@@@@@@@@@@@@###########@,

:#@@@@@@@@@@@@@@@@##########@,

`##@@@@@@@@@@@@@@@########+,

`+@@@@@@@@@@@@@@@#####@:`

`:@@@@@@@@@@@@@@##@;.

`,'@@@@##@@@+;,`

``...``

_ _ /_ _ _/_. ____ / _

/ / //_//_//_|/ /_\ /_///_/_\ Talk is cheap. Show me the code.

_/ /

*/

/**

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

*

这个 Mapper 支持 id 泛型

*

* @author hubin

* @since 2016-01-23

*/

public interface BaseMapper extends Mapper {

/**

* 插入一条记录

*

* @param entity 实体对象

*/

int insert(T entity);

/**

* 根据 ID 删除

*

* @param id 主键ID

*/

int deleteById(Serializable id);

/**

* 根据 columnMap 条件,删除记录

*

* @param columnMap 表字段 map 对象

*/

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

/**

* 根据 entity 条件,删除记录

*

* @param wrapper 实体对象封装操作类(可以为 null)

*/

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

/**

* 删除(根据ID 批量删除)

*

* @param idList 主键ID列表(不能为 null 以及 empty)

*/

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

/**

* 根据 ID 修改

*

* @param entity 实体对象

*/

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

/**

* 根据 whereEntity 条件,更新记录

*

* @param entity 实体对象 (set 条件值,可以为 null)

* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)

*/

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

/**

* 根据 ID 查询

*

* @param id 主键ID

*/

T selectById(Serializable id);

/**

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

*

* @param idList 主键ID列表(不能为 null 以及 empty)

*/

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

/**

* 查询(根据 columnMap 条件)

*

* @param columnMap 表字段 map 对象

*/

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

/**

* 根据 entity 条件,查询一条记录

*

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**

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

*

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

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

/**

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

*

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

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

/**

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

*

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

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

/**

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

*

注意: 只返回第一个字段的值

*

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

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

/**

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

*

* @param page 分页查询条件(可以为 RowBounds.DEFAULT)

* @param queryWrapper 实体对象封装操作类(可以为 null)

*/

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

/**

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

*

* @param page 分页查询条件

* @param queryWrapper 实体对象封装操作类

*/

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

}2.0 对 BaseMapper中的方式进行使用2.1 新增用户信息 (insert)代码语言:javascript代码运行次数:0运行复制 @Test

public void testInsert(){

User user = new User();

user.setName("小龙");

user.setAge(22);

user.setEmail("757631644@qq.com");

int insert = userMapper.insert(user);

System.out.println("insert"+insert);

//获取mapper新增的id 使用雪花算法生成的id

System.out.println("id"+user.getId());

}测试结果如下:

2.2 删除用户信息2.2.1 deleteById 根据id删除代码语言:javascript代码运行次数:0运行复制 @Test

public void testDelectById(){

//通过id删除 DELETE FROM user WHERE id=?

int deleteById = userMapper.deleteById(1576958456307400705L);

if (deleteById>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteById"+deleteById);

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: DELETE FROM user WHERE id=?

==> Parameters: 1576958456307400705(Long)

<== Updates: 12.2.2 deleteByMap根据map条件删除代码语言:javascript代码运行次数:0运行复制 @Test

public void testDelectByMap(){

//通过map中设置的条件进行删除

HashMap map = new HashMap<>();

map.put("name","小龙");

map.put("age",22);

int deleteByMap = userMapper.deleteByMap(map);

if (deleteByMap>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteByMap"+deleteByMap);

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: DELETE FROM user WHERE name = ? AND age = ?

==> Parameters: 小龙(String), 22(Integer)

<== Updates: 12.2.3 deleteBatchIds 批量删除代码语言:javascript代码运行次数:0运行复制 @Test

public void testDelectBatchIds(){

//通过id中批量删除

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

int deleteBatchIds = userMapper.deleteBatchIds(longs);

if (deleteBatchIds>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteBatchIds"+deleteBatchIds);

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? )

==> Parameters: 1(Long), 2(Long), 3(Long)

<== Updates: 33.0 修改用户信息3.1 根据id进行修改 updateById代码语言:javascript代码运行次数:0运行复制 @Test

public void testUpdate(){

User user = new User();

user.setId(4L);

user.setName("我是老王");

user.setAge(322);

int updateById = userMapper.updateById(user);

if (updateById>0){

System.out.println("修改成功");

}else {

System.out.println("修改失败");

}

System.out.println("updateById"+updateById);

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: UPDATE user SET name=?, age=? WHERE id=?

==> Parameters: 我是老王(String), 322(Integer), 4(Long)

<== Updates: 14.0 查询用户信息4.1 selectById 根据id查询用户代码语言:javascript代码运行次数:0运行复制@Test

public void testSelect(){

//通过id查询用户信息

User user = userMapper.selectById(1L);

System.out.println(user);

}运行结构如下:

代码语言:javascript代码运行次数:0运行复制==> Parameters: 1(Long)

<== Columns: id, name, age, email

<== Row: 1, Jone, 18, test1@baomidou.com

<== Total: 14.2 selectBatchIds根据多个id查询用户代码语言:javascript代码运行次数:0运行复制 @Test

public void testSelectBatchIds(){

List longs = Arrays.asList(1L, 2L);

//通过多个id查询用户信息

List user = userMapper.selectBatchIds(longs);

System.out.println(user);

System.out.println("---------------------");

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

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )

==> Parameters: 1(Long), 2(Long)

<== Columns: id, name, age, email

<== Row: 1, Jone, 18, test1@baomidou.com

<== Row: 2, Jack, 20, test2@baomidou.com

<== Total: 2

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3f9f71ff]

[User(id=1, name=Jone, age=18, email=test1@baomidou.com), User(id=2, name=Jack, age=20, email=test2@baomidou.com)]

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

User(id=1, name=Jone, age=18, email=test1@baomidou.com)

User(id=2, name=Jack, age=20, email=test2@baomidou.com)4.3 selectBatchIds 通过map集合中的条件查询用户信息代码语言:javascript代码运行次数:0运行复制 @Test

public void testSelectByMap(){

HashMap hashMap = new HashMap<>();

hashMap.put("name","Jack");

hashMap.put("age",20);

//通过map集合中的条件查询用户信息

List user = userMapper.selectByMap(hashMap);

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

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND age = ?

==> Parameters: Jack(String), 20(Integer)

<== Columns: id, name, age, email

<== Row: 2, Jack, 20, test2@baomidou.com

<== Total: 14.3 selectList 通过map集合中的条件查询用户信息代码语言:javascript代码运行次数:0运行复制 @Test

public void testselectList(){

//通过条件构造器查询一个list集合,若没有条件则可设置null

List users = userMapper.selectList(null);

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

}运行结果如下:

代码语言:javascript代码运行次数:0运行复制==> Preparing: SELECT id,name,age,email FROM user

==> Parameters:

<== Columns: id, name, age, email

<== Row: 1, Jone, 18, test1@baomidou.com

<== Row: 2, Jack, 20, test2@baomidou.com

<== Row: 3, Tom, 28, test3@baomidou.com

<== Row: 4, Sandy, 21, test4@baomidou.com

<== Row: 5, Billie, 24, test5@baomidou.com

<== Total: 5

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3f9f71ff]

User(id=1, name=Jone, age=18, email=test1@baomidou.com)

User(id=2, name=Jack, age=20, email=test2@baomidou.com)

User(id=3, name=Tom, age=28, email=test3@baomidou.com)

User(id=4, name=Sandy, age=21, email=test4@baomidou.com)

User(id=5, name=Billie, age=24, email=test5@baomidou.com)基本的CRUD基本到这里就完成了,大家也可尝试其他的一些方法;

5.0 扩展 Test代码集合整理代码语言:javascript代码运行次数:0运行复制package com.example;

import com.example.mapper.UserMapper;

import com.example.pojo.User;

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;

@SpringBootTest

public class MyBatisPlusTest {

@Autowired

private UserMapper userMapper;

/**

* 查询全部

*/

@Test

public void testSelectList(){

//通过条件构造器查询一个list集合,若没有条件则可设置null

List users = userMapper.selectList(null);

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

}

@Test

public void testInsert(){

User user = new User();

user.setName("小龙");

user.setAge(22);

user.setEmail("757631644@qq.com");

int insert = userMapper.insert(user);

System.out.println("insert"+insert);

//获取mapper新增的id 使用雪花算法生成的id

System.out.println("id"+user.getId());

}

@Test

public void testDelectById(){

//通过id删除 DELETE FROM user WHERE id=?

int deleteById = userMapper.deleteById(1576958456307400705L);

if (deleteById>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteById"+deleteById);

}

@Test

public void testDelectByMap(){

//通过map中设置的条件进行删除

HashMap map = new HashMap<>();

map.put("name","小龙");

map.put("age",22);

int deleteByMap = userMapper.deleteByMap(map);

if (deleteByMap>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteByMap"+deleteByMap);

}

@Test

public void testDelectBatchIds(){

//通过id中批量删除

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

int deleteBatchIds = userMapper.deleteBatchIds(longs);

if (deleteBatchIds>0){

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

System.out.println("deleteBatchIds"+deleteBatchIds);

}

/**

* 根据id修改

*/

@Test

public void testUpdate(){

User user = new User();

user.setId(4L);

user.setName("我是老王");

user.setAge(322);

int updateById = userMapper.updateById(user);

if (updateById>0){

System.out.println("修改成功");

}else {

System.out.println("修改失败");

}

System.out.println("updateById"+updateById);

}

@Test

public void testSelect(){

//通过id查询用户信息

User user = userMapper.selectById(1L);

System.out.println(user);

}

@Test

public void testSelectBatchIds(){

List longs = Arrays.asList(1L, 2L);

//通过多个id查询用户信息

List user = userMapper.selectBatchIds(longs);

System.out.println(user);

System.out.println("---------------------");

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

}

@Test

public void testselectList(){

//通过条件构造器查询一个list集合,若没有条件则可设置null

List users = userMapper.selectList(null);

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

}

}

Copyright © 2088 世界杯点球_2022世界杯亚洲预选赛 - ktllb.com All Rights Reserved.
友情链接