美文网首页SpringBoot全家桶
SpringBoot 全家桶 | MyBatisPlus(二)提

SpringBoot 全家桶 | MyBatisPlus(二)提

作者: 码农StayUp | 来源:发表于2020-09-22 15:10 被阅读0次

本文源码:Gitee·点这里

继承了BaseMapper<T>接口后,便可实现表的基础CRUD功能了,下面我们来看看具体内容。

BaseMapper<T>接口源码:

public interface BaseMapper<T> extends Mapper<T> {

    /**
     * 插入一条记录
     *
     * @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<String, Object> columnMap);

    /**
     * 根据 entity 条件,删除记录
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> 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<T> updateWrapper);

    /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    T selectById(Serializable id);

    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

基础CRUD

插入一条记录

@Test
public void testInsert() {
    User user = new User();
    user.setName("Lili");
    user.setAge(0);
    user.setEmail("lili@163.com");
    userMapper.insert(user);
}

执行日志:

JDBC Connection [HikariProxyConnection@648176342 wrapping com.mysql.cj.jdbc.ConnectionImpl@11bd803] will not be managed by Spring
==>  Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1307918819829981186(Long), Lili(String), 0(Integer), lili@163.com(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30506c0d]

根据 ID 删除

@Test
public void testDeleteById() {
    int result = userMapper.deleteById(1307918819829981186L);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@693275170 wrapping com.mysql.cj.jdbc.ConnectionImpl@7ea08277] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1307918819829981186(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62e6a3ec]
1

根据 ID 批量删除

@Test
public void testDeleteBatchIds() {
    int result = userMapper.deleteBatchIds(Arrays.asList(1307916018324221953L, 1307916038335238146L));
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@1299885218 wrapping com.mysql.cj.jdbc.ConnectionImpl@604b1e1d] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? )
==> Parameters: 1307917913344421889(Long), 1307918050477113346(Long)
<==    Updates: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5754de72]
2

根据 ID 修改

@Test
public void testUpdateById() {
    User user = new User();
    user.setId(5L);
    user.setAge(10);
    int result = userMapper.updateById(user);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@719146276 wrapping com.mysql.cj.jdbc.ConnectionImpl@3f93e4a8] will not be managed by Spring
==>  Preparing: UPDATE user SET age=? WHERE id=?
==> Parameters: 10(Integer), 5(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5754de72]
1

根据 ID 查询

@Test
public void testSelectById() {
    User user = userMapper.selectById(5L);
    System.out.println(user);
}

执行日志:

JDBC Connection [HikariProxyConnection@1286643712 wrapping com.mysql.cj.jdbc.ConnectionImpl@5ff2e84b] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Long)
<==    Columns: id, name, age, email
<==        Row: 5, Billie, 10, test5@baomidou.com
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62e6a3ec]
User{id=5, name='Billie', age=10, email='test5@baomidou.com'}

根据 ID 批量查询

@Test
public void testSelectBatchIds() {
    List<User> users = userMapper.selectBatchIds(Arrays.asList(4L, 5L));
    System.out.println(users);
}

执行日志:

JDBC Connection [HikariProxyConnection@1970900227 wrapping com.mysql.cj.jdbc.ConnectionImpl@40f8f5a8] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
==> Parameters: 4(Long), 5(Long)
<==    Columns: id, name, age, email
<==        Row: 4, Sandy, 21, test4@baomidou.com
<==        Row: 5, Billie, 10, test5@baomidou.com
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c81fd12]
[User{id=4, name='Sandy', age=21, email='test4@baomidou.com'}, User{id=5, name='Billie', age=10, email='test5@baomidou.com'}]

columnMap参数

根据 columnMap 条件删除记录

删除年龄为18岁的用户

@Test
public void testDeleteByMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("age", 18);
    int result = userMapper.deleteByMap(map);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@874981105 wrapping com.mysql.cj.jdbc.ConnectionImpl@c808207] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE age = ?
==> Parameters: 18(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@31ee96f4]
1

根据 columnMap 条件查询

查看年龄为28岁的用户

@Test
public void testSelectByMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("age", 28);
    List<User> users = userMapper.selectByMap(map);
    System.out.println(users);
}

执行日志:

JDBC Connection [HikariProxyConnection@1183572822 wrapping com.mysql.cj.jdbc.ConnectionImpl@5c9ac4cc] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE age = ?
==> Parameters: 28(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 28, test3@baomidou.com
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@652ab8d9]
[User{id=3, name='Tom', age=28, email='test3@baomidou.com'}]

相关文章

网友评论

    本文标题:SpringBoot 全家桶 | MyBatisPlus(二)提

    本文链接:https://www.haomeiwen.com/subject/ktpwyktx.html