服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis-plus中BaseMapper入门使用

mybatis-plus中BaseMapper入门使用

2021-11-22 13:30大田酱紫 Java教程

本文主要介绍了mybatis-plus中BaseMapper入门使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

具体教程参考官网文档: baomidou.com/

入门使用BaseMapper完成增删改查

根据数据库表制作相应实体类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@TableName(value = "user")
public class User implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
 
    private String name;
 
    private String password;
 
    private String username;
    // 省略set,get
    }

创建对应mapper类

?
1
2
3
public interface UserMapper extends BaseMapper<User> {
 //这里什么都不用写
}

由于BaseMapper已经集成了基础的增删改查方法,这里对应的mapper.xml也是不用写的

添加关于mapper包的注册

?
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan("com.hyx.mybatisplusdemo.mapper")
public class MybatisplusdemoApplication {
 
 public static void main(String[] args) {
  SpringApplication.run(MybatisplusdemoApplication.class, args);
 }
 
}

修改配置文件

?
1
2
3
4
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringBootTest
class MybatisplusdemoApplicationTests {
 
 @Autowired
 UserMapper userMapper;
 
 @Test
 void contextLoads() {
  
  User user = userMapper.selectById(7l);
  userMapper.deleteById(user);
  System.out.println(user);
 }
 
}

如果要自定义一些增删改查方法,可以在配置类中添加:

?
1
2
3
4
##mybatis-plus mapper xml 文件地址
mybatis-plus.mapper-locations= classpath*:mapper/*Mapper.xml
##mybatis-plus type-aliases 文件地址
mybatis-plus.type-aliases-package= com.hyx.mybatisplusdemo.entity

然后就与mybatis一样,创建对应的xml文件,去实现相应的方法就可以了

BaseMapper各方法详解

Insert

?
1
2
// 插入一条记录
int insert(T entity);

Delete

?
1
2
3
4
5
6
7
8
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

Update

?
1
2
3
4
// 根据 whereEntity 条件,更新记录
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

Select

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

到此这篇关于mybatis-plus中BaseMapper入门使用的文章就介绍到这了,更多相关mybatis-plus BaseMapper入门内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6904901352959246344

延伸 · 阅读

精彩推荐