• Mybatis-Plus


    几种配置文件

    1.新建springboot项目-pom文件

     引入依赖
    
    • 1
    
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.0.5version>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.application.properties

    1.mysql 8下

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=123456
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.mysql 5下

    #mysql数据库连接
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus
    spring.datasource.username=root
    spring.datasource.password=00000
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.简单测试一下

    3.1 pojo

    package com.atguigu.mpdemo1010.pojo;
    
    import lombok.Data;
    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.2 mapper

    @Repository
    public interface UserMapper extends BaseMapper<User> {
    }
    
    • 1
    • 2
    • 3
    3.3 测试类
    package com.atguigu.mpdemo1010;
    
    import com.atguigu.mpdemo1010.mapper.UserMapper;
    import com.atguigu.mpdemo1010.pojo.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class Mpdemo1010ApplicationTests {
    
    @Autowired
    private UserMapper userMapper;
        @Test
        void contextLoads() {
            List<User> users = userMapper.selectList(null);
            System.out.println(users);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    注意:若是启动报错找不到bean,需要在启动类上添加mapperScan注解,或者mapper接口添加@mapper

    在这里插入图片描述
    在这里插入图片描述

    4.在调用mapper时发现好多方法

    在这里插入图片描述

    这说明基本的单表的增删改查Mybatis-Plus已经帮我们做好了,但是复杂的业务还需要我们手动写xml

    我们还可以配置sql日志

    #mybatis日志
    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    
    • 1
    • 2

    控制台效果

    在这里插入图片描述

  • 相关阅读:
    Android Studio入门——页面跳转
    springmvc@RequestBody原理及扩展
    c# 中的类
    vue开启splitChunks分包处理
    Mac系统下Gauge初体验
    开关电源测试解决方案之浪涌电流测试 -纳米软件
    Hadoop HBase Hive day4.md
    Windows版 PostgreSQL 利用 pg_upgrade 进行大版升级操作
    『干货』WebStorm代码模板配置大全
    用python混合检索 + 重排序改善
  • 原文地址:https://blog.csdn.net/weixin_45156514/article/details/126090358