MybatisPlus基础篇

一.入门案例

1.搭建SpringBoot环境

2.引入相关依赖

  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

3.集成MybatisPlus

Mapper层

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

Sercice层

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


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

Controller

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/all")
    public  String selectAll(){
        List<User> 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, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),

配置文件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<User> {
}

2.通用service接口介绍

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

public interface UserService extends IService<User> {

}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> 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> {
    User loginByEmailAndPassword(String email);


}

映射文件

<mapper namespace="com.hu.mybatispluse.mapper.MyUserMapper" >
    <select id="loginByEmailAndPassword" resultType="com.hu.mybatispluse.pojo.User">
        select * from user where email = #{haha}
    </select>

进阶篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值