SpringBoot series of tutorials MybatisPlus integration articles

191231-SpringBoot tutorial series MybatisPlus integration articles

SpringBoot the previously described deletions to achieve integration of db Mybatis change search operation, and are given the annotation implemented in two ways xml mapper interface; although annotation embodiment kill xml file, but does not use elegant article describes mybats-plus use case, a simplified conventional CRUD operations

I. Environment

As used herein, SpringBoot version 2.2.1.RELEASE, mybatis-plus version 3.2.0, the database is mysql 5+

1. Project to build

The official tutorial is recommended to create a SpringBoot project; if directly create a maven project, it will configure the following content, your copy pom.xmlof

  • The main introduction is mybatis-spring-boot-starterpossible to reduce the suffocating configuration
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

2. Configure the information

In application.ymlthe configuration file, add a bit of configuration db

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password:

Next, prepare a test table (table structure before borrowing db still operating in the blog series) for subsequent CURD; result information table below

DROP TABLE IF EXISTS `money`;

CREATE TABLE `money` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `money` int(26) NOT NULL DEFAULT '0' COMMENT '有多少钱',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

II. Examples of integration

mybatis-plus and posture of using mybatis there are some differences, the following means is not generatordirectly hand the code line and case

1. PO

Create a table corresponding PO objects: MoneyPo

@Data
public class MoneyPo {
    private Integer id;

    private String name;

    private Long money;

    private Integer isDeleted;

    private Timestamp createAt;

    private Timestamp updateAt;
}

2. DAO Interface

Table operator interface, and this interface is different mybatis succession BaseMapperafter CURD comes with a single operation of the interface of the table, is basically no need to define additional interface, the interaction can be achieved db

public interface MoneyMapper extends BaseMapper<MoneyPo> {
}
  • Note that BaseMapperthe parameter for the object corresponding to the table PO

3. Test

After completion of the above, the integration process would be finished basically, yes, it's that simple, then we enter the testing session

The first is the startup class, we add a @MapperScancomment, so there is no need to add the DAO interface @Mapperannotated

@SpringBootApplication
@MapperScan("com.git.hui.boot.mybatisplus.mapper")
public class Application {

    public Application(MoneyRepository repository) {
        repository.testMapper();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

About the test case, we will demonstrate the following four basic CRUD operation case, because the focus is not on paper describes the use of mybatis-plus, for the following codes in question can see the official document: https://mp.baomidou.com/guide/

@Component
public class MoneyRepository {
    @Autowired
    private MoneyMapper moneyMapper;

    private Random random = new Random();

    public void testDemo() {
        MoneyPo po = new MoneyPo();
        po.setName("mybatis plus user");
        po.setMoney((long) random.nextInt(12343));
        po.setIsDeleted(0);

        // 添加一条数据
        moneyMapper.insert(po);

        // 查询
        List<MoneyPo> list =
                moneyMapper.selectList(new QueryWrapper<MoneyPo>().lambda().eq(MoneyPo::getName, po.getName()));
        System.out.println("after insert: " + list);

        // 修改
        po.setMoney(po.getMoney() + 300);
        moneyMapper.updateById(po);
        System.out.println("after update: " + moneyMapper.selectById(po.getId()));

        // 删除
        moneyMapper.deleteById(po.getId());

        // 查询
        Map<String, Object> queryMap = new HashMap<>(2);
        queryMap.put("name", po.getName());
        System.out.println("after delete: " + moneyMapper.selectByMap(queryMap));
    }
}

Output

II. Other

Project Scope

1. A gray Blog

Believe everything the book is not as good, above, is purely one of the words, due to limited personal capacity, it is inevitable omissions and mistakes, such as find a bug or have better suggestions are welcome criticism and generous gratitude

Here a gray personal blog, recording all study and work in the blog, welcome to go around

A gray blog

Guess you like

Origin www.cnblogs.com/yihuihui/p/12159619.html