MyBatis Generator的使用
本文是基于Intellij+Maven实现。
MyBatis Generator的详细介绍http://mybatis.github.io/generator/index.html
MyBatis Generator With Maven
http://mybatis.github.io/generator/running/runningWithMaven.html
搭建逆向工程
1.创建一个Maven项目:File——New Project——Maven
2.在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件,IDE会自动帮我们下载插件

3.在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
(官方配置以及说明:http://mybatis.github.io/generator/configreference/xmlconfig.html)



4.这里使用了外置的配置文件generator.properties,可以将一下属性配置到properties文件之中,增加配置的灵活性:

5.点击Maven Project——项目——Plugins——mybatis generator——Run Maven build

可以看下我们生成的文件:

6.可以根据自己项目的配置,把生成的代码拷贝到自己的项目中去。mybatis设计比较巧妙,Dao层就不用说了,User类就是普通的实体类,定义了数据库对应的字段,以及set/get方法;Example类是干嘛的呢?Example中提供了Critertia,一种面向对象的查询方式,并且根据实体类中字段的属性,生成不同的操作。当然你也可以根据实际需要直接使用实体类进行增删改查。