This section mainly introduces how to integrate Mybatis- Plus in new projects. Thus complete the operation of adding, deleting and checking a database table.

Create a Springboot project

— Skipped here

Import corresponding dependency packages

The complete POM.xml file is shown below


      
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.hank</groupId>
    <artifactId>mybatisdemo</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>mybatisdemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <! -- mybatis -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
<! -- <dependency>-->
<! -- <groupId>com.alibaba</groupId>-->
<! -- <artifactId>druid</artifactId>-->
<! - < version > 1.1.20 < / version > -- >
<! -- </dependency>-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code

The above contents are mainly related to Mybatis- Plus as follows

 <! -- mybatis -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1</version>
  </dependency>
  <! -- <dependency>-->
  <! -- <groupId>com.alibaba</groupId>-->
  <! -- <artifactId>druid</artifactId>-->
  <! - < version > 1.1.20 < / version > -- >
  <! -- </dependency>-->
Copy the code

Mybatis-plus is an enhanced plug-in based on Mybatis, with all the features of Mybatis plus pagination function by default;

Finally, we can map the Jar package resources in the project structure, among which the resources related to Mybatis- Plus are as follows

In the IDEA tool, File–>Project Structure… –>Modules–> Select your project –>Dependencies)

From the above we can see that although we only introduced Mybatis -plus-boot-starter in pm.xml at version 3.3.1, other related resource bundles were actually introduced in the end.

\

The coding part

Add start scan annotations

Add a startup annotation scan to the project startup class If you do not start the class configuration, you can also annotate each mapper with @mapper annotations

@SpringBootApplication
@MapperScan("com.hank.mybatisdemo.dao")
public class MybatisdemoApplication {
    public static void main(String[] args) { SpringApplication.run(MybatisdemoApplication.class, args); }}Copy the code

Write entity classes and Mapper classes

T_Temp is used as an example. Note that the interface class needs to inherit from the BaseMapper class, with the generic User entity object

package com.hank.mybatisdemo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hank.mybatisdemo.entity.User;

public interface UserMapper extends BaseMapper<User> {}Copy the code

The User entity object is as follows

package com.hank.mybatisdemo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName(value = "T_Temp")
public class User {

    @TableId(value = "id",type= IdType.AUTO)
    Integer id;
    @TableField(value = "user_name")
    String userName;
    Integer age;
    String sex;
    String remark;
}
Copy the code

The structure of the corresponding table is as follows

/*Table structure for table `t_temp` */

DROP TABLE IF EXISTS `t_temp`;

CREATE TABLE `t_temp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(30) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` varchar(2) DEFAULT NULL,
  `remark` varchar(225) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4;
Copy the code

The configuration file

spring:
  datasource:
# type: com.alibaba.druid.pool.DruidDataSource
    url: JDBC: mysql: / / 127.0.0.1:3306 / btest? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.mysql.cj.jdbc.MysqlDataSource
Copy the code

\

Write test case validation

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserMapper userMapper;

    @Test
    public void testUser(a){
        List<User> userList=userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

    @Test
    public void testUser2(a){
        for (int i = 0; i <10 ; i++) {
            User u=new User();
            u.setUserName("Zhang"+i);
            Random random = new Random();
            u.setAge(random.nextInt());
            u.setSex(i%2>1?"Male":"Female");
            int row=userMapper.insert(u);
            Assert.assertEquals(1, row); }}}Copy the code