Mybatis在IDEA中使用generator逆向工程生成pojo,mapper

1.创建maven 工程,修改pom.xml

 

 

Java代码   收藏代码
  1. <strong style="margin: 0px; padding: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; background-color: #fffef7;"><build>  
  2.         <plugins>  
  3.             <!-- mybatis逆向工程 -->  
  4.             <plugin>  
  5.                 <groupId>org.mybatis.generator</groupId>  
  6.                 <artifactId>mybatis-generator-maven-plugin</artifactId>  
  7.                 <version>1.3.2</version>  
  8.                 <configuration>  
  9.                     <!--配置文件的位置-->  
  10.                     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>  
  11.                     <verbose>true</verbose>  
  12.                     <overwrite>true</overwrite>  
  13.                 </configuration>  
  14.               </plugin>  
  15.          </plugins>  
  16.     </build></strong>  

 

 

2.在resources下新建   Personal-DB.propertie(数据库连接文件) (也可以写死)

   

Java代码   收藏代码
  1. jdbc.driverLocation=D:\\maven\\com\\oracle\\ojdbc14\\10.2.0.4.0\\ojdbc14-10.2.0.4.0.jar    
  2. jdbc.driverClass=oracle.jdbc.driver.OracleDriver    
  3. jdbc.connectionURL=jdbc:oracle:thin:@//localhost:1521/XE    
  4. jdbc.userId=LOUIS    
  5. jdbc.password=123456    

 

  

 

3.在resources下新建   generatorConfig.xml   文件 (内容如下)

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5. <generatorConfiguration>  
  6.     <properties resource="Personal-DB.properties"></properties>  
  7.     <classPathEntry location="${jdbc.driverLocation}" />  
  8.     <!--classPathEntry location="D:\zngkpt\m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" /-->  
  9.     <context id="context1" targetRuntime="MyBatis3">  
  10.   
  11.         <commentGenerator>  
  12.             <!-- 去除自动生成的注释 -->  
  13.             <property name="suppressAllComments" value="true" />  
  14.         </commentGenerator>  
  15.           
  16.         <!-- 数据库连接配置 -->  
  17.         <jdbcConnection driverClass="${jdbc.driverClass}"  
  18.             connectionURL="${jdbc.connectionURL}"  
  19.             userId="${jdbc.userId}"  
  20.             password="${jdbc.password}" />  
  21.         <!--jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  22.                         connectionURL="jdbc:mysql://localhost:3306/test"  
  23.                         userId="root"  
  24.                         password="mysql" /-->  
  25.   
  26.  <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->  
  27.         <javaTypeResolver>  
  28.             <property name="forceBigDecimals" value="false"/>  
  29.         </javaTypeResolver>  
  30.   
  31.         <!--配置生成的实体包  
  32.             targetPackage:生成的实体包位置,默认存放在src目录下  
  33.             targetProject:目标工程名  
  34.          -->  
  35.         <javaModelGenerator targetPackage="com.unisits.zngkpt.common.userprivrman.pojo"  
  36.             targetProject="src/main/java" />  
  37.               
  38.         <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->  
  39.         <sqlMapGenerator targetPackage="com.unisits.zngkpt.common.userprivrman.mapper" targetProject="src/main/java" />  
  40.   
  41.          <span style="color: #ff0000;"><!-- mapper方法层,默认存放在src目录下 -->  
  42.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.bldz.train.mapper" targetProject="src/main/java">  
  43.                 <property name="enableSubPackages" value="true"/>  
  44.         </javaClientGenerator></span>  
  45.         <!-- 配置表   
  46.             schema:不用填写  
  47.             tableName: 表名  
  48.             enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:  
  49.             去除自动生成的例子  
  50.         -->  
  51.         <table schema="" tableName="sys_role" enableCountByExample="false" enableSelectByExample="false"  
  52.             enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  53.         </table>  
  54.         <table schema="" tableName="sys_permission" enableCountByExample="false" enableSelectByExample="false"  
  55.                enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  56.         </table>  
  57.         <table schema="" tableName="sys_role_permission" enableCountByExample="false" enableSelectByExample="false"  
  58.                enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  59.         </table>  
  60.         <table schema="" tableName="sys_user" enableCountByExample="false" enableSelectByExample="false"  
  61.                enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  62.         </table>  
  63.         <table schema="" tableName="sys_user_role" enableCountByExample="false" enableSelectByExample="false"  
  64.         enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  65.         </table>  
  66.         <table schema="" tableName="unit_info" enableCountByExample="false" enableSelectByExample="false"  
  67.                enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  68.         </table>  
  69.         <table schema="" tableName="unit_type" enableCountByExample="false" enableSelectByExample="false"  
  70.                enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >  
  71.         </table>  
  72.     </context>  
  73. </generatorConfiguration>  

 

 

3,到现在为止,所有的mybatis配置工作已经结束了,开始配置idea来运行生成pojo吧

点击菜单Run->Edit Configuration,然后在弹出窗体的左上角,如下,输入 mybatis-generator:generate -e

然后点击确定

 

 

 4.运行刚才编辑的maven,不出意外就会生成成功

 

  

 

 补充:最终 红色部分会生成 xml  对应method 的 mapper.java 文件 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值