使用baseMapper简化常规的数据操作

使用mybatis里面的BaseMapper简化常规的数据操作

引入:

Gradle:

implementation 'tk.mybatis:mapper-spring-boot-starter:2.1.5'

包里面有一个接口类

BaseMapper

@tk.mybatis.mapper.annotation.RegisterMapper
public interface BaseMapper<T> extends
        BaseSelectMapper<T>,
        BaseInsertMapper<T>,
        BaseUpdateMapper<T>,
        BaseDeleteMapper<T> {

}

这里面继续了常见的增删改查的操作,实现它,就省去了定义常规的操作,对应resourcexiad 的xml文件也省了(如果有其它的操作,再加上去)

数据库信息:

CREATE TABLE `tache_info` (
	`tch_id` BIGINT (20),
	`tch_name` VARCHAR (1500),
	`tch_type` VARCHAR (150)
)ENGINE=INNODB DEFAULT CHARSET=utf8; 
INSERT INTO `tache_info` (`tch_id`, `tch_name`, `tch_type`) VALUES('1','开始','es');
INSERT INTO `tache_info` (`tch_id`, `tch_name`, `tch_type`) VALUES('2','审核','json');
INSERT INTO `tache_info` (`tch_id`, `tch_name`, `tch_type`) VALUES('3','评估','mysql');

插入一些基本的数据

代码:

基础类:TacheInfo

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "tache_info")
public class TacheInfo {

    /**
     *  @Id 设置主键,调用方法中ByPrimaryKey接口的时候用
     */
    @Id
    private Long tchId;

    private String tchName;

    private String tchType;
}

自定义注解:MyBatisRepository

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Component
public @interface MyBatisRepository {
    String value() default "";
}

Mapper类继承:BaseMapper

@MyBatisRepository
public interface TacheInfoMapper extends BaseMapper<TacheInfo> {

}

服务类:TacheService

@Service
public class TacheService {

    @Resource
    private TacheInfoMapper tacheInfoMapper;

    public  List<TacheInfo> getTacheInfo(){
        List<TacheInfo> tacheInfos = tacheInfoMapper.selectAll();
        System.out.println(tacheInfos);
        return tacheInfos;
    }

    public  void addTacheInfo(TacheInfo tache){
        tacheInfoMapper.insert(tache);
    }

    public void updateTacheInfo(TacheInfo tache){
        tacheInfoMapper.updateByPrimaryKey(tache);
    }

    public  TacheInfo getTacheInfoById(Long tchId) {
        return tacheInfoMapper.selectByPrimaryKey(tchId);
    }


    public  void delTacheInfo(Long tchId){
        tacheInfoMapper.deleteByPrimaryKey(tchId);
    }

}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestMyBatis {

    @Resource
    private TacheService tacheService;

    @Test
    public void getPath() {
        String path = TestMyBatis.class.getResource("/").getPath();
        System.out.println(path);
    }

    @Test
    public void testGetAll() {
        tacheService.getTacheInfo();
    }

    @Test
    public void testAddTache() {
        TacheInfo add = new TacheInfo(4L,"处理","add");
        tacheService.addTacheInfo(add);
        // 查看变化后的数据
        tacheService.getTacheInfo();
    }

    @Test
    public void testUpdateTache() {
        Long tchId = 5L;
        TacheInfo update = tacheService.getTacheInfoById(tchId);
        System.out.println(update);
        update.setTchName("更新");
        update.setTchType("update");
        tacheService.updateTacheInfo(update);
        // 查看变化后的数据
        tacheService.getTacheInfo();
    }

    @Test
    public void testDelTache() {
        Long tchId = 1L;
        tacheService.delTacheInfo(tchId);
        // 查看变化后的数据
        tacheService.getTacheInfo();
    }
}

注意:

 如果是新建的项目,记得加上扫描的内容

@ComponentScan(value = "com.study")
@MapperScan(annotationClass = MyBatisRepository.class, basePackages = "com.study")

总结:

  常见的数据操作,实现BaseMapper这个借口,很方便,也省去了些对应xml的工作。如果需要添加的时候,再进行添加就好。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 使用 MyBatisBaseMapper 添加分页功能很简单,您只需要在 Mapper 接口中添加如下方法即可: ``` List<T> selectByRowBounds(T record, RowBounds rowBounds); ``` 然后在你的 Mapper.xml 文件中编写对应的 SQL 语句即可。 您还可以使用 MyBatis-Plus 提供的分页插件,它能让分页更加方便和简单。 使用方法如下: 1. 在 pom.xml 文件中添加依赖 ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> ``` 2. 在你的配置文件中开启分页插件 ``` mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:/mapper/*Mapper.xml type-aliases-package: com.example.demo.entity global-config: db-config: id-type: auto plugins: - paging ``` 3. 在你的 Service 层调用分页方法 ``` Page<User> page = new Page<>(1, 10); List<User> users = userMapper.selectPage(page, null).getRecords(); ``` 这样就可以在 Mapper 接口中使用分页功能了。 ### 回答2: 在使用BaseMapper添加分页功能前,首先需要了解BaseMapper的作用和使用方法。BaseMapperMyBatis-Plus提供的一个通用Mapper接口,它提供了一些常用的CRUD方法,用于执行数据库的增删改查操作。 要使用BaseMapper添加分页功能,可以通过在Mapper接口中定义方法,并使用注解@Select来实现。在方法中,可以调用BaseMapper的方法来实现分页查询,并返回结果。 下面是一个使用BaseMapper添加分页功能的示例代码: ``` // 在Mapper接口中添加方法 @Select("SELECT * FROM table_name") List<YourEntity> selectByPage(Page<YourEntity> page); // 在Service中调用Mapper接口方法 public IPage<YourEntity> queryByPage(Page<YourEntity> page) { // 设置分页参数 page.setSize(10); // 设置每页记录数 page.setCurrent(1); // 设置当前页码 // 调用Mapper接口方法进行查询 List<YourEntity> list = yourMapper.selectByPage(page); // 将结果封装到分页对象中返回 IPage<YourEntity> result = new Page<>(); result.setRecords(list); // 设置查询结果列表 result.setCurrent(page.getCurrent()); // 设置当前页码 result.setSize(page.getSize()); // 设置每页记录数 result.setTotal(yourMapper.selectCount(null)); // 设置总记录数 return result; } ``` 在上述示例中,首先在Mapper接口中使用@Select注解定义了一个查询方法selectByPage,并使用BaseMapper的方法来执行分页查询。在Service中调用该方法,并设置分页参数,然后将查询结果封装到一个分页对象中返回。 需要注意的是,分页功能还需要配合分页插件来实现。MyBatis-Plus提供了一些常用的分页插件,如PageHelper和PaginationInterceptor等,可以根据项目的实际情况选择合适的插件来使用使用BaseMapper添加分页功能能够很方便地实现分页查询操作,减少了编写重复代码的工作量,提高了开发效率。同时,配合分页插件的使用,还能够优化查询性能,提升系统的响应速度。 ### 回答3: BaseMapper是一种用于数据库操作的通用Mapper,它提供了一些常用的数据库操作方法。要实现分页功能,可以通过以下步骤进行操作: 1. 首先,在定义BaseMapper接口时,添加一个方法,用于查询指定页数的数据。方法的参数可以包括页码和每页的数据数量。 2. 在BaseMapper接口的实现类中,实现这个分页查询的方法。可以使用数据库的分页查询语句,比如MySQL中的LIMIT语句,来实现分页功能。根据输入的页码和每页的数量,计算需要查询的数据的起始位置,再使用LIMIT语句查询指定范围的数据。 3. 在具体的Mapper接口中,继承BaseMapper接口,并调用BaseMapper中定义的分页查询方法,来实现具体的分页查询功能。 4. 在调用这个Mapper接口的地方,传入正确的页码和每页数量的参数,即可获取到指定页码的数据使用BaseMapper添加分页功能的好处是可以避免重复编写相同的分页查询代码。因为分页查询在实际开发中非常常见,将其抽象为通用的BaseMapper可以减少代码的重复性,提高开发效率。此外,使用BaseMapper还可以让数据库操作更加规范化和统一,方便维护和管理。 总之,使用BaseMapper添加分页功能需要在BaseMapper接口和实现类中定义和实现一个分页查询的方法,在具体的Mapper接口中继承BaseMapper,并调用这个方法来实现分页查询。这样可以方便地实现分页功能,并提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值