mybatis generator eclipse 使用

来源:互联网 发布:品质365 知乎 编辑:程序博客网 时间:2024/06/02 05:50


api:  http://mbg.cndocs.tk/


 下载插件包 

   https://github.com/mybatis/generator/releases  

 选择 1.3.3 

  只支持eclipce 

 eclipce  加入该插件 

  1:   help窗口 

  

  2:

  add选项 

   

    

    location选择插件所在位置   

    name随便写 


3:勾上 

 

取消 更新 

 


选择项目


 



  

生成配置文件 


<?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>  <context id="context1">  <!-- jdbc连接 4要素  -->    <jdbcConnection connectionURL="jdbc:oracle:thin:@localhost:1521:orclKevin" driverClass="oracle.jdbc.OracleDriver" password="tiger" userId="scott" />    <!-- 生成emp的mybatis代码    emp的实体类     -->    <javaModelGenerator targetPackage="cn.zj.mybatis.entity" targetProject="mybatis/src/main/java" />    <!--     生成 接口 映射     -->    <sqlMapGenerator targetPackage="cn.zj.mybatis.dao" targetProject="mybatis/src/main/java" />    <!--     生成注解 或者xml映射      type=XMLMAPPER 就是xml文件         =ANNOTATEDMAPPER 就是注解      -->    <javaClientGenerator targetPackage="cn.zj.mybatis.dao" targetProject="mybatis/src/main/java" type="ANNOTATEDMAPPER" />    <!--     schema =方案名  =用户名      -->    <table schema="scott" tableName="emp"    enableCountByExample="false" enableDeleteByExample="false"    enableSelectByExample="false" enableUpdateByExample="false"    >    </table>  </context></generatorConfiguration>

具体配置见api 


通过Maven运行 MyBatis Generator


1:在pom.xml 中添加 jar包 

   

<dependency>    <groupId>org.mybatis.generator</groupId>        <artifactId>mybatis-generator-core</artifactId>          <version>1.3.3</version>    </dependency>            <dependency>    <groupId>org.mybatis.generator</groupId>        <artifactId>mybatis-generator-maven-plugin</artifactId>          <version>1.3.3</version>    </dependency>

2:在 build里面 集成

  

 <plugin>       <!-- 将MBG(mybatis-generator)集成到Maven  -->        <groupId>org.mybatis.generator</groupId>        <artifactId>mybatis-generator-maven-plugin</artifactId>          <version>1.3.3</version>                    <!--  指定 配置文件地址  配置文件无法生成,要去eclipse 生成或者网上找   -->                    <configuration>          <!-- 指定配置文件地址           这里需要注意的是配置文件中targetProject 必须指定src的全路径          -->          <configurationFile>generatorConfig.xml</configurationFile>          </configuration>                    <!--  获取ojdbc6  -->          <dependencies>          <dependency>      <groupId>generators</groupId>  <artifactId>generators</artifactId>  <version>generators</version>    <scope>system</scope>    <systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>    </dependency>          </dependencies>                  </plugin>

配置文件 

 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>  <context id="context1">  <!-- jdbc连接 4要素  -->    <jdbcConnection connectionURL="jdbc:oracle:thin:@localhost:1521:orclKevin" driverClass="oracle.jdbc.OracleDriver" password="tiger" userId="scott" />    <!-- 生成emp的mybatis代码    emp的实体类     -->    <javaModelGenerator targetPackage="cn.zj.mybatis.entity" targetProject="E:\MyEclipse_code\Workspaces\MyEclipse 8.5\mybatis\src" />    <!--     生成 接口 映射     -->    <sqlMapGenerator targetPackage="cn.zj.mybatis.dao" targetProject="E:\MyEclipse_code\Workspaces\MyEclipse 8.5\mybatis\src" />    <!--     生成注解 或者xml映射  unexpected error while running mybatis generator    type=XMLMAPPER 就是xml文件         =ANNOTATEDMAPPER 就是注解      -->    <javaClientGenerator targetPackage="cn.zj.mybatis.dao" targetProject="E:\MyEclipse_code\Workspaces\MyEclipse 8.5\mybatis\src" type="ANNOTATEDMAPPER" />    <!--     schema =方案名  =用户名      -->    <table schema="scott" tableName="emp"    enableCountByExample="false" enableDeleteByExample="false"    enableSelectByExample="false" enableUpdateByExample="false"    >    </table>  </context></generatorConfiguration>

选中项目 



进入 run 


创建一个新的 build



workspace 选中要输出的项目




写入该jar包的generate方法


run 


原创粉丝点击