java-mybatis generator-生成 mysql 数据库访问文件

java-mybatis generator-生成 mysql 数据库访问文件


mybatis generator 版本:1.3.5


官网:http://www.mybatis.org/generator/
源码:https://github.com/mybatis/generator


修改后的代码下载地址:http://download.csdn.net/download/xxj_jing/10024312


一、配置文件,配置说明
其中:
table 标签下新增了三个属性
    entityName="{0}Model",表实体对象格式
    xmlName="{0}Mapper",xml文件名格式
    interName="I{0}Dao",接口文件名格式
    
commentGenerator 标签的type为自定义的类,实现了接口 org.mybatis.generator.api.CommentGenerator 



<?xml version="1.0" encoding="UTF-8"?>
<!--


       Copyright ${license.git.copyrightYears} the original author or authors.


       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at


          http://www.apache.org/licenses/LICENSE-2.0


       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.


-->
<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 该配置文件说明插件需要如何生成,以及生成对应的包名,路径等信息。
      还有重要的就是我们要生成的实体类所对应的的表或者试图 -->


<!--
注:该配置文件中有两个要注意的地方:
a、配置文件中的注释应该为:不能在注释里面再嵌套–之类的符号,比如:,否则运行时会提示报错信息。
b、标签里面元素是有顺序的,如果顺序乱了也会报错的,顺序依次为:
property——>plugin——>commentGenerator——>jdbcConnection——>
javaTypeResolver——>javaModelGenerator——>sqlMapGenerator——>
javaClientGenerator——>table+
c、对于generatorConfig.xml文件中引入的mybatis_generator.properties文件,里面主要是数据库连接信息和生成的文件的目录信息,我们可以在generatorConfig.xml的同目录下创建该文件
-->
<generatorConfiguration>
    <!--可以将数据先关内容放到配置文件中-->
    <properties resource="mybatis_generator.properties" />
    <context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional">
        <!-- 注意以下标签的顺序:property*,plugin*,commentGenerator?,jdbcConnection,
                javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,
                javaClientGenerator?,table+ -->


        <!-- property -->
        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
        一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖
     -->
        <property name="autoDelimitKeywords" value="false"/>
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>


        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <!-- property-end -->
        <!-- 生成的表实体对象重写了 ToString 方法
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> -->
        <!-- 生成的表实体对象增加了,equals和hashCode方法
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>-->
        <!--生成注解配置-->
        <commentGenerator type="MyCommentGenerator">
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
            <!-- 不希望生成的注释中包含时间戳 -->
            <property name="suppressDate" value="true" />
            <!-- 是否自动为每一个生成的类创建一个构造方法-->
            <property name="constructorBased" value="false"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>


        <!-- 数据库连接 -->
        <jdbcConnection
                driverClass="${jdbc_driver}"
                connectionURL="${jdbc_url}"
                userId="${jdbc_username}"
                password="${jdbc_password}">
            <!--获取表的表注释,需要设置该值为 true-->
            <property name="useInformationSchema" value="true"/>
        </jdbcConnection>
        <!-- java类型处理器
        用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl;
        注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型;-->
        <javaTypeResolver  type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
            <!--
            true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
            false:默认,
                scale>0;length>18:使用BigDecimal;
                scale=0;length[10,18]:使用Long;
                scale=0;length[5,9]:使用Integer;
                scale=0;length<5:使用Short;-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!--1.生成表名实体对象 -->
        <javaModelGenerator
                targetPackage="${modelPackage}"
                targetProject="${targetProject}">
            <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="true"/>
            <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>
        <!--2.生成 mapper.xml 文件  -->
        <sqlMapGenerator
                targetPackage="${sqlMapperPackage}"
                targetProject="${targetProject}" >
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--3.生成 dao.java 接口文件 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${daoMapperPackage}"
                             targetProject="${targetProject}" >
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--table 可选参数:
        tableName:数据库表名,支持sql模糊查询 % 占位符;如,txt_% 生成所有 txt_ 开头的表名
        1,schema:数据库的schema;
        2,catalog:数据库的catalog;
        3,alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName
        4,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;
        5,enableInsert(默认true):指定是否生成insert语句;
        6,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);
        7,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句;
        8,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);
        9,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);
        10,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句;
        11,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询);
        12,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性);
        13,modelType:参考context元素的defaultModelType,相当于覆盖;
        14,delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性)
        15,delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来。默认为false,delimitIdentifiers参考context的属性
        16,mapperName:1.3.5 新增
        注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写;
        自定义节点:支持{0}占位符操作,mapperName 属性将不起作用
               entityName="{0}Model",表实体对象格式
               xmlName="{0}Mapper",xml文件名格式
               interName="I{0}Dao",接口文件名格式
        -->
        <!--需要生成的表,tableName 支持%占位符-->
        <table tableName="xxx%"
            delimitAllColumns="fals"
            mapperName="{0}Xxx"
            entityName="{0}Model"
            xmlName="{0}Mapper"
            interName="I{0}Dao"
            enableInsert ="true"
            enableSelectByPrimaryKey ="true"
            enableUpdateByPrimaryKey ="true"
            enableDeleteByPrimaryKey ="true"
            selectByPrimaryKeyQueryId ="false"
            selectByExampleQueryId ="false"
            enableDeleteByExample ="false"
            enableCountByExample ="false"
            enableUpdateByExample ="false"
            enableSelectByExample ="false"/>
    </context>
</generatorConfiguration>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值