出处:http://www.cnblogs.com/lichenwei/p/4145696.html
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1、相关文件
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
和Hibernate逆向生成一样,这里也需要一个配置文件:
generatorConfig.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 /p>
3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。
生成语句文件:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
2、使用方法
在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。
看下效果图:
生成相关代码:
Message.java
1 package lcw.model;
2
3 public class Messgae {
4 private Integer id;
5
6 private String title;
7
8 private String describe;
9
10 private String content;
11
12 public Integer getId() {
13 return id;
14 }
15
16 public void setId(Integer id) {
17 this.id = id;
18 }
19
20 public String getTitle() {
21 return title;
22 }
23
24 public void setTitle(String title) {
25 this.title = title == null ? null : title.trim();
26 }
27
28 public String getDescribe() {
29 return describe;
30 }
31
32 public void setDescribe(String describe) {
33 this.describe = describe == null ? null : describe.trim();
34 }
35
36 public String getContent() {
37 return content;
38 }
39
40 public void setContent(String content) {
41 this.content = content == null ? null : content.trim();
42 }
43 }
MessgaeMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2
3
4
5
6
7
8
9
10
11 id, title, describe, content
12
13
14 select
15
16 from message
17 where id = #{id,jdbcType=INTEGER}
18
19
20 delete from message
21 where id = #{id,jdbcType=INTEGER}
22
23
24 insert into message (id, title, describe,
25 content)
26 values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
27 #{content,jdbcType=VARCHAR})
28
29
30 insert into message
31
32
33 id,
34
35
36 title,
37
38
39 describe,
40
41
42 content,
43
44
45
46
47 #{id,jdbcType=INTEGER},
48
49
50 #{title,jdbcType=VARCHAR},
51
52
53 #{describe,jdbcType=VARCHAR},
54
55
56 #{content,jdbcType=VARCHAR},
57
58
59
60
61 update message
62
63
64 title = #{title,jdbcType=VARCHAR},
65
66
67 describe = #{describe,jdbcType=VARCHAR},
68
69
70 content = #{content,jdbcType=VARCHAR},
71
72
73 where id = #{id,jdbcType=INTEGER}
74
75
76 update message
77 set title = #{title,jdbcType=VARCHAR},
78 describe = #{describe,jdbcType=VARCHAR},
79 content = #{content,jdbcType=VARCHAR}
80 where id = #{id,jdbcType=INTEGER}
81
82
MessgaeMapper.java
1 package lcw.dao;
2
3 import lcw.model.Messgae;
4
5 public interface MessgaeMapper {
6 int deleteByPrimaryKey(Integer id);
7
8 int insert(Messgae record);
9
10 int insertSelective(Messgae record);
11
12 Messgae selectByPrimaryKey(Integer id);
13
14 int updateByPrimaryKeySelective(Messgae record);
15
16 int updateByPrimaryKey(Messgae record);
17 }