MyBatis3.0代码生成
mybatis3.0提供了代码生成的功能.目前最新的是mybatis-generator-core-1.3.1.这是一个比较灵活的插件.当然不是IDE集成的,只是一个包.可以在命令行中使用,也可以用Ant,Maven甚至直接写到Java代码中来实现MyBatis代码的生成.如果它还是不满足则可以进行拓展.有了它就可以在使用建模工具生成数据库之后,连接数据库来生成相应的基础代码.包括了值对象,数据访问对象的接口及其MyBatis实现.
个人还是喜欢用Ant的方式.下面就用Ant生成的方式来说明.首先写个Ant文件来声明一些属性和几个Target.这些Target对于要生成的模块.分成多个模块便于管理和编译.
build.xml:
- <?xml version="1.0"?>
- <project default="sysGenerator" basedir=".">
- <property name="generated.source.dir" value="${basedir}" />
- <target name="sysGenerator" description="mybatis-generator">
- <taskdef name="sysGenerator"
- classname="org.mybatis.generator.ant.GeneratorAntTask"
- classpath="../Common_lib/mybatis-generator-core-1.3.1.jar" />
- <sysGenerator overwrite="true" configfile="sysGenerator.xml" verbose="false" >
- <propertyset>
- <propertyref name="generated.source.dir"/>
- </propertyset>
- </sysGenerator>
- </target>
- </project>
首先定义了一个名称为sysGenerator的Target.实际操作中可以按模块创建多个.它有个taskdef 定义的是任务的类型和要引用到的包.然后就是它的正文内容.configfile就是配置生成代码的信息.个人建议按照系统分模块.下面看看sysGenerator.xml这个文件的内容
- <generatorConfiguration>
- <classPathEntry location="../Common_lib/mysql-connector-java-5.0.4-bin.jar" />
- <context id="sysGenerator" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressAllComments" value="true" />
- </commentGenerator>
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/leestyle?useUnicode=true&characterEncoding=utf8"
- userId="root" password="">
- </jdbcConnection>
- <!-- Model生成到什么位置-->
- <javaModelGenerator targetPackage="cn.lihk.leestyle.sys.vo"
- targetProject="./src">
- <property name="enableSubPackages" value="true" />
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- sqlMap*.xml生成的位置-->
- <sqlMapGenerator targetPackage="cn.lihk.leestyle.sys.dao.mapper"
- targetProject="./src">
- <property name="enableSubPackages" value="true" />
- </sqlMapGenerator>
- <!-- 客户端应用接口生成的位置(DAO Interface)-->
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="cn.lihk.leestyle.sys.dao" targetProject="./src">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <table tableName="SYS_MENU" enableCountByExample="false"
- enableUpdateByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" selectByExampleQueryId="false">
- <generatedKey column="SYS_MENU_ID" sqlStatement="select uuid()"
- identity="true" />
- </table>
- </context>
- </generatorConfiguration>
这个配置文件提供了 mybatis-generator所需要的参数信息: * 其中classPathEntry 是引用的jdbc的类路径,这里将jdbc jar和generator的jar包放在一起了; * commentGenerator 是用来除去时间信息的,这在配合类似subversion的代码管理工具时使用很有效,因为可以减少没有必要的注释迁入; * jdbcConnection是指定的jdbc的连接信息; * javaTypeResolver式类型转换的信息,这里并没有用到; * javaModelGenerator是模型的生成信息,这里将指定这些Java model类的生成路径; * sqlMapGenerator是mybatis 的sqlMapper XML文件的生成信息,包括生成路径等; * javaClientGenerator是应用接口的生成信息; * table是用户指定的被生成相关信息的表,它必须在指定的jdbc连接中已经被建立。
其实想要mybaits代码生成器生成Example类换个名字,是通过插件实现的,即下面的这个插件配置参数很关键。 <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Criteria" /> </plugin> 如果不喜欢生成的代码中带有代理的mybatis代码生成器的注解信息,可以配置下面的配置参数关闭注解信息的生成: <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator>
如上述代码generatorConfiguration这个根元素下面包含了2个节点.classPathEntry是数据库驱动的位置.context是具体生成规则的配置.generatorConfiguration可以包含多个context节点.context节点有个id属性是必须的官方的说法是:A unique identifier for this context. This value will be used in some error messages.属性targetRuntime是可选的.默认是MyBatis3.另外2个值是Ibatis2Java2和Ibatis2Java5具体意义可查阅文档.commentGenerator是配置注释情况的节点.个人觉得这个注释生成比较难看,于是去掉了.jdbcConnection配置的是数据库连接.javaModelGenerator是值对象的生成,sqlMapGenerator生成SQL的配置文件.当然是简单的语句复杂的操作还是自己写比较靠谱.javaClientGenerator 生成与SQL配置文件对应的接口.table是需要生成的表.enableCountByExample等属性如果是true的话会生成一些Example类,虽说是方便,但是个人不是很喜欢.于是去掉,它生成的代码还是具备参考意义的.generatedKey代表的是插入的时候是否生成主键,它的sqlStatement属性是配置生成主键的语句.这个可以定制确实是方便,因为有的项目主键规则是用一张表来维护的,跟数据库自身的生成规则的无关的.table可以有多个.这边只是为了节省篇幅.
有了这个生成方法.在PowerDesigner进行数据建模后直接生成数据库,然后用它生成值对象和数据访问层的代码,煞是节省时间.想想一张表要是出现了60几个字段,那重复写那些字段岂不是要写到崩溃.简单重复的动作通通交给它来生成.复杂的操作.由编码人员自己来进行定制.提升开发效率.