generator如何在mybatis中使用

发布时间:2020-12-02 15:56:29 作者:Leah
来源:亿速云 阅读:212

这期内容当中小编将会给大家带来有关 generator如何在mybatis中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

引言:

最近的一个项目,由于数据库表巨多,导致需要创建N多个java实体、dao、mapper.xml映射文件,如果均使用纯手工编写,无疑需要耗费大量时间和精力。于是上网学习了mybatis generator的使用。

现在项目写完了,闲暇之余把干货奉上,供大家直接使用。

需求场景:

当你的java 项目数据库有N张表需要使用mybatis进行数据库操作时,建议使用mybatis generator 自动生成工具。可以自动帮助你生成java实体类、dao、mapper.xml等。

首先给大家分享我自己封装好的mybatis generator代码自动生成项目,里面集成了中文注释、mysql的limit分页功能。

此外需要注意需要重新引入一下jar文件夹中的mybatis-generator-plugin-1.0.0.jar,如图:

generator如何在mybatis中使用

最终目录结构如下

generator如何在mybatis中使用

接下来,请打开配置文件,如图:

(关于generatorConfig.xml的具体教程可参见:http://blog.csdn.net/isea533/article/details/42102297)

generator如何在mybatis中使用

接下来,打开generatorConfig.xml,根据你自己的需求,改变如下配置:

首先,修改数据库连接地址。

generator如何在mybatis中使用

期次,声明本次需要操作的表及为即将生成的实体类命名。

generator如何在mybatis中使用

再次,设置实体文件、dao、mapper.xml生成的路径。

generator如何在mybatis中使用

最后,运行StartUp.java

generator如何在mybatis中使用

的main方法执行生成操作。

mysql中本地数据库表为

generator如何在mybatis中使用

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

对照表,我们看一下生成的包和文件:

generator如何在mybatis中使用generator如何在mybatis中使用generator如何在mybatis中使用

其中Student.java文件当然就是数据库表实体类,对应表的相关字段。

下面,在我们的项目中导入生成的相关文件,如下:

generator如何在mybatis中使用

打开Student.java 我们可以发现字段已经生成了中文注释;

generator如何在mybatis中使用

打开StudentMapper.xml可以发现已经可以使用mysql的limit分页;

generator如何在mybatis中使用

在配置好mybatis的数据库连接后(mybatis相关配置请自行baidu,本文终点介绍mybatis generator的使用),我们开始数据库的相关操作:

打开: testMybatis.java

在此,我主要讲几个容易出错的方法和区别:

1.selectByExample和selectByExampleWithBLOBs的区别(包含Example的使用)

@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();
  }
 }

结果:generator如何在mybatis中使用

原因:

由于student表中,disc字段类型为longtext,故如果想要搜索结果包含大字段类型,则必须使用selectByExampleWithBLOBs。无需检索大字段,则使用selectByExample;

2.insertSelective和insert的区别

当有部分字段未设值时,使用insertSelective:

<SPAN >@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>

结果:

generator如何在mybatis中使用

当有所有字段均已设值时,使用insert;

<SPAN >@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> 

结果:

generator如何在mybatis中使用

3.修改操作

generator如何在mybatis中使用

updateByExample        

如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段改为null;

updateByExampleSelective    

如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段不动;

updateByExampleWithBLOBs   

和updateByExample相比此方法可以修改大字段类型,其余性质和updateByExample相同

updateByPrimaryKey       

如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段改为null;

updateByPrimaryKeySelective   

如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段不动;

updateByPrimaryKeyWithBLOBs  

和updateByPrimaryKey相比此方法可以修改大字段类型,其余性质和updateByPrimaryKey相同

上述就是小编为大家分享的 generator如何在mybatis中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. MyBatis Plus Generator生成entity
  2. 使用mybatis-generator工具加快开发速度

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis generator

上一篇:利用RecyclerView进行滑动时如何指定Position

下一篇:使用springboot怎么结合mqtt进行开发

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》