SpringMVC——(SSM框架)使用MyBatis Generator自動建立程式碼
阿新 • • 發佈:2018-11-20
首先要用到以下幾個jar包(沒有的可以自行上網搜尋資源或者留言找我要喲~)
其中有mybatis框架的jar包,資料庫驅動程式jar包以及MyBatis生成器jar包。其中的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="mysql-connector-java-5.1.25-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--資料庫連結URL,使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://47.94.3.109/takoutShopping" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.Takout.entitys" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成對映檔案的包名和位置--> <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.Takout.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名--> <table tableName="buyer" domainObjectName="Buyer" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="good" domainObjectName="Good" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="seller" domainObjectName="Seller" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
大家根據相關的註釋更改配置就好了。
完成上面這些配置,
接下來先在控制檯定位到安裝目錄
再執行下列語句
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
即
這就生成好了。
現在我們就可以在src目錄下找到相應的資料夾,每個表格都會對應三個檔案(實體類、介面、配置檔案)。
我們去看看。
好啦,這就都生成了。
大家可以自己去試一試啦~~