利用mybatis-generator自动生成代码

来源:互联网 发布:晨曦软件视频教程 编辑:程序博客网 时间:2024/06/07 03:33

最近要开发一个网站,正好借此机会重新学习一下Java Web编程SSM框架。

由于不太喜欢Hibernate(作为程序员还是原生的SQL比较亲切),用过一段时间的JdbcTemplate,把SQL语句作为静态变量放在DAO类的开头,对于SQL语句需要拼接或者需要参数的情况,则需要结合静态变量和方法体才能确定具体的SQL语句,非常的不优雅。于是乎,新项目选择使用MyBatis。

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,手动书写很容易出错也很讨厌幸好我们可以利用Mybatis-Generator来帮我们自动生成实体类、DAO接口和Mapping映射文件。

生成代码需要的文件和jar包:

所需文件和jar包

其中src为生成文件保存目录,generatorConfig.xml为生成操作配置文件,mybatis-generator-core-1.3.2.jar是mybatis自动生成所需jar包,mysql-connector-java-5.1.16-bin.jar 为访问MySQL数据库所需jar包,这里我用的是MySQL数据库,如果使用的其他数据库,请使用对应的jar文件。

配置文件generatorConfig.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration>    <!--数据库驱动-->    <classPathEntry    location="mysql-connector-java-5.1.16-bin.jar"/>    <context id="DB2Tables"    targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true"/>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--数据库链接地址账号密码-->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bitnami_redmine" userId="root" password="root">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!--生成Model类存放位置-->        <javaModelGenerator targetPackage="com.ly.model" targetProject="src">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!--生成映射文件存放位置-->        <sqlMapGenerator targetPackage="com.ly.mapping" targetProject="src">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!--生成Dao类存放位置-->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ly.dao" targetProject="src">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!--生成对应表及类名-->        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="news" domainObjectName="News" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>    </context></generatorConfiguration>
其中的配置信息都已经通过注释说明,数据库驱动,数据库连接,生成类所在的包,要操作的表和生成的类名等根据实际情况修改即可。需要生成多个表对应文件则添加多个<table>配置项即可。

配置文件修改完成后,在CMD跳转到该目录下,执行以下生成命令即可:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
可以在jar包所在文件夹中创建一个文本文件,将上述语句拷贝到文本文件中,将其修改为 gen.bat 双击执行即可。

gen.bat内容如下,添加了pause语句,防止执行完直接退出。

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwritepause
运行gen.bat


运行成功后会在src目录下生成所需的实体类、DAO接口和Mapping映射文件。

此过程中,常见的错误如下:

1.XML Parser Error on line 13: 注释中不允许出现字符串 "--"。

原因:文件中存在注释。

解决方式:将注释去掉即可。

 2.XML Parser Error on line 36: XML 文档结构必须从头至尾包含在同一个实体内。

原因:文件中存在注释。

解决方式:将所有注释去掉即可。

相关文件下载地址(包括两个jar包、配置文件、src目录、gen.bat脚本文件):http://download.csdn.net/detail/pyluyuan/9850141。

如果您喜欢本文,可以通过以下方式打赏微笑