mybatis自动生成工具generator的用法(springboot)
1.eclipse中安装generator工具
2.在springboot项目的application.properties文件中配置数据源,例如:
1 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crm?useSSL=false&connectTimeout=1000&socketTimeout=30000&autoReconnect=true&useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&allowMultiQueries=true 2 spring.datasource.username=zhoushuo 3 spring.datasource.password=zhoushuo 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.在application.properties同级目录配置generatorConfig.xml文件,例如:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 3 <generatorConfiguration> 4 <properties resource="application.properties" /> 5 6 <context id="Tables" targetRuntime="MyBatis3"> 7 8 <!-- 注释 --> 9 <commentGenerator> 10 <!-- 是否生成注释代时间戳 --> 11 <property name="suppressDate" value="true"/> 12 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 13 <property name="suppressAllComments" value="true"/> 14 </commentGenerator> 15 16 <!-- JDBC连接 --> 17 <jdbcConnection 18 driverClass="${spring.datasource.driver-class-name}" 19 connectionURL="${spring.datasource.url}" 20 userId="${spring.datasource.username}" 21 password="${spring.datasource.password}"> 22 </jdbcConnection> 23 24 <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> 25 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 26 NUMERIC 类型解析为java.math.BigDecimal --> 27 <javaTypeResolver> 28 <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> 29 <property name="forceBigDecimals" value="false" /> 30 </javaTypeResolver> 31 32 <!-- 生成实体类地址 --> 33 <javaModelGenerator targetPackage="com.dadaabc.crm.dts.model" targetProject="demo/src/main/java"> 34 <!-- 从数据库返回的值被清理前后的空格 --> 35 <property name="trimStrings" value="true" /> 36 <!-- enableSubPackages:是否让schema作为包的后缀 --> 37 <property name="enableSubPackages" value="false" /> 38 </javaModelGenerator> 39 40 <!-- 生成mapper xml文件 --> 41 <sqlMapGenerator targetPackage="mapper" targetProject="demo/src/main/resources"> 42 <!-- enableSubPackages:是否让schema作为包的后缀 --> 43 <property name="enableSubPackages" value="false" /> 44 </sqlMapGenerator> 45 46 <!-- 生成mapper xml对应Client--> 47 <javaClientGenerator targetPackage="com.dadaabc.crm.dts.dao" targetProject="demo/src/main/java" type="XMLMAPPER"> 48 <!-- enableSubPackages:是否让schema作为包的后缀 --> 49 <property name="enableSubPackages" value="false" /> 50 </javaClientGenerator> 51 52 <!-- 配置表信息 --> 53 <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 54 是否生成 example类 --> 55 56 <table schema="crm" tableName="student" 57 domainObjectName="Student" enableCountByExample="false" 58 enableDeleteByExample="false" enableSelectByExample="false" 59 enableUpdateByExample="false"> 60 </table> 61 62 <table schema="crm" tableName="lead" 63 domainObjectName="Lead" enableCountByExample="false" 64 enableDeleteByExample="false" enableSelectByExample="false" 65 enableUpdateByExample="false"> 66 </table> 67 68 <table schema="crm" tableName="lead_information" 69 domainObjectName="LeadInformation" enableCountByExample="false" 70 enableDeleteByExample="false" enableSelectByExample="false" 71 enableUpdateByExample="false"> 72 </table> 73 74 </context> 75 </generatorConfiguration>
4.右键generatorConfig.xml,Run As—>Run Mytatis Generator,即可。
注意点:
如果报:WARNING: Project src does not exist
原因是:targetProject=”demo/src/main/java”这里不能写src/main/java这个,一定要加上项目名