idea怎么使用Mybatis逆向工程插件
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,idea怎么使用Mybatis逆向工程插件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、使用mybatis连接数据库
千家信息网最后更新 2025年11月15日idea怎么使用Mybatis逆向工程插件
idea怎么使用Mybatis逆向工程插件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
一、使用mybatis连接数据库

添加连接的mysql的信息,测试链接成功即可。

二、安装Better-Mybatis-Generator插件
安装成功后,在需要生成的表上右键选择mybatis-generator。
添加要生成的一些配置。
点击OK,第一次生成会弹出窗口,需要输入数据库的帐号密码。可以看到生成该表对应的mapper接口、实体类和sql。
三、关于example类详解
1、example成员变量
mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。
//作用:升序还是降序 //参数格式:字段+空格+asc(desc) protected String orderByClause; //作用:去除重复 //true是选择不重复记录,false,反之 protected boolean distinct; //自定义查询条件 //Criteria的集合,集合中对象是由or连接 protected ListoredCriteria; // 分页的显示条数 private Integer limit; // 分页的起始下标 private Long offset; //内部类Criteria包含一个Cretiron的集合, //每一个Criteria对象内包含的Cretiron之间是由 AND连接的 public static class Criteria extends GeneratedCriteria { protected Criteria() {super();} } //是mybatis中逆向工程中的代码模型 protected abstract static class GeneratedCriteria {......} //是最基本,最底层的Where条件,用于字段级的筛选 public static class Criterion {......}
2、example使用
在MybatisDemoApplicationTests类中进行测试:
package org.ywz.test; import org.junit.jupiter.api.Test;import org.junit.platform.commons.util.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.ywz.dao.StudentDao;import org.ywz.pojo.Student;import org.ywz.pojo.StudentExample; import java.util.List; /** * Example类使用说明 */@SpringBootTestclass MybatisDemoApplicationTests { @Autowired private StudentDao studentDao; @Test void contextLoads() { StudentExample studentExample = new StudentExample(); // 查询数据的总条数 类似于:select count(*) from student long l = studentDao.countByExample(studentExample); System.out.println("---------------总条数----------------"); System.out.println("数据库的总条数:" + l); System.out.println("----------------and条件---------------"); // where条件查询或多条件查询 Student student = new Student(); student.setName("王五"); student.setSex("男"); selectAndCondition(student); System.out.println("---------------or条件----------------"); selectOrCondition(student); System.out.println("-----------------模糊查询--------------"); student.setName("王"); selectLikeCondition(student); System.out.println("-----------------分页查询--------------"); selectLimit(); } /** * where条件查询或多条件查询 * 类似于:select * from student where name={#student.name} and sex={#student.sex} order by score asc; * * @param student */ private void selectAndCondition(Student student) { StudentExample studentExample = new StudentExample(); StudentExample.Criteria criteria = studentExample.createCriteria(); studentExample.setOrderByClause("score asc"); //升序 studentExample.setDistinct(false); //不去重 if (StringUtils.isNotBlank(student.getName())) { criteria.andNameEqualTo(student.getName()); } if (StringUtils.isNotBlank(student.getSex())) { criteria.andSexEqualTo(student.getSex()); } List students = studentDao.selectByExample(studentExample); students.forEach(System.out::println); } /** * 类似于:select * from student where name={#student.name} or sex={#student.sex} ; * * @param student */ private void selectOrCondition(Student student) { StudentExample studentExample = new StudentExample(); StudentExample.Criteria criteria1 = studentExample.createCriteria(); StudentExample.Criteria criteria2 = studentExample.createCriteria(); if (StringUtils.isNotBlank(student.getName())) { criteria1.andNameEqualTo(student.getName()); } if (StringUtils.isNotBlank(student.getSex())) { criteria2.andSexEqualTo(student.getSex()); } studentExample.or(criteria2); List students = studentDao.selectByExample(studentExample); students.forEach(System.out::println); } /** * 类似于:select * from student where name like %{#student.name}% * * @param student */ private void selectLikeCondition(Student student) { StudentExample studentExample = new StudentExample(); StudentExample.Criteria criteria = studentExample.createCriteria(); if (StringUtils.isNotBlank(student.getName())) { criteria.andNameLike("%" + student.getName() + "%"); } List students = studentDao.selectByExample(studentExample); students.forEach(System.out::println); } /** * 类似于:select * from student limit offset,limit */ public void selectLimit() { StudentExample studentExample = new StudentExample(); studentExample.setOffset(2l); studentExample.setLimit(5); List students = studentDao.selectByExample(studentExample); students.forEach(System.out::println); }} 运行结果:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
条件
查询
字段
数据
生成
数据库
工程
插件
逆向工程
成功
作用
升序
对象
是由
帮助
测试
选择
清楚
下标
之间
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
南昌中睿网络技术
软件开发交流群
禅道 数据库怎么进
奉贤区服务器回收哪家便宜
莱芜职业学院软件开发好吗
江苏数据库空投箱销售厂
新中大财务软件修复数据库
服务器维护安全培训
杭州南曙网络技术有限公司
江西网络软件开发供应
idc网络安全法行政处罚
荆州订制软件开发中心
工业园区正规网络技术服务电话
网络安全 网络隔离
国产软件开发工具哪家好
服务器生存模式没有命令
软件开发新增需求协议
网络安全等级g
绿色征途服务器列表载显示不了
mysql 数据库队列
服务器能安装的360
网络安全队会手抄小报
局域网的网络安全吗
软件开发有哪些应用软件
天创息壤互助软件开发
网络安全培训ppt开场白
dnf别的服务器可以邮寄吗
什么是服务器的操作系统
外国网络技术
广西广电网络技术岗位