使用MyBatis Generator自动创建代码

来源:互联网 发布:君知其难也的其 编辑:程序博客网 时间:2024/06/06 07:15

使用MyBatis Generator自动创建代码

由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。这样可以省去很多的功夫,将生成的代码copy到项目工程中即可。

           使用自动生成有很多方式,可以在eclipse中安装插件,但是以下将要介绍的这种方式我认为很轻松,最简单,不需要装插件,只需要下几个jar包即可,把它们放在一个目录下面。



generatorConfig.xml的内容:项目要先创建三个包名(com.pojo;   com.mappinng;   com.dao;),targetProject的设置我弄了好久,用相对地址:(项目名\src)弄不好,结果只能用绝对地址了。别人都说用相对地址就可以了,迷惑,我用就报错:

The specified target project directory mybatis\src does not exist
The specified target project directory mybatis\src does not exist
The specified target project directory mybatis\src does not exist
MyBatis Generator finished successfully, there were warnings.



<?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.40-bin.jar"/>      <context id="DB2Entity"  targetRuntime="MyBatis3">          <commentGenerator>              <property name="suppressDate" value="true"/>              <!-- 是否去除自动生成的注释 true:是 : false:否 -->              <property name="suppressAllComments" value="true"/>          </commentGenerator>          <!--数据库URL,用户名、密码 -->          <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/books?useSSL=true" userId="root" password="123456">          </jdbcConnection>          <javaTypeResolver>              <property name="forceBigDecimals" value="false"/>          </javaTypeResolver>          <!-- 生成实体的包名和位置-->          <javaModelGenerator targetPackage="com.pojo" targetProject="C:\Users\Administrator\workspace1\mybatis\src">                <property name="enableSubPackages" value="true"/>                <property name="trimStrings" value="true"/>            </javaModelGenerator>              <!-- 生成映射文件的包名和位置-->          <sqlMapGenerator targetPackage="com.mapping" targetProject="C:\Users\Administrator\workspace1\mybatis\src">              <property name="enableSubPackages" value="true"/>          </sqlMapGenerator>          <!-- 生成DAO的包名和位置-->          <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="C:\Users\Administrator\workspace1\mybatis\src">              <property name="enableSubPackages" value="true"/>          </javaClientGenerator>          <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->       <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->            <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>       </context>  </generatorConfiguration>  

 当以上这些完成之后,只需要打开控制台,进入项目的lib目录下,执行脚本:

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

       即可。

这样在生成之后,就可以在src目录下找到相应的文件夹,每个表格都会对应三个文件(实体类、接口、配置文件)。