经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
idea使用Mybatis逆向工程插件详情
来源:jb51  时间:2022/1/3 16:47:35  对本文有异议

一、使用mybatis连接数据库

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

二、安装Better-Mybatis-Generator插件

安装成功后,在需要生成的表上右键选择mybatis-generator。

添加要生成的一些配置。

点击OK,第一次生成会弹出窗口,需要输入数据库的帐号密码。可以看到生成该表对应的mapper接口、实体类和sql

三、关于example类详解

1、example成员变量

mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。

  1. ?//作用:升序还是降序
  2. ?//参数格式:字段+空格+asc(desc)
  3. ?protected String orderByClause; ?
  4. ?//作用:去除重复
  5. ?//true是选择不重复记录,false,反之
  6. ?protected boolean distinct;
  7. ?//自定义查询条件
  8. ?//Criteria的集合,集合中对象是由or连接
  9. ?protected List<Criteria> oredCriteria;
  10. ?// 分页的显示条数
  11. ?private Integer limit;
  12. ?// 分页的起始下标 ??
  13. ?private Long offset;
  14. ?//内部类Criteria包含一个Cretiron的集合,
  15. ?//每一个Criteria对象内包含的Cretiron之间是由 ?AND连接的
  16. ?public static class Criteria extends GeneratedCriteria {
  17. ? protected Criteria() {super();}
  18. ?}
  19. ?//是mybatis中逆向工程中的代码模型
  20. ?protected abstract static class GeneratedCriteria {......}
  21. ?//是最基本,最底层的Where条件,用于字段级的筛选
  22. ?public static class Criterion {......}

2、example使用

在MybatisDemoApplicationTests类中进行测试:

  1. package org.ywz.test;
  2. ?
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.platform.commons.util.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.ywz.dao.StudentDao;
  8. import org.ywz.pojo.Student;
  9. import org.ywz.pojo.StudentExample;
  10. ?
  11. import java.util.List;
  12. ?
  13. /**
  14. ?* Example类使用说明
  15. ?*/
  16. @SpringBootTest
  17. class MybatisDemoApplicationTests {
  18. ? ? @Autowired
  19. ? ? private StudentDao studentDao;
  20. ?
  21. ? ? @Test
  22. ? ? void contextLoads() {
  23. ? ? ? ? StudentExample studentExample = new StudentExample();
  24. ?
  25. ? ? ? ? // 查询数据的总条数 类似于:select count(*) from student
  26. ? ? ? ? long l = studentDao.countByExample(studentExample);
  27. ? ? ? ? System.out.println("---------------总条数----------------");
  28. ? ? ? ? System.out.println("数据库的总条数:" + l);
  29. ? ? ? ? System.out.println("----------------and条件---------------");
  30. ? ? ? ? // where条件查询或多条件查询
  31. ? ? ? ? Student student = new Student();
  32. ? ? ? ? student.setName("王五");
  33. ? ? ? ? student.setSex("男");
  34. ? ? ? ? selectAndCondition(student);
  35. ? ? ? ? System.out.println("---------------or条件----------------");
  36. ? ? ? ? selectOrCondition(student);
  37. ? ? ? ? System.out.println("-----------------模糊查询--------------");
  38. ? ? ? ? student.setName("王");
  39. ? ? ? ? selectLikeCondition(student);
  40. ? ? ? ? System.out.println("-----------------分页查询--------------");
  41. ? ? ? ? selectLimit();
  42. ? ? }
  43. ?
  44. ? ? /**
  45. ? ? ?* where条件查询或多条件查询
  46. ? ? ?* 类似于:select * from student where name={#student.name} and sex={#student.sex} order by score asc;
  47. ? ? ?*
  48. ? ? ?* @param student
  49. ? ? ?*/
  50. ? ? private void selectAndCondition(Student student) {
  51. ? ? ? ? StudentExample studentExample = new StudentExample();
  52. ? ? ? ? StudentExample.Criteria criteria = studentExample.createCriteria();
  53. ? ? ? ? studentExample.setOrderByClause("score asc"); //升序
  54. ? ? ? ? studentExample.setDistinct(false); //不去重
  55. ? ? ? ? if (StringUtils.isNotBlank(student.getName())) {
  56. ? ? ? ? ? ? criteria.andNameEqualTo(student.getName());
  57. ? ? ? ? }
  58. ? ? ? ? if (StringUtils.isNotBlank(student.getSex())) {
  59. ? ? ? ? ? ? criteria.andSexEqualTo(student.getSex());
  60. ? ? ? ? }
  61. ? ? ? ? List<Student> students = studentDao.selectByExample(studentExample);
  62. ? ? ? ? students.forEach(System.out::println);
  63. ? ? }
  64. ?
  65. ? ? /**
  66. ? ? ?* 类似于:select * from student where name={#student.name} or sex={#student.sex} ;
  67. ? ? ?*
  68. ? ? ?* @param student
  69. ? ? ?*/
  70. ? ? private void selectOrCondition(Student student) {
  71. ? ? ? ? StudentExample studentExample = new StudentExample();
  72. ? ? ? ? StudentExample.Criteria criteria1 = studentExample.createCriteria();
  73. ? ? ? ? StudentExample.Criteria criteria2 = studentExample.createCriteria();
  74. ? ? ? ? if (StringUtils.isNotBlank(student.getName())) {
  75. ? ? ? ? ? ? criteria1.andNameEqualTo(student.getName());
  76. ? ? ? ? }
  77. ? ? ? ? if (StringUtils.isNotBlank(student.getSex())) {
  78. ? ? ? ? ? ? criteria2.andSexEqualTo(student.getSex());
  79. ? ? ? ? }
  80. ? ? ? ? studentExample.or(criteria2);
  81. ? ? ? ? List<Student> students = studentDao.selectByExample(studentExample);
  82. ? ? ? ? students.forEach(System.out::println);
  83. ? ? }
  84. ?
  85. ? ? /**
  86. ? ? ?* 类似于:select * from student where name like %{#student.name}%
  87. ? ? ?*
  88. ? ? ?* @param student
  89. ? ? ?*/
  90. ? ? private void selectLikeCondition(Student student) {
  91. ? ? ? ? StudentExample studentExample = new StudentExample();
  92. ? ? ? ? StudentExample.Criteria criteria = studentExample.createCriteria();
  93. ? ? ? ? if (StringUtils.isNotBlank(student.getName())) {
  94. ? ? ? ? ? ? criteria.andNameLike("%" + student.getName() + "%");
  95. ? ? ? ? }
  96. ? ? ? ? List<Student> students = studentDao.selectByExample(studentExample);
  97. ? ? ? ? students.forEach(System.out::println);
  98. ? ? }
  99. ?
  100. ? ? /**
  101. ? ? ?* 类似于:select * from student limit offset,limit
  102. ? ? ?*/
  103. ? ? public void selectLimit() {
  104. ? ? ? ? StudentExample studentExample = new StudentExample();
  105. ? ? ? ? studentExample.setOffset(2l);
  106. ? ? ? ? studentExample.setLimit(5);
  107. ? ? ? ? List<Student> students = studentDao.selectByExample(studentExample);
  108. ? ? ? ? students.forEach(System.out::println);
  109. ? ? }
  110. }

运行结果:

 官方文档:MyBatis Generator Core – Example Class Usage Notes 

到此这篇关于idea使用Mybatis逆向工程插件详情的文章就介绍到这了,更多相关idea使用Mybatis逆向工程插件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号