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

时间:2023-03-09 22:09:05
mybatis generator 使用教程(生成带注释的实体类)

引言:

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

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


需求场景:

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


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

git地址:git@github.com:zhaojiatao/com.zjt.mybatisGenerator.git

代码克隆到自己的机器上,import到myeclipse中,需要重新编译一下,就不会报错了。

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

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

最终目录结构如下

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

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

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

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

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

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

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

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

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

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

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

最后,运行StartUp.java

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

的main方法执行生成操作。


mysql中本地数据库表为

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

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

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

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

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

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

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

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

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

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

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


在配置好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();
}
}

结果:mybatis generator 使用教程(生成带注释的实体类)

原因:

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

2.insertSelective和insert的区别

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

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

  

结果:mybatis generator 使用教程(生成带注释的实体类)

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

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

  结果:mybatis generator 使用教程(生成带注释的实体类)

3.修改操作

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

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

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

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

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

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

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

综上稍加练习,基本可以使用了。多说无益,不如敲起来。

我自己的demo:

git@github.com:zhaojiatao/testgenerator.git