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

2025-05-29 0 94

引言:

最近的一个项目,由于数据库表巨多,导致需要创建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.csdn.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的使用)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
@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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<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>

结果:

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<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>

结果:

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相同

以上这篇mybatis generator 使用方法教程(生成带注释的实体类)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/zhaojiatao/archive/2017/08/17/7380608.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 mybatis generator 使用方法教程(生成带注释的实体类) https://www.kuaiidc.com/115269.html

相关文章

发表评论
暂无评论