springboot集成mybatis-plus

目录结构:

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
    </dependencies>

application.yml

spring:
  datasource:
    url: jdbc:mysql://IP:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver

UserInfo.java 【使用lombok插件】

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class UserInfo {
    @TableId
    private int id;
    private String name;
    private Integer age;
    private String email;

    public UserInfo(int id, String name, Integer age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }

}
UserInfoMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsw.mybatisplus.model.UserInfo;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
}
TestController.java
/**
 * 增删改查,代码生成器
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsw.mybatisplus.mapper.UserInfoMapper;
import com.zsw.mybatisplus.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {
    @Autowired
    private UserInfoMapper userMapper;

    @RequestMapping("/selectAll")
    public List<UserInfo> selectAll() {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.ge("age", 18);
        List<UserInfo> list = userMapper.selectList(wrapper);
        return list;
    }

    @RequestMapping("/selectOne")
    public UserInfo selectOne(@RequestParam int id) {
        return userMapper.selectById(id);

    }

    @RequestMapping("/insert")
    public int insert() {
        UserInfo userInfo = new UserInfo(12, "Rorschach", 24, "772@qq.com");
        return userMapper.insert(userInfo);
    }

    @RequestMapping("/update")
    public int update(@RequestParam int id) {
        UserInfo userInfo = userMapper.selectById(id);
        userInfo.setAge(78);
        return userMapper.updateById(userInfo);
    }

    @RequestMapping("/delete")
    public int dalete(@RequestParam int id) {
        return userMapper.deleteById(id);
    }

    @RequestMapping("/page")
    public IPage<UserInfo> selectUserPage() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<UserInfo> page = new Page<>(2, 3);  // 查询第1页,每页返回5条
        IPage<UserInfo> iPage = userMapper.selectPage(page, queryWrapper);
        return iPage;
    }
}
MybatisPlusConfig.java 分页的配置文件

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.zsw.mybatisplus.mapper")
public class MybatisPlusConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rorschach01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值