Introduction to MyBatis Plus: 1. Introduction to MyBatis Plus

MyBatis Plus is a MyBatis enhancement tool developed by domestic personnel. On the basis of MyBatis, only enhancements are made and no changes are made to simplify development and improve efficiency.


The core functions of MyBatis Plus are: support common CRUD, code generator and condition builder.


General CRUD: after the Mapper interface is defined, you only need to inherit the BaseMapper interface to obtain the general function of adding, deleting, modifying and querying, without writing any interface methods and configuration files.


Condition Builder: through EntityWrapper (entity wrapper class), it can be used to splice SQL statements and support complex SQL such as sorting and grouping query.

2. Add dependency






3. configuration

    <bean id="sqlSessionFactoryBean"


            <!-- data source -->

            <property name="dataSource" ref="dataSource"/>

            <!-- Alias processing -->

            <property name="typeAliasesPackage" value="com.qf.entity"/>


            <!-- Plug-in registration -->

            <property name="plugins">


                            <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />




4.Dao level

public interface IUserDao extends BaseMapper {


5. entity class



public class User{



    private Integer id;



    private String name;


    private Integer age;


    private String password;



    private Integer xxx;


6. Common notes

@TableField(exist = false): indicates that the property is not a database table field, but must be used.

@TableField(exist = true): indicates that the property is a database table field.

@TableName: database table related

@TableId: table primary key ID

@TableField: table field ID

7. Test method


    public void testMybatisPlus(){


            System.out.println("selectById:"+userDao.selectById(4)); // Query by Id

            System.out.println("selectList:"+userDao.selectList(null)); // Query all


            com.baomidou.mybatisplus.plugins.Page<User> page = new com.baomidou.mybatisplus.plugins.Page<>();

            List<User> list = userDao.selectPage(page, null); // Paging query

            page.setRecords(list); // Encapsulate results into paging objects








            EntityWrapper<User> entityWrapper = new EntityWrapper<>();

            entityWrapper.eq("id", 4);

            entityWrapper.or().like("username", "3");


            List<User> selectList = userDao.selectList(entityWrapper); // Conditional query