mybatis 自动生成integer_使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

出处:http://www.cnblogs.com/lichenwei/p/4145696.html

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。

1、相关文件

我自己实际用到的是第一个和第三个文件,

可以自己新建一个文件夹叫做mybatis-generator,解压源码后,将解压的文件放置新建的src文件内部,解压第一个文件中,将其中lib下的jar提取出来放置与src同级别的目录下,将下方介绍的config.xml文件也放置一起,最终形成的文件夹目录如下,这里用的是sqlserver:

下面这个是用mysql,

如果是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动mysql-connector-java jar包

如果是使用sqlserver,需要准备sqljdbc4.jar包

需要添加的依赖

1

2 org.mybatis.generator

3 mybatis-generator

4 1.3.7

5

6

7 org.mybatis.generator

8 mybatis-generator-maven-plugin

9 1.3.7

10

需要的一个配置文件:

generatorConfig.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2 "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)。

下面是sqlserver的配置方式

1 <?xml version="1.0" encoding="UTF-8"?>

2 "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

生成语句文件:

java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

2、使用方法

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

但是win10出现的是powershell命令窗口,所以我是直接用windows+R,输入cmd命令,然后进入其文件夹下面,执行此命令,上述是网上的窗口,下面是自己的运行窗口,所以目录可能不一样

看下效果图:

这是用网上m'ysql对应配置文件的版本

下面是使用sqlserver版本的,,我自己创建的

生成相关代码mysql版本的:

Message.java

1 packagelcw.model;2

3 public classMessgae {4 privateInteger id;5

6 privateString title;7

8 privateString describe;9

10 privateString content;11

12 publicInteger getId() {13 returnid;14 }15

16 public voidsetId(Integer id) {17 this.id =id;18 }19

20 publicString getTitle() {21 returntitle;22 }23

24 public voidsetTitle(String title) {25 this.title = title == null ? null: title.trim();26 }27

28 publicString getDescribe() {29 returndescribe;30 }31

32 public voidsetDescribe(String describe) {33 this.describe = describe == null ? null: describe.trim();34 }35

36 publicString getContent() {37 returncontent;38 }39

40 public voidsetContent(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, content12

13

14 select15

16 from message17 where id = #{id,jdbcType=INTEGER}18

19

20 delete from message21 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 message31

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 message62

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 message77 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 packagelcw.dao;2

3 importlcw.model.Messgae;4

5 public interfaceMessgaeMapper {6 intdeleteByPrimaryKey(Integer id);7

8 intinsert(Messgae record);9

10 intinsertSelective(Messgae record);11

12 Messgae selectByPrimaryKey(Integer id);13

14 intupdateByPrimaryKeySelective(Messgae record);15

16 intupdateByPrimaryKey(Messgae record);17 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值