MybatisPlus基础篇

一.入门案例

1.搭建SpringBoot环境

2.引入相关依赖

  
            com.baomidou
            mybatis-plus-boot-starter
            3.5.3.2
        
        
            com.mysql
            mysql-connector-j
        
        
            com.alibaba
            druid
            1.2.18
        
        
            org.projectlombok
            lombok
        

3.集成MybatisPlus

Mapper层

@Mapper
public interface UserMapper extends BaseMapper {
}

Sercice层

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List selectAll() {
        return  userMapper.selectList(null);
    }
}


//接口
public interface UserService {
    List selectAll();
}

Controller

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/all")
    public  String selectAll(){
        List users = userService.selectAll();
        return users.toString();
    }
}

4.测试

5.其他  表结构   数据       配置文件

DROP TABLE IF EXISTS user;
CREATE TABLE user(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id));


DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),

配置文件yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
  main:
    banner-mode: off



mybatis-plus:
  global-config:
    banner: false
  #设置日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#
    map-underscore-to-camel-case: true

二.基础通用篇

1.通用Mapper接口介绍

我们自己编写的Mapper接口继承自BaseMapper接口,由BaseMapper接口提供了很多单表的增删改查相关的操作方法

@Mapper
public interface UserMapper extends BaseMapper {
}

2.通用service接口介绍

,MybatisPlus还提供了IService接口和对应的实现类ServiceImpl,该实现类已经提供好了一些对应的Service相关的方法,在某些场景下,我们可以直接使用ServiceImpl提供的方法,实现对应的功能。

public interface UserService extends IService {

}
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
   
   
}

测试类

@SpringBootTest
class MybatisPlusApplicationTests {
    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;
    @Test
    public void addService() {
        User user = new User(6L, "chris_jolin", 18, "qq.com@");
        boolean save = userService.save(user);
        System.out.println(save);

    }

    @Test
    public void add() {
        User user = new User(5L, "shane", 18, "qq.com@");
        int insert = userMapper.insert(user);
        System.out.println(insert);

    }

}

3.自定义接口方法使用

Mapper接口中提供抽象方法

@Mapper
public interface MyUserMapper extends BaseMapper {
    User loginByEmailAndPassword(String email);


}

映射文件


    

进阶篇

你可能感兴趣的:(MyBatisPlus,java,开发语言)