关于MyBatisPlus的一点总结

  这几天在做小项目的时候,发现国内第三方将MyBatis进一步封装,提供了MyBatisPlus工具,进一步方便了开发人员的使用。于是乎尝试把项目中的MyBatis更换为MyBatisPlus,过程中也是出现了重重错误。下面简单记录一下这些错误,防止以后再次出现,也为其他同学出现相同错误提供一些解决思路。

1 更改SQLSessionFactoryBean的配置

  在使用MyBatis时,我们配置的为SqlSessionFactoryBean,配置信息如下

<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybaits-config.xml"></property>
</bean>

  在更改使用MyBatisPlus后,需要配置MybatisSqlSessionFactoryBean,配置信息具体如下

<bean class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean" id="sessionFactory">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybaits-config.xml"></property>
    <!--配置分页查插件-->
    <property name="plugins">
        <array>
            <bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
                <property name="dialectType" value="mysql"></property>
            </bean>
        </array>
    </property>
</bean>

2 实体类与表对应

  MyBatisPlus提供了实体类与表对应的机制,使用@TableName可以将此实体类与数据库中表进行对应。使用@TableId使的某一个属性与表中某一字段对应。使用@TableField可以用来说明这个属性是否在表中存在对应的字段,即@TableField(exist=false)表示对应属性在表中不存在。此外还提供了@TableName和@TableLogic注解,需要的同学们可以在有时间的时候了解一下。

3 BaseMapper

  MyBatisPlus提供了BaseMapper,在BaseMapper中提供了一系列基础的增、删、改、查功能,在实现具体的Mapper时可以在此基础上进行扩展。BaseMapper具体内容如下所示。

public interface BaseMapper<T> {
    Integer insert(T var1);

    Integer insertAllColumn(T var1);

    Integer deleteById(Serializable var1);

    Integer deleteByMap(@Param("cm") Map<String, Object> var1);

    Integer delete(@Param("ew") Wrapper<T> var1);

    Integer deleteBatchIds(List<? extends Serializable> var1);

    Integer updateById(T var1);

    Integer updateAllColumnById(T var1);

    Integer update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);

    T selectById(Serializable var1);

    List<T> selectBatchIds(List<? extends Serializable> var1);

    List<T> selectByMap(@Param("cm") Map<String, Object> var1);

    T selectOne(@Param("ew") T var1);

    Integer selectCount(@Param("ew") Wrapper<T> var1);

    List<T> selectList(@Param("ew") Wrapper<T> var1);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);

    List<Object> selectObjs(@Param("ew") Wrapper<T> var1);

    List<T> selectPage(RowBounds var1, @Param("ew") Wrapper<T> var2);

    List<Map<String, Object>> selectMapsPage(RowBounds var1, @Param("ew") Wrapper<T> var2);
}

4 Page

  在MyBatisPlus中,提供了分页查询的方法,即查询得到Page<T>类型,极大的简化了分页查询的操作。

public Page<Employee> selectListByPage(int pageNo) {
    // 当前页数,表示当前页数中的记录大小,根据哪一个字段进行排序
    Page<Employee> employeePage = new Page<>(pageNo, 10, "id");
    // 是否为升序排列(默认为true)
    employeePage.setAsc(false);
    List<Employee> employeeList = baseMapper.selectPage(employeePage, null);
    for(Employee e : employeeList) {
        setEmployeeDepAndPos(e);
    }
    employeePage.setRecords(employeeList);

    return employeePage;
}

总结:

  目前小编暂时总结了以上三点,在后面继续学习过程中,会逐渐完善该内容。加油喽。

猜你喜欢

转载自blog.csdn.net/m0_37135421/article/details/80650743