MyBatisPlus

【狂神说Java】MyBatisPlus最新完整教程通俗易懂:https://www.bilibili.com/video/BV17E411N7KN/

快速入门

使用第三方组件:
1、导入对应的依赖
2、研究依赖如何配置
3、代码如何编写
4、提高扩展技术能力

【代码演示】
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.liweixiao</groupId>
    <artifactId>TestMyBatisPlus</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.7.6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.6</version>
        </dependency>
    </dependencies>

</project>

application.properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/book?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

t_book.java

package com.liweixiao.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author:LiWeixiao
 * @date:2023/7/24
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class t_book {
    private int id;
    private String name;
    private String author;
    private double price;
}

BookMapper.java

package com.liweixiao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liweixiao.pojo.t_book;
import org.springframework.stereotype.Repository;

/**
 * @author:LiWeixiao
 * @date:2023/7/24
 * @description:
 */
@Repository
public interface BookMapper extends BaseMapper<t_book> {
}

MybatisPlusApplication.java

package com.liweixiao;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author:LiWeixiao
 * @date:2023/7/24
 * @description:
 */
@MapperScan("com.liweixiao.mapper")
@SpringBootApplication
public class MybatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class,args);
    }
}

MybatisPlusApplicationTest.java

package com.liweixiao;

import com.liweixiao.mapper.BookMapper;
import com.liweixiao.pojo.t_book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @author:LiWeixiao
 * @date:2023/7/24
 * @description:
 */
@SpringBootTest
public class MybatisPlusApplicationTest {
    @Autowired
    private BookMapper bookMapper;

    @Test
    void contextloads(){
        List<t_book> books =bookMapper.selectList(null);
        books.forEach(System.out::println);
        /*for (int i = 0; i < books.size(); i++) {
            System.out.println(books.get(i));
        }*/
    }
}
posted @ 2023-07-26 08:47  LiWeixiao  阅读(10)  评论(0编辑  收藏  举报