天天看點

Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼

一. 說明: Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼 二. 準備:MyBatis_Generator相關jar MyBatis_Generator和項目源碼百度網盤下載下傳: http://pan.baidu.com/s/1boUJhW7 三. 在myeclipse中引入MyBatis_Generator插件 1.找到myeclipse安裝目錄下的features和plugins檔案夾,如果沒有則建立。

Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼
Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼

2.将下載下傳的MyBatis_Generator裡面對應的檔案家裡面的jar包拷貝到對應的myeclipse檔案夾下 3.重新開機myeclipse在某個項目上右鍵->建立->other 裡面,能看到已經加入了插件如下圖

Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼
Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼

四.使用插件生成代碼 1.繼續上述操作,點選next在項目下建立一個generatorConfiguration.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" > <jdbcConnection driverClass="???" connectionURL="???" userId="???" password="???" /> <javaModelGenerator targetPackage="???" targetProject="???" /> <sqlMapGenerator targetPackage="???" targetProject="???" /> <javaClientGenerator targetPackage="???" targetProject="???" type="XMLMAPPER" /> <table schema="???" tableName="???" > <columnOverride column="???" property="???" /> </table> </context> </generatorConfiguration> 5.配置generatorConfiguration.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" > <commentGenerator> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 資料庫連接配接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/kiboy-maven" userId="root" password="root" /> <!-- 實體類檔案生成位置 --> <javaModelGenerator targetPackage="com.luo.domain" targetProject="kiboy-maven/src/main/java" /> <!-- mapper.xml檔案位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="kiboy-maven/src/main/resources" /> <!-- dao檔案生成位置 --> <javaClientGenerator targetPackage="com.luo.dao" targetProject="kiboy-maven/src/main/java" type="XMLMAPPER" /> <!-- domainObjectName對應的POJO的類名,其它true,false去掉一些生成example無用的代碼 --> <table tableName="t_user" domainObjectName="TUser" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> <!-- 下面是需要在實體類裡面生成的字段 --> <columnOverride column="id" property="userId" /> <columnOverride column="user_name" property="userName" /> <columnOverride column="user_password" property="userPassword" /> <columnOverride column="address" property="address" /> <columnOverride column="age" property="age" /> </table> <table tableName="student" domainObjectName="Student" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> <columnOverride column="id" property="id" /> <columnOverride column="password" property="password" /> <columnOverride column="sex" property="sex" /> <columnOverride column="idcard" property="idcard" /> </table> <table tableName="fee" domainObjectName="Fee" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> <table tableName="bookcost" domainObjectName="Bookcost" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> </context> </generatorConfiguration>

2.配置好後右鍵配置檔案點選 Generate MyBatis/iBATIS Artifacts自動生成代碼如下

Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼
Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼
Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼
Maven Web項目使用MyBatis_Generator_1.3.1自動生成javabean,dao,mapper.xml代碼

注意:發現實體類多了一個FeeKey.java檔案,這是因為fee表裡面設定的是兩個字段的關聯主鍵。 并且如果再執行一次生成操作,這些java檔案就會報錯,因為,再次生成對檔案做的是追加操作, 是以會有重複方法和變量,就會報錯,通過配置檔案來配置的解決方式目前還沒有找到,百度不到啊(┬_┬)!!!,是以就需要先删除原有的代碼檔案 3.如有不合理之處,歡迎提意見!此篇完結!