1.下載相關jar包https://github.com/mybatis/generator/releases
2.配置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="/Users/zhangxiongfeng/repo/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.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://192.168.5.109:3306/test" userId="root" password="#zhangXF123"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.example.demo.dao.model" targetProject="../../java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成對映檔案的包名和位置--> <sqlMapGenerator targetPackage="mapping" targetProject="../../resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao.mapper" targetProject="../../java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名--> <table tableName="blogs_user_role" domainObjectName="BlogsUserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
3.執行相關命令
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
(注意:相關路徑)
專案結構: