使用mybatis-generator

配置文件

  下面是配置文件示例:

<?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>

    <!-- 用于指定运行的对象环境(包括生成的代码分类)-->
    <context id="default" targetRuntime="MyBatis3" >

        <!-- 配置生成的注释
            suppressAllComments:是否禁用所有注释,默认为false
            suppressDate:生成的注释是否禁止显示生成代码的时间,默认为false
            dateFormat:生成的注释中,显示的日期的格式(会使用SimpleDateFormat进行格式化)
            addRemarkComments:生成的注释中,是否提示数据库表的字段名
         -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="false"/>
            <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
            <property name="addRemarkComments" value="false"/>
        </commentGenerator>

        <!-- 连接数据库的信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!-- 字段的类型解析配置,比如 -->
        <javaTypeResolver>
            <!-- forceBigDecimals为true,使用java.math.BigDecimal表示DECIMAL或者NUMERIC类型;为false,表示使用Integer替代 -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- javaModelGenerator用来对生成的模型类(实体类)进行配置:
            targetPackage指定生成的模型类保存的包名,targetProject指定保存的路径-->
        <javaModelGenerator targetPackage="cn.ganlixin.ssm.model.entity" targetProject="src/main/java">
            <!--是否允许子包-->
            <property name="enableSubPackages" value="true"/>
            <!--trimStrings表示是否对字符串进行trim(消除两边的空格)-->
            <property name="trimStrings" value="true"/>
            <!--immutable可以为true,设置生成的类为不可变类(不生成setter方法,所以不能改变),但是会生成包含所有字段的有参构造器-->
            <!-- <property name="immutable" value="false"/> -->

            <property name="constructorBased" value="true"/>
        </javaModelGenerator>

        <!-- 配置mapper文件的包名和路径名-->
        <!--        <sqlMapGenerator targetPackage="test.xml" targetProject="MBGTestProjectsrc">-->
        <!--            <property name="enableSubPackages" value="true"/>-->
        <!--        </sqlMapGenerator>-->

        <!-- 配置mapper接口详情的生成规则
               type:
                ANNOTATEDMAPPER:基于注解,且会生成包含SqlProviders接口,不会生成xml mapper
                MIXEDMAPPER:会生成既包含注解(sqlProvider)又包含xml的mapper
                XMLMAPPER:生成只基于xml的mapper接口
        -->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.ganlixin.ssm.dao.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- ,生成的类名,以及生成mapper中哪些接口
            table:指定使用的表名
            domainObjectName:生成的模型类(实体类)名称
            mapperName:生成的mapper接口类名
            sqlProviderName:如果使用注解方式,那么sqlProviderName可以指定sqlProvider接口类的名称
            接口配置(默认都是为false)
                enableInsert:是否生成insert接口
                enableSelectByPrimaryKey:是否生成selectByPrimaryKey接口
                enableSelectByExample:是否生成selectByExample接口
                enableUpdateByPrimaryKey:是否生成updateByPrimaryKey接口
                enableUpdateByExample:是否生成updateByExample接口
                enableDeleteByPrimaryKey:是否生成deleteByPrimaryKey接口
                enableDeleteByExample:是否生成deleteByExample接口
                enableCountByExample:是否生成countByExample接口
         -->
        <table
                tableName="book"
                domainObjectName="BookDO"
                enableInsert="true"
                enableSelectByPrimaryKey="false"
                enableSelectByExample="false"
                enableUpdateByPrimaryKey="false"
                enableUpdateByExample="false"
                enableDeleteByPrimaryKey="false"
                enableDeleteByExample="false"
                enableCountByExample="false"
        />
    </context>
</generatorConfiguration>

  

原文地址:https://www.cnblogs.com/-beyond/p/10546294.html