MyBatis Generator
简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
【1】POM文件
引入依赖如下:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> <!--<configurationFile>--> <!--${basedir}/src/main/resources/generator/generatorConfig.xml--> <!--</configurationFile>--> </configuration> </plugin> </plugins>
【2】generator配置文件
generatorConfig.properties
validationQuery=SELECT 1 jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://127.0.0.1:3306/hhprovince?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true jdbc_username=root jdbc_password=123456 tableName=user_employee domainObjectName=SysEmployee
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> <!-- 引用系统全局配置文件 --> <properties resource="generator/generatorConfig.properties" /> <classPathEntry location="E:/softinstall/mavenRep/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar"/> <context id="hh" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="${jdbc_driverClassName}" connectionURL="${jdbc_url}" userId="${jdbc_username}" password="${jdbc_password}"> </jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.hh.sys.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="com.hh.sys.mappers" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.hh.sys.dao" implementationPackage="com.hh.sys.dao.impl" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="${tableName}" domainObjectName="${domainObjectName}" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
关于最后一段的说明
如下所示会只对表tb_sys_active进行逆向,生成的目标pojo为domainObjectName属性指定的值。另外<domainObjectRenamingRule searchString="^Tb" replaceString="" />将会过滤掉tb_前缀,注意searchString="^Tb"中T为大写。
<table schema="general" tableName="tb_sys_active" domainObjectName="SysActive" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <domainObjectRenamingRule searchString="^Tb" replaceString="" /> </table>
如下所示,将会对数据库所有表进行逆向:
<table schema="general" tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <domainObjectRenamingRule searchString="^Tb" replaceString="" /> </table>
关于generator配置文件位置说明
默认情况下,插件会先去src/main/resources下找generatorConfig.xml
。如果在pom中插件位置进行了配置文件配置,则会再去指定地方找:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> <configurationFile> ${basedir}/src/main/resources/generator/generatorConfig.xml </configurationFile> </configuration> </plugin>
【3】application.yml配置
主要是数据源的配置:
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/hhprovince?serverTimezone=GMT%2B8 username: root password: 123456 # driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: true testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath*:mybatis/mapper/*.xml # 下面可以不要 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql serv
【4】maven命令运行
maven命令:
mybatis-generator:generate -e
在IntelliJ IDEA中的操作步骤如下:
首先选择Run->Edit Configurations…
然后点击左上角的“+”号,选择Maven
最后在Working directory中填入你项目的根目录,然后在下面的Command line中填入mybatis-generator:generate -e。点击OK即可。如下图所示:
idea直接在右侧插件地方执行