java自动生成数据表相关代码

将数据表生成 mybatis 相关的实体类、接口和mapper文件

新建一个maven项目

在pom.xml文件中加入mysql驱动

    <dependencies>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.49version>
        dependency>
    dependencies>

在pom.xml文件中加入mybatis代码自动生成插件

<build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.6version>
                <configuration>
                    
                    <configurationFile>GeneratorMapper.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <dependencies>
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.49version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

插件中用到了GeneratorMapper.xml文件,在项目的根目录下创建GeneratorMapper.xml,其内容格式如下:


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="tables" targetRuntime="MyBatis3">

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

        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ylb"
                        userId="root"
                        password="123456">
        jdbcConnection>

        
        <javaModelGenerator targetPackage="org.example.api.model"
                            targetProject="D:\javaProject\ylb\mirco-parent\src\main\java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mappers" targetProject="D:\javaProject\ylb\mirco-parent\src\main\resources">
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="org.example.dataservice.mapper" targetProject="D:\javaProject\ylb\mirco-parent\src\main\java">
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>

        
        <table tableName="b_product_info" domainObjectName="ProductInfo"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

       

        <table tableName="u_user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    context>

generatorConfiguration>

代码生成

配置好相应的数据库表等信息后,通过运行插件执行代码生成。

java自动生成数据表相关代码_第1张图片
点击mybatis-generator:generate执行插件。

你可能感兴趣的:(Java,java,开发语言)