其他分享
首页 > 其他分享> > mybatis-plus快速使用

mybatis-plus快速使用

作者:互联网

Mybatis-plus整合到springboot

1.1 引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>

1.2 编写实体类和mapper接口并且接口继承BaseMapper<实体类>

package com.zhao.mybatisplus.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class user {
    private int id;
    private String name;
    private String email;
}

package com.zhao.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhao.mybatisplus.pojo.user;
import org.springframework.stereotype.Component;

@Component
public interface userMapper extends BaseMapper<user> {

}

1.3 扫描mapper路径

@SpringBootApplication
@MapperScan("com/zhao/mybatisplus/mapper")
public class MybatisplusApplication {

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

1.4 注入测试

package com.zhao.mybatisplus;

import com.zhao.mybatisplus.mapper.userMapper;
import com.zhao.mybatisplus.pojo.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MybatisplusApplicationTests {

    @Autowired
    com.zhao.mybatisplus.mapper.userMapper usermapper;
    @Test
    void contextLoads() {
        user user = usermapper.selectById(1);
        System.out.println(user);
    }

}

标签:mapper,mybatisplus,com,zhao,plus,user,mybatis,import,快速
来源: https://blog.csdn.net/tianqi11/article/details/116308111