Mybatis- Plus quick build

Create a sample spring-boot-Web project

Introducing Spring Boot Starter parent project:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4. RELEASE</version>
    <relativePath/>
</parent>
Copy the code

Introducing dependent dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

Create a database table

create table classes
(
    id   int auto_increment comment 'class ID'
        primary key,
    name varchar(64) not null comment 'Class Name',
    constraint class_name_uindex
        unique (name)
);
Copy the code

The entity class corresponding to the table

@data @tablename ("classes") public class ClassEntity {/* class ID */ @tableId (type = idType.auto) private Integer ID; /* class name */ private String name; }Copy the code

The Mapper class

@Mapper
public interface IClassMapper extends BaseMapper<ClassEntity> {
}
Copy the code

CRUD usage examples

Create a Test class

@SpringBootTest
@RunWith(SpringRunner.class)
public class ClassTest {

    @Autowired
    private IClassMapper classMapper;
}
Copy the code

Insert data

@Test
public void testInsert() {
    ClassEntity classEntity = new ClassEntity();
    classEntity.setName("className");
    classMapper.insert(classEntity);
    System.out.println(classMapper.selectById(classEntity.getId()));
}
Copy the code

Delete the data

@Test
public void testDelete() {
    ClassEntity classEntity = new ClassEntity();
    classEntity.setName("testDelete");
    classMapper.insert(classEntity);
    classMapper.deleteById(classEntity.getId());
}
Copy the code

Modify the data

@Test
public void testUpdate() {
    ClassEntity classEntity = classMapper.selectById(2);
    classEntity.setName("testUpdate");
    classMapper.updateById(classEntity);
}
Copy the code

Conditions of the query

@Test
public void testSelect() {
    String name = "class";
    QueryWrapper<ClassEntity> wrapper = Wrappers.query(new ClassEntity())
            .eq("id", 1)
            .or()
            .like(Strings.isNotEmpty(name),"name", name);

    List<ClassEntity> classEntities = classMapper.selectList(wrapper);
    classEntities.forEach(System.out::println);
}
Copy the code

The official documentation

mp.baomidou.com/guide