通用mapper的查询,selectByPrimaryKey、select、selectByExample等

最近有在用到mybatis的通用mapper的查询,既然接触了索性记录总结一下,以便帮到后来人。

一. 首先,放上mybatis 通用mapper的接口代码Mapper.class:

package tk.mybatis.mapper.common;

import tk.mybatis.mapper.annotation.RegisterMapper;

@RegisterMapper
public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker {
}

对于我们项目来说,很多对数据库表的自定义mapper映射都是继承自通用Mapper的,要想继承,就必须指定泛型。

拿我项目来举例子:假设我有一实体类 Student.java,与我数据库里的Student表属性、字段一一对应,那么我想使用通用Mapper的各种方法,就要这么实现:
在mapper文件夹下新建StudentMapper.java文件,代码如下:

package com.project.business.mapper;


import com.project.common.model.business.Student;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;

/**
 * @Author cyl
 * @Date 2021/10/15 09:55
 * @Version 1.0
 **/
@Component
public interface StudentMapper extends Mapper<Student> {
}

二. 在自己的service目录下定义好service以及serviceIml实现,在实现类中通过注解@Autowired注入StudentMapper对象作为属性;然后在类中的方法里便可以使用通用mapper的各类方法了。

@Transactional
@Service
public class StudentServiceImpl implements StudentService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentMapper studentMapper;

	@Override
    public StudentVO findInfo(String s_id) throws BusinessException
    {}

2.1 selectByPrimaryKey()主键查询

使用主键查询时,要注意几个地方:

  • 主键只有一个,多个不起作用;
  • 待查询的主键,在***对应实体类属性上必须标有 @Id***,该方法才能识别它为主键且按照主键去查询,否则只会将所有字段作为联合主键来查询!

具体使用:

Student student = studentMapper.selectByPrimaryKey(s_id);
if(student == null) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR, s_id + " 该学生不存在");
        }

2.2 非主键字段使用selectByExample()查询

当我想查询的字段不是主键时,可以调用selectByExample()方法:

Example o = new Example(Student.class);
Example.Criteria criteria = o.createCriteria();
o.setOrderByClause("s_score desc");   // 查询条件按成绩降序排序
criteria.andLike("s_name","%"+s_name+"%");  // 按名字模糊查询
criteria.andNotEqualTo("deleted",1);  // 查询未被删除的学生(deleted 1为已被删除)

List<Student> students= studentMapper.selectByExample(o);  // 普通查询
logger.info("query result students size: " + students.size());

if(students.size() > 0) {
    logger.info(" result s_id: " + students.get(0).getS_id());
        }

对于Example的解释:
Mybatis的逆向工程中会生成实例及实例对应的Example,Example用于添加条件,等同于SQL语句WHERE关键字后的筛选条件部分。


以上仅记录主键查询和普通查询实例,更多详细增删改查等方法使用看
另外一篇文章专门记录通用mapper常用函数以及Example常用筛选条件:

传送门:通用mapper常用函数总结

参考博客:

  1. Mybatis通用Mapper使用详解:https://www.jianshu.com/p/5854bfd0f0e6
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地整合通用Mapper,只需要在pom.xml中添加通用Mapper的依赖,然后在配置文件中配置数据源和通用Mapper的相关属性即可。 具体步骤如下: 1. 在pom.xml中添加通用Mapper的依赖: ```xml <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> ``` 2. 在配置文件中配置数据源和通用Mapper的相关属性: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true mapper: mappers: - tk.mybatis.mapper.common.Mapper not-empty: false identity: MYSQL ``` 其中,mapper.mappers指定了要使用的Mapper接口,这里使用了通用MapperMapper接口;mapper.identity指定了主键生成策略,这里使用了MySQL的自增长主键。 3. 在Mapper接口中继承通用MapperMapper接口即可使用通用Mapper提供的方法: ```java public interface UserMapper extends Mapper<User> { } ``` 这样就可以使用通用Mapper提供的方法来进行数据库操作了,例如: ```java @Autowired private UserMapper userMapper; public void addUser(User user) { userMapper.insert(user); } public void updateUser(User user) { userMapper.updateByPrimaryKeySelective(user); } public void deleteUser(Long id) { userMapper.deleteByPrimaryKey(id); } public User getUser(Long id) { return userMapper.selectByPrimaryKey(id); } public List<User> getUsers() { return userMapper.selectAll(); } ``` 以上就是Spring Boot整合通用Mapper的基本步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值