mybatis.generator自动生成代码使用介绍(maven插件)

1.首先在项目的pom文件中加入如下的代码片段,进行插件配置

 <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
                    
                    <configurationFile>src\main\resources\mybatis-generator\generatorConfig.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>${mysql-connector}version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

2.第二部分配置generatorConfig.xml文件



<generatorConfiguration>
    <properties resource="mybatis-generator/generator.properties"/>
    <context id="my" targetRuntime="MyBatis3" defaultModelType="flat">

        
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>


        
        <commentGenerator>
            <property name="suppressAllComments" value="false" />
        commentGenerator>

        
        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.connectionURL}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        jdbcConnection>

        
        <javaModelGenerator targetPackage="${model.TargetPackage}" targetProject="${model.TargetProject}">
            
            <property name="enableSubPackages" value="true" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="${mapper.XMLTargetPackage}" targetProject="${mapper.XmlTargetProject}">
            
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="${dao.TargetPackage}" targetProject="${dao.TargetProject}">
            
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>

        
        
        <table schema="${dbName}" tableName="${tableName}"
               domainObjectName="${domainObjectName}" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        table>
    context>
generatorConfiguration>

3.其中xml文件中的可以引用配置文件,这里我们引用配置文件generator.properties

#数据库连接信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=###
jdbc.user=###
jdbc.password=###

#实体类位置
model.TargetPackage=###
model.TargetProject=src/main/java

#mapper.xml文件位置
mapper.XMLTargetPackage=mapper
mapper.XmlTargetProject=src/main/resources

#dao文件位置
dao.TargetPackage=###
dao.TargetProject=src/main/java

#数据库名
dbName=###

#需要生产的表名
tableName=###

#指定生成实体类名
domainObjectName=###

配置完之后在项目中就可以运用maven插件生成map.xml,实体类,dao类,可以很大的提高开发效率。

你可能感兴趣的:(java小记)