源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

mybatis generator 使用方法教程(生成带注释的实体类)

  • 时间:2022-11-25 21:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:mybatis generator 使用方法教程(生成带注释的实体类)
[b]引言:[/b] 最近的一个项目,由于数据库表巨多,导致需要创建N多个java实体、dao、mapper.xml映射文件,如果均使用纯手工编写,无疑需要耗费大量时间和精力。于是上网学习了mybatis generator的使用。 现在项目写完了,闲暇之余把干货奉上,供大家直接使用。 [b]需求场景:[/b] 当你的java 项目数据库有N张表需要使用mybatis进行数据库操作时,建议使用mybatis generator 自动生成工具。可以自动帮助你生成java实体类、dao、mapper.xml等。 首先给大家分享我自己封装好的mybatis generator代码自动生成项目,里面集成了中文注释、mysql的limit分页功能。 git地址:git@github.com:zhaojiatao/com.zjt.mybatisGenerator.git 代码克隆到自己的机器上,import到myeclipse中,[b]需要重新编译[/b]一下,就不会报错了。 此外需要注意[b]需要重新引入一下jar文件夹中的mybatis-generator-plugin-1.0.0.jar[/b],如图: [img]http://files.jb51.net/file_images/article/201708/201708180859176.png[/img] [b]最终目录结构如下[/b]: [img]http://files.jb51.net/file_images/article/201708/201708180859177.png[/img] 接下来,请打开配置文件,如图: (关于generatorConfig.xml的具体教程可参见:http://blog.csdn.net/isea533/article/details/42102297) [img]http://files.jb51.net/file_images/article/201708/201708180859178.png[/img] 接下来,打开generatorConfig.xml,根据你自己的需求,改变如下配置: 首先,修改数据库连接地址。 [img]http://files.jb51.net/file_images/article/201708/201708180859179.png[/img] 期次,声明本次需要操作的表及为即将生成的实体类命名。 [img]http://files.jb51.net/file_images/article/201708/2017081808591710.png[/img] 再次,设置实体文件、dao、mapper.xml生成的路径。 [img]http://files.jb51.net/file_images/article/201708/2017081808591711.png[/img] 最后,运行StartUp.java [img]http://files.jb51.net/file_images/article/201708/2017081808591712.png[/img] 的main方法执行生成操作。 mysql中本地数据库表为 [img]http://files.jb51.net/file_images/article/201708/2017081808591713.png[/img] CREATE TABLE `student` ( `id` varchar(50) NOT NULL COMMENT '主键', `name` varchar(10) DEFAULT NULL COMMENT '姓名', `gender` int(2) DEFAULT NULL COMMENT '性别1男2女', `disc` longtext COMMENT '大文本描述', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 对照表,我们看一下生成的包和文件: [img]http://files.jb51.net/file_images/article/201708/2017081808591714.png[/img] [img]http://files.jb51.net/file_images/article/201708/2017081808591715.png[/img] [img]http://files.jb51.net/file_images/article/201708/2017081808591716.png[/img] 其中Student.java文件当然就是数据库表实体类,对应表的相关字段。 下面,在我们的项目中导入生成的相关文件,如下: [img]http://files.jb51.net/file_images/article/201708/2017081808591717.png[/img] 打开Student.java 我们可以发现字段已经生成了中文注释; [img]http://files.jb51.net/file_images/article/201708/2017081808591718.png[/img] 打开StudentMapper.xml可以发现已经可以使用mysql的limit分页; [img]http://files.jb51.net/file_images/article/201708/2017081808591719.png[/img] 在配置好mybatis的数据库连接后(mybatis相关配置请自行baidu,本文终点介绍mybatis generator的使用),我们开始数据库的相关操作: 打开: testMybatis.java [b]在此,我主要讲几个容易出错的方法和区别:[/b] [b]1.selectByExample和selectByExampleWithBLOBs的区别(包含Example的使用)[/b]
@Test
 public void testQueryStudentExample() {
  SqlSession sqlSession = sqlSessionFactory.openSession(false);
  StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
  try {
   //分页查询性别为男、并且名称中包含z的记录,第一页,每页3条记录,按性别排序
   StudentExample studentExample=new StudentExample();
   studentExample.or().andGenderEqualTo(1).andNameLike("%z%");
   studentExample.setOffset(0);
   studentExample.setLimit(3);
       studentExample.setOrderByClause("GENDER DESC");

   List<Student> list1 = studentMapper.selectByExample(studentExample);
   List<Student> list2 = studentMapper.selectByExampleWithBLOBs(studentExample);
   System.out.println(list1.get(0).getDisc());
   System.out.println(list2.get(0).getDisc());
  } catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback(); 
  }finally {
   sqlSession.close();
  }
 }
结果:[img]http://files.jb51.net/file_images/article/201708/2017081808591720.png[/img] 原因: 由于student表中,disc字段类型为longtext,故如果想要搜索结果包含大字段类型,则必须使用selectByExampleWithBLOBs。无需检索大字段,则使用selectByExample; [b]2.insertSelective和insert的区别[/b] 当有部分字段未设值时,使用insertSelective:
<SPAN style="FONT-SIZE: 14px">@Test
 public void testInsertStudent() { 
  SqlSession sqlSession = sqlSessionFactory.openSession(false); 
  StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 
  try { 
    
   Student s=new Student(); 
   s.setId(java.util.UUID.randomUUID().toString().replaceAll("\\-", "")); 
   s.setName("zjt"); 
   s.setGender(1); 
   s.setDisc("MyBatis Generator 真心好用"); 
   studentMapper.insertSelective(s); 
   sqlSession.commit(); 
    
    
  } catch(Exception e){ 
   e.printStackTrace(); 
   sqlSession.rollback(); 
  }finally { 
   sqlSession.close(); 
  } 
 } 
</SPAN>
结果: [img]http://files.jb51.net/file_images/article/201708/2017081808591721.png[/img] 当有所有字段均已设值时,使用insert;
<SPAN style="FONT-SIZE: 14px">@Test
  public void testInsertStudent() { 
    SqlSession sqlSession = sqlSessionFactory.openSession(false); 
    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 
    try { 
       
      Student s=new Student(); 
      s.setId(java.util.UUID.randomUUID().toString().replaceAll("\\-", "")); 
      s.setName("zjt"); 
      s.setGender(1); 
      s.setDisc("MyBatis Generator 真心好用"); 
      studentMapper.insertSelective(s); 
      sqlSession.commit(); 
       
       
    } catch(Exception e){ 
      e.printStackTrace(); 
      sqlSession.rollback();  
    }finally { 
      sqlSession.close(); 
    } 
  } 
</SPAN> 
结果: [img]http://files.jb51.net/file_images/article/201708/2017081808591722.png[/img] [b]3.修改操作[/b] [img]http://files.jb51.net/file_images/article/201708/2017081808591723.png[/img] [b]updateByExample [/b]        如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段改为null; [b]updateByExampleSelective [/b]    如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段不动; [b]updateByExampleWithBLOBs[/b]    和updateByExample相比此方法可以修改大字段类型,其余性质和updateByExample相同 [b]updateByPrimaryKey  [/b]      如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段改为null; [b]updateByPrimaryKeySelective [/b]   如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段不动; [b]updateByPrimaryKeyWithBLOBs[/b]   和updateByPrimaryKey相比此方法可以修改大字段类型,其余性质和updateByPrimaryKey相同 以上这篇mybatis generator 使用方法教程(生成带注释的实体类)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部