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

  

                    <groupId>com.baomidou</groupId>

                    <artifactId>mybatis-plus</artifactId>

                    <version>2.3</version>

            </dependency>

3. configuration

    <bean id="sqlSessionFactoryBean"

            class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">

            <!-- data source -->

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

            <!-- Alias processing -->

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

           

            <!-- Plug-in registration -->

            <property name="plugins">

                    <list>

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

                    </list>

            </property>

    </bean>

4.Dao level

public interface IUserDao extends BaseMapper {

}

5. entity class

@Data

@TableName(value="t_user")

public class User{

   

    @TableId(value="id",type=IdType.AUTO)

    private Integer id;

   

    @TableField(value="username")

    private String name;

   

    private Integer age;

   

    private String password;

   

    @TableField(exist=false)

    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

  @Test

    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

            System.out.println(page.getCurrent());

            System.out.println(page.getPages());

            System.out.println(page.getSize());

            System.out.println(page.getTotal());

            System.out.println(page.getRecords());

           

           

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

            entityWrapper.eq("id", 4);

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

           

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

            System.out.println("wrapper:"+selectList);

           

    }