如何做漫画赚钱的网站,响应式网页设计用什么软件,vue 做的网站,软件开发税率是13%还是6MyBatis Generator(MBG)#xff0c;这是官方帮我们提供的一个自动生成代码的工具#xff0c;前面的课程中#xff0c;我们都是脑袋里想好#xff0c;pojo有哪些属性#xff0c;属性的类型是什么#xff0c;对应的数据表中的字段名字是什么#xff0c;匹配的类型是什么..…MyBatis Generator(MBG)这是官方帮我们提供的一个自动生成代码的工具前面的课程中我们都是脑袋里想好pojo有哪些属性属性的类型是什么对应的数据表中的字段名字是什么匹配的类型是什么.....然后还要写接口xxxDao以及它的实现配置文件xxxDao.xml等等都是手动自己操作以前我们学习Hibernate的时候感觉方便就是写好pojo启动服务器Hibernate会自动帮助我们生成对应的数据表MyBatis也有类似的工具MBG就是官方给我提供的这样的工具但它和Hibernate有点不一样就是Hibernate帮我们生成表MBG帮我们根据表生成接口、pojo类和xml这些文件方向是反的。
要使用MBG首先要导jar包和建立一个XML配置文件
dependencygroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-core/artifactIdversion1.3.7/version
/dependency
以下元素就是MBG的最小配置
元素指定如何连接数据库
元素指定生成Model的目标package与目标project
元素指定生成Mapping XML文件的目标package与目标project
(Optionally)元素指定生成Mapper(即DAO)文件的目标package与目标project, 如果不指定这个元素就不会生成Mapper文件至少一个table元素。
下面是一个较为完整的示例, 可以保存下来按需修改
?xml version1.0 encodingUTF-8?
!DOCTYPE generatorConfigurationPUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtdgeneratorConfiguration!-- 数据库的驱动, JAR/ZIP文件的全路径maven工程驱动已经依赖了没用--classPathEntry location/Program Files/IBM/SQLLIB/java/db2java.zip/!--targetRuntime用MyBatis3, 也就是默认的, 其他我基本不会用-context idDB2Tables targetRuntimeMyBatis3commentGenerator!-- 去除自动生成的注释 --property namesuppressAllComments valuetrue//commentGenerator!--基础的数据库连接--jdbcConnection driverClassCOM.ibm.db2.jdbc.app.DB2DriverconnectionURLjdbc:db2:TESTuserIddb2adminpassworddb2admin/jdbcConnection!--Java类型解析器, 目前也就只有forceBigDecimals可以给你玩--javaTypeResolver!--当数据类型为DECIMAL或者NUMERIC的时候, 如果是true的话则总是使用java.math.BigDecimal--!--以下是false, 即默认值的情况--!--如果有小数或者decimal长度大于18, Java类型为BigDecimal--!--如果没有小数, 以及decimal长度为10至18, Java类型为Long--!--如果没有小数, 以及decimal长度为5至9, Java类型为Integer--!--如果没有小数, 以及decimal长度少于5, Java类型为Short--property nameforceBigDecimals valuefalse//javaTypeResolver!--Domain生成器--javaModelGenerator targetPackagetest.model targetProject.\src\main\java!--据说可以自动添加schema名, 可是我没用到过--property nameenableSubPackages valuetrue/!--生成全属性构造器, 没什么用, 如果有指定immutable元素的话这个会被忽略--property nameconstructorBased valuetrue/!--生成不可变的domain, 这个我也很少用--property nameimmutable valuetrue/!--每个Domain都继承这个bean--property namerootClass valuecom.github.prontera.domain.base.BasicEntity/!--当遇到String的时候setter是否会先trim()--property nametrimStrings valuetrue//javaModelGenerator!--Mapping生成器--sqlMapGenerator targetPackagetest.xml targetProject.\src\main\javaproperty nameenableSubPackages valuetrue//sqlMapGenerator!--Mapper生成器, 当type为ANNOTATEDMAPPER时是带有annotation的Mapper, MIXEDMAPPER是XML文件--javaClientGenerator typeXMLMAPPER targetPackagetest.dao targetProject.\src\main\javaproperty nameenableSubPackages valuetrue/!--每个Mapper所继承的接口--property namerootInterface valuecom.github.prontera.Mapper//javaClientGenerator!--字段命名策略过程: columnRenamingRule property nameuseActualColumnNames--!--alias属性是个神器, 会为所有SQL都添加, 做关联的时候就非常方便了--!--至于什么Example, 全关了就是--table aliasha tableNameALLTYPES domainObjectNameCustomerenableCountByExamplefalse enableUpdateByExamplefalseenableDeleteByExamplefalse enableSelectByExamplefalseselectByExampleQueryIdfalse!--指定是否用数据库中真实的字段名, 而不是采用MBG转换后的驼峰--property nameuseActualColumnNames valuetrue/!--自动集成改类--property namerootClass valuecom.github.prontera.domain.base.HelloBasicClass/!--Mapper自动继承的接口--property namerootInterface valuecom.github.prontera.Mapper/!--当遇到String的时候setter是否会先trim()--property nametrimStrings valuetrue/!--先进行columnRenamingRule, 再进行useActualColumnNames. 如果有columnOverride则忽略该配置--!--关于columnRenamingRule的具体例子 http://www.mybatis.org/generator/configreference/columnRenamingRule.html--columnRenamingRule searchString^CUST_ replaceString/!--顾名思义, 忽略某些列--ignoreColumn columnCREATE_TIME/!--也是忽略数据列, 但是可以通过正则表达式, except子元素是可选的, 代表忽略除UPDATE_TIME外的列--ignoreColumnsByRegex pattern.*_TIME$except columnUPDATE_TIME//ignoreColumnsByRegex/table/context
/generatorConfigurationJava的方法运行插件 ListString warnings new ArrayListString();boolean overwrite true;File configFile new File(generatorConfig.xml);ConfigurationParser cp new ConfigurationParser(warnings);Configuration config cp.parseConfiguration(configFile);DefaultShellCallback callback new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);