- 目的
- 利用generator来生成dao,domain,mapper
- 工具
- oracle
- eclipse generator插件
- mybatis
- 配置文件-本例用到的配置文件如下,参考自谋篇博客,后来没找到那篇博客地址,请作者原谅!本例仅供学习!
- init.properties
-
#Mybatis Generator configuration project=E:/xxxx/Backend/src classPath=D:/ojdbc7.jar jdbc_driver=oracle.jdbc.driver.OracleDriver jdbc_url=jdbc:oracle:thin:@127.0.0.1:15230:xe #ip port xe 可能要改,看个人情况 jdbc_user=test jdbc_password=test
2.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> <!-- 引入配置文件 --> <!-- 可以用于加载配置项或者配置文件,在整个配置文件中就可以使用${propertyKey}的方式来引用配置项 resource:配置资源加载地址,使用resource,MBG从classpath开始找,比如com/myproject/generatorConfig.properties url:配置资源加载地质,使用URL的方式,比如file:///C:/myfolder/generatorConfig.properties. 注意,两个属性只能选址一个; --> <properties resource="generator/init.properties"/> <!-- 指定数据连接驱动jar地址 --> <classPathEntry location="${classPath}"/> <!-- 一个数据库一个context --> <context id="assetsoracle"> <!-- 注释 --> <commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 类型转换 --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="true"/> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.web.base.domain" targetProject="Backend/src/main/java" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false"/> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.web.base.mapper" targetProject="Backend/src/main/java" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.web.base.dao" targetProject="Backend/src/main/java" type="XMLMAPPER" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <table schema="${jdbc_user}" tableName="ROLES" domainObjectName="RolesEntity" enableCountByExample="true" enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true"> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <!-- 忽略列,不生成bean 字段 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
- 问题及解决
- 坑一:init.properties文件中每一行后面不能带空格,就是如classPath=D:/ojdbc7.jar 这行后面不能有空格,否则会提示Exception getting JDBC driver。
- 坑二:targetProject别用绝对路径,否则会提示 “Project E: not exits”,应采用相对路径 "项目名/src/xxx"
- resource 引用配置文件路径应是class path路径,其它路径会报错,提示找不到。仔细领会上面注释部分。
转载于:https://my.oschina.net/wtzheng/blog/904199