場景
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 {@Autowiredprivate UserService userService;@GetMapping("/test")public User test() {QueryWrapper<User> userQueryWrapper= new QueryWrapper<>();userQueryWrapper.eq("id",1);User user = userService.getOne(userQueryWrapper);return user;}
}
添加數據并測試效果