1. 程式人生 > >MyBatis-MyEclipse+MyBatis-Generator外掛

MyBatis-MyEclipse+MyBatis-Generator外掛

外掛準備:

資料庫(mysql)表準備:

    CREATE TABLE `role` (
      `id` int(11) NOT NULL auto_increment,
      `rolename` varchar(20) default NULL,
      `note` varchar(100) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

建立專案MyBatis

建立相關的包

  • com.yan.po
  • com.yan.dao
  • com.yan.mapper

建立資源包

  • src/main/resources

生成generatorConfig.xml

  • 右擊專案
  • new
  • Other
  • MyBatis Generator Configuration File
  • Next
  • Location中填寫存放的位置:/MyBatis/src/main/resources
  • Finish

配置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="F:\Maven\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> <context id="context1" > <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 資料庫連結URL、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mysql" userId="root" password="mysql" /> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.yan.po" targetProject="MyBatis/src/main/java" /> <!-- 生成的對映檔案報名和位置 --> <sqlMapGenerator targetPackage="com.yan.mapper" targetProject="MyBatis/src/main/java" /> <!-- 生成DAO的包名和位置 --> <javaClientGenerator targetPackage="com.yan.dao" targetProject="MyBatis/src/main/java" type="XMLMAPPER" /> <!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) --> <table schema="mysql" tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <columnOverride column="id" property="id" /> <columnOverride column="rolename" property="roleName" /> <columnOverride column="note" property="note"/> </table> </context> </generatorConfiguration>

生成檔案

右擊generatorConfig.xml,選擇Generator MyBatis/IBATIS Artifacts

參考