MyBatisX代码生成插件在IDEA中的安装配置、连接数据库表生成代码快速开发示例

发布于:2025-09-09 ⋅ 阅读:(23) ⋅ 点赞:(0)

场景

MyBatisX插件介绍

MybatisX是一款基于IDEA的快速开发插件,由MyBatis-Plus团队开发维护,为效率而生。

它的主要功能如下:

支持mapper.xml和Mapper接口之间方法的互相导航跳转;

内置代码生成器,通过使用GUI的形式,能根据数据库来生成Domain、mapper.xml、Mapper、Service

和Service实现类代码;

可以自定义代码生成器模板;

可以通过类似JPA的方式,直接根据方法名称在mapper.xml中生成查询实现,同时支持提示。

注:

博客:
霸道流氓气质-CSDN博客

实现

MyBatisX安装

IDEA-Settings-Plugins-MybatisX

代码生成

在IDEA插件市场搜索"MyBatisX"安装

重启后工具栏出现海豚图标

创建一个测试user表

CREATE TABLE `user` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

IDEA-数据库连接,连接上数据库

可能需要下载数据库驱动。

连接成功之后,找到对应的表,在表上右击选择MybatisX-Generator

配置生成的位置,包路径等。

next

这里继承mybatisplus3,选择对应选项,以及是否需要支持lombok等

点击Finish,生成完毕

编写一个测试接口

import com.badao.demo.domain.User;
import com.badao.demo.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public User test() {
        QueryWrapper<User> userQueryWrapper= new QueryWrapper<>();
        userQueryWrapper.eq("id",1);
        User user = userService.getOne(userQueryWrapper);
        return user;
    }
}

添加数据并测试效果


网站公告

今日签到

点亮在社区的每一天
去签到