使用数据库可以自己选择
下面大概讲一下两种方法使用命令和Eclispe集成;
1.Eclipse中集成MyBatis Generator
- 点击Help>Eclipse Marketplace>输入MyBatis Generator ,选择安装(安装完后会提示你重启Eclipse)
- 在项目中右键New>Other-选择MyBatis Generator Configuration File
- 打开generatorConfig.xml,修改配置文件
- 右键 Run AS>Run MyBatis Generator。因为使用的是高版本的mysql,故mysql的驱动、以及URL需要重点关注(低版本按正常来写就可以了)
<?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="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成true:是:false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接的信息:驱动类,链接地址,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/maven?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false" userId="" password=""> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL和NUMERIC类型解析为integer,为true时把JDBC DECIMAL和numeric类型解析为 java.math.BigDECIMAL --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.javen.entity" targetProject="Carmodel/src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper接口生成的位置 --> <sqlMapGenerator targetPackage="com.javen.mapper" targetProject="Carmodel/src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.javen.mapper" targetProject="Carmodel/src/main/java" type="XMLMAPPER"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="user_t" domainObjectName="User_t" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="ID" sqlStatement="MYSQL" identity="true" /> <!-- <columnOverride column="???" property="???" /> --> </table> </context> </generatorConfiguration>
备注:在XML中配置&符号,会报错。需要将&修改为&详细可百度自行了解
- 在项目就可以找到生成的实体类、mapper
2.直接使用MyBatis Generator配置xml文件生成
- 下载文件链接:https://pan.baidu.com/s/1zb3n7n6T17ryht5I_p6JGw
提取码:2r3m
- 修改generator.xml,修改如上述配置文件一样
- 运行命令java -jar mybatis.jar -configfile generator.xml -overwrite
- 生成完毕