MyBatisPlus入门

一,创建SpringBoot项目

二,引入依赖

<dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.5</version>
 </dependency>

三,application.yml配置

复制代码
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

#控制台日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码

四,在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@MapperScan("com.zl.mapper")

 

五,编码

1,编写实体类TUser

复制代码
package com.zl.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TUser {
    private Long id;
    private String username;
    private Integer age;
    private String password;
}
复制代码

2,编写Mapper类 TUserMapper.java

复制代码
package com.zl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zl.pojo.TUser;

import java.util.List;

public interface TUserMapper extends BaseMapper<TUser> {

    
}
复制代码

3,开始使用

复制代码
package com.zl;

import com.zl.mapper.TUserMapper;
import com.zl.pojo.TUser;
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 MybatisPlusApplicationTests {

    @Autowired
    private TUserMapper tUserMapper;

    @Test
    void contextLoads() {
        List<TUser> userList = tUserMapper.selectList(null);

        userList.forEach(System.out::println);

    }

}
复制代码

 

posted @   风子磊  阅读(151)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示