2016年做网站能赚钱吗,外贸自建站如何收款,网络服务平台有哪些,什么是网络营销?常见的网络营销方式有哪些?mybatis代码生成器generator, 生成文件名自定义配置 MyBatis-Generator 使用一. 引入依赖和相关插件二.设置配置文件三.运行四. mybatis-generator 自定义生成的文件名一.domainObjectName和mapperName属性二.domainObjectRenamingRule标签三. RenameExampleClassPlugin插件四.… mybatis代码生成器generator, 生成文件名自定义配置 MyBatis-Generator 使用一. 引入依赖和相关插件二.设置配置文件三.运行四. mybatis-generator 自定义生成的文件名一.domainObjectName和mapperName属性二.domainObjectRenamingRule标签三. RenameExampleClassPlugin插件四. columnRenamingRule标签 MyBatis-Generator 使用
一. 引入依赖和相关插件 dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.0/version/dependencybuildplugins!-- 引入 MyBatis-Generator 插件在通过 Maven 的插件运行 MBG 时有效 --plugingroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-maven-plugin/artifactIdversion1.3.6/versionconfiguration!-- 引入 MyBatis-Generator 的配置文件 --configurationFile./src/main/resources/mybatis-generator-config.xml/configurationFile!-- 允许 MBG 将构建消息写入日志中 --verbosetrue/verbose!-- 再次运行 MBG 时允许覆盖已生成的文件但是不会覆盖 xml 文件 --overwritetrue/overwrite/configurationdependencies!-- 引入 mysql 的 JDBC 驱动否则会报错找不到驱动 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql.version}/version/dependencydependencygroupIdcom.itfsw/groupIdartifactIdmybatis-generator-plugin/artifactIdversion1.3.10/version/dependency/dependencies/plugin/plugins/build二.设置配置文件 !-- 所有的配置均在 generatorConfiguration 标签下 --
generatorConfiguration!-- 引入外部配置文件 --properties resourcemybatis-generator.properties/!-- 加载需要的额外的依赖包 --!-- classPathEntry location/Users/deecyn/Files/db2java.zip/--!-- 配置 context 上下文 --context idMySqlContext targetRuntimeMyBatis3 defaultModelTypeflat!-- 自动识别数据库关键字默认为 false一般保留默认值遇到数据库关键字Java关键字时按照 table 元素中 columnOverride 属性的配置进行覆盖如果设置为 true 则需按照 SqlReservedWords 中定义的关键字列表对关键字进行定界分隔定界符分隔符参见 beginningDelimiter 和 endingDelimiter 的设置--property nameautoDelimitKeywords valuefalse/!-- beginningDelimiter 和 endingDelimiter定界符分隔符指明用于标记数据库关键字的符号默认为为双引号 ()在 oracle 中是双引号 ()在 MySQL 中需配置为反引号 () --property namebeginningDelimiter value/property nameendingDelimiter value/!-- 生成的 Java 文件的编码 --property nameJavaFileEncoding valueUTF-8/!-- 格式化 Java 代码 --property namejavaFormatter valueorg.mybatis.generator.api.dom.DefaultJavaFormatter/!-- 格式化 XML 代码 --property namexmlFormatter valueorg.mybatis.generator.api.dom.DefaultXmlFormatter/!-- 使生成的 Model 实现 Serializable 接口 --plugin typeorg.mybatis.generator.plugins.SerializablePlugin/!-- 为生成的 Model 覆写 toString() 方法 --plugin typeorg.mybatis.generator.plugins.ToStringPlugin/!-- 为生成的 Model 覆写 equals() 和 hashCode() 方法 --plugin typeorg.mybatis.generator.plugins.EqualsHashCodePlugin/!-- 数据Model属性对应Column获取插件 --plugin typecom.itfsw.mybatis.generator.plugins.ModelColumnPlugin/!-- 批量插入插件 --plugin typecom.itfsw.mybatis.generator.plugins.BatchInsertPlugin/!-- 配置注释生成器 --commentGenerator!-- 不生成所有注释默认为 false --property namesuppressAllComments valuetrue/!-- 生成的注释中不包含时间信息默认为 false --property namesuppressDate valuetrue/!-- 生成的注释中时间的显示格式 --property namedateFormat valueyyyy/MM/dd/!-- 是否添加数据库表中字段的注释默认为 false --property nameaddRemarkComments valuetrue//commentGenerator!-- 引用自定义的注释生成器 --!-- commentGenerator typedeecyn.shop_02.mbg.MyCommentGenerator property nameauthor valueDeecyn//commentGenerator--!-- 配置数据库连接 --jdbcConnection driverClass${jdbc.driverClass}connectionURL${jdbc.connectionURL}userId${jdbc.userId}password${jdbc.password}!-- 若为 8.0 版本以上的 mysql-connector-java 驱动需要设置 nullCatalogMeansCurrent true --!-- property namenullCatalogMeansCurrent valuetrue/ --/jdbcConnection!-- 配置类型转换规则 --javaTypeResolver!-- 是否强制使用 BigDecimal默认为 false把 JDBC 的 DECIMAL 和 NUMERIC 类型解析为 Integer设置为 true 时把 JDBC 的 DECIMAL 和 NUMERIC 类型解析为 java.math.BigDecimal --property nameforceBigDecimals valuetrue/!-- 设置时间类型的转换默认 false将所有 JDBC 的时间类型解析为 java.util.Date设置为 true 时将 JDBC 的时间类型按如下规则解析DATE - java.time.LocalDateTIME - java.time.LocalTimeTIMESTAMP - java.time.LocalDateTimeTIME_WITH_TIMEZONE - java.time.OffsetTimeTIMESTAMP_WITH_TIMEZONE - java.time.OffsetDateTime--property nameuseJSR310Types valuetrue//javaTypeResolver!-- 配置 Java 模型生成器 --javaModelGenerator targetPackage${package.model} targetProjectsrc/main/java!-- 自动为每一个生成的类创建一个构造方法构造方法包含了所有的 field而不是使用 setter默认值为 false --property nameconstructorBased valuefalse/!-- 在 targetPackage 的基础上根据数据库的 schema 再生成一层 package最终生成的类放在这个package下默认为false --property nameenableSubPackages valuefalse/!-- 是否创建一个不可变的类如果为true那么 MBG 生成的类会没有 setter 方法采用构造函数的方式来接收和设置每个字段的值此时会忽略 constructorBased 属性的设置默认值为 false --property nameimmutable valuefalse/!-- 设置在 getter 方法中是否对 String 类型的字段调用 trim() 方法默认为 false --property nametrimStrings valuetrue//javaModelGenerator!-- SQL Map 的 xml 文件生成器 --sqlMapGenerator targetPackage${package.sqlMapper} targetProjectsrc/main/resources!-- 同 javaModelGenerator 元素中的配置 --property nameenableSubPackages valuefalse//sqlMapGenerator!-- 关于 Mapper 接口的生成 --javaClientGenerator typeXMLMAPPER targetPackage${package.mapper}targetProjectsrc/main/java!-- 同 javaModelGenerator 元素中的配置 --property nameenableSubPackages valuefalse//javaClientGenerator!-- 配置需要生成代码的数据库表 --!-- table tableNamerole domainObjectNameRole--!-- enableCountByExamplefalse enableUpdateByExamplefalse--!-- enableDeleteByExamplefalse enableSelectByExamplefalse--!-- selectByExampleQueryIdfalse--!-- lt;!ndash; 指定是否只生成 domain 类默认为 false--!-- 如果设置为 true则只生成 domain 类如果还配置了sqlMapGenerator那么--!-- 在 mapper.xml 文件中只生成 resultMap 元素 ndash;gt;--!-- property namemodelOnly valuefalse/--!-- lt;!ndash; 默认为 false如果设置为 true生成的 model 类会直接使用 column 本身的名字而不会再使用驼峰命名方法--!-- 比如 CREATE_DATE生成的属性名字就是 CREATE_DATE,而不会是 createDate ndash;gt;--!-- property nameuseActualColumnNames valuefalse/--!-- lt;!ndash; 生成主键的方法如果设置了该元素MBG 会在生成的 insert 元素中生成一条正确的 selectKey 元素 ndash;gt;--!-- generatedKey columnid sqlStatementMySql identitytrue/--!-- lt;!ndash; 用来修改表中某个列的属性MBG 会根据修改后的配置来生成 domain 的属性--!-- column要重新设置的列名一个 table 元素中可以定义多个 columnOverride 元素哈 ndash;gt;--!-- columnOverride columnshow_status--!-- lt;!ndash; 使用 property 属性来指定列要生成的属性名称 ndash;gt;--!-- property nameproperty valueshowStatus/--!-- lt;!ndash; javaType 用于指定生成的 domain 的属性类型使用类型的全限定名ndash;gt;--!-- property namejavaType valuejava.lang.Integer/--!-- lt;!ndash; jdbcType用于指定该列的JDBC类型--!-- property namejdbcType value/--!-- ndash;gt;--!-- /columnOverride--!-- /table--table tableNameuser_account enableCountByExamplefalse/table/context
/generatorConfiguration
每个标签的具体含义可以参考博客 Mybatis代码生成器Mybatis-Generator使用详解
三.运行
mvn mybatis-generator:generate四. mybatis-generator 自定义生成的文件名
在根据表名生成实体类时其类名和字段名默认使用驼峰命名比方说 CREATE_DATE生成的属性名字就是createDate 相关属性设置 property nameuseActualColumnNames valuefalse/
但是表名有时候比较长我们就想要自己定义生成的文件名。
一.domainObjectName和mapperName属性
比较简单的做法就是对每一个表名定义你想要的实体类名domainObjectName
比方说由于业务需要你定义了持久化类(Persistent Object)业务类(Business Object)在不同使用场景想在持久化类加上对应的后缀那么就可以使用domainObjectName table tableNamet_user_account enableCountByExamplefalse domainObjectNameUserAccountPo注意: domainObjectName 定义的名称会导致生成的Mapper类名和Example类名也发生变化上述例子中会变为UserAccountPoMapper和UserAccountPoExample
如果不想让Mapper类名发生变化, 可以指定下Mapper类名 table tableNamet_user_account enableCountByExamplefalse mapperNameUserAccountMapper domainObjectNameUserAccountPo此时生成的Mapper和Example类名为 UserAccountMapper和UserAccountPoExample
上述方式的缺点就是每个table都需要单独指定类名,比较繁琐
二.domainObjectRenamingRule标签
如果表名有统一的规则我们只是想将表名的固定前缀去除然后加上固定后缀就可以使用domainObjectRenamingRule标签
!-- 去除首字母t(之所以要忽略大小写是因为searchString是在table名称驼峰化之后修改的), 然后在剩余部分加上后缀Po--table tableNamet_user_account enableCountByExamplefalse domainObjectRenamingRule searchString(?i)^t(.*) replaceString$1Po//table那么如果该表没有需要去除的前缀呢? 可以这样写 table tableNameuser_account enableCountByExamplefalse domainObjectRenamingRule searchString(?i)^user(.*) replaceStringUser$1Po//table三. RenameExampleClassPlugin插件
RenameExampleClassPlugin可以修改example文件名
!--修改Example文件名--
plugin typeorg.mybatis.generator.plugins.RenameExampleClassPlugin property namesearchString valueExample$/ property namereplaceString valueCondition/
/plugin四. columnRenamingRule标签
columnRenamingRule 可以修改实体属性名其有两个属性: searchStringreplaceString。用法跟domainObjectRenamingRule类似。注意 : 该标签的searchString 是在没有驼峰化之前生效的