mybatis generator

  1. 引入maven依赖
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.myutil.test</groupId>
        <artifactId>MyUtils</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.9</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.16</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>
    
    
        <build>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/java/</directory>
                </resource>
            </resources>
    
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.4.2</version>
                    <configuration>
                        <argLine>-Dfile.encoding=UTF-8</argLine>
                        <skipTests>true</skipTests>
                        <excludes>
                        </excludes>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
    
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <skip>false</skip>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

  2. generatorConfig.xml
    <?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>
    
        <!-- Class Driver Path -->
        <classPathEntry location="C:\Users\Administrator.USER-20170829XY\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
    
        <context id="context" targetRuntime="MyBatis3">
    
            <!-- 生成的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 name="autoDelimitKeywords" value="true"/>
    
            <!--使生成的Model实现Serializable接口-->
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
    
            <commentGenerator>
                <!-- This property is used to specify whether MBG will include any coments in the generated code -->
                <property name="suppressAllComments" value="false"/>
                <!-- This property is used to specify whether MBG will include the generation timestamp in the generated comments -->
                <property name="suppressDate" value="true"/>
                <!--model类加上数据库字段的注释-->
                <property name="addRemarkComments" value="true"/>
            </commentGenerator>
    
            <!--这个是数据库的连接url和用户名密码-->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://xxx.xxx.xxxx:3306/sp2p?characterEncoding=UTF-8"
                            userId="xxx" password="xxx"/>
            <javaTypeResolver>
                <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal
          for DECIMAL and NUMERIC fields, rather than substituting integral types when possible -->
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <!--只需要修改 targetPackage java model定义的包名儿 -->
            <javaModelGenerator targetPackage="com.myutil.mybatis.model" targetProject="src/main/java">
                <!-- This property is used to select whether MyBatis Generator will generate different Java packages for
          the objects based on the catalog and schema of the introspected table -->
                <property name="enableSubPackages" value="false"/>
                <!-- This property is used to select whether MyBatis Generator adds code to trim the white space from character fields returned from the database -->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
    
            <!--只需要修改 targetPackage java sqlMap定义的包名儿 -->
            <sqlMapGenerator targetPackage="com.myutil.mybatis.mapper.xml" targetProject="src/main/java">
                <!-- This property is used to select whether MyBatis Generator will generate different Java packages for
          the objects based on the catalog and schema of the introspected table -->
                <property name="enableSubPackages" value="false"/>
            </sqlMapGenerator>
    
            <!--只需要修改 targetPackage 这个就是那个interface 包名儿  -->
            <!-- type字段是 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                   type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                   type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                   type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
           -->
            <javaClientGenerator targetPackage="com.myutil.mybatis.mapper" targetProject="src/main/java"
                                 type="MIXEDMAPPER">
                <!-- This property is used to select whether MyBatis Generator will generate different Java packages for
          the objects based on the catalog and schema of the introspected table -->
                <property name="enableSubPackages" value="false"/>
            </javaClientGenerator>
    
            <!--具体的表 domainObjectName 是你自己定义的文件名-->
             <table tableName="t_channel"
                   domainObjectName="Channel"
                   enableCountByExample="false"
                   enableDeleteByPrimaryKey="false"
                   enableDeleteByExample="false"
                   enableSelectByExample="false"
                   enableUpdateByExample="false"
                   modelType="flat">
                <property name="useActualColumnNames" value="true"/>
                <property name="useCompoundPropertyNames" value="true"/>
            </table>
    
        </context>
    </generatorConfiguration>
    

  3. 运行
       
       

之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate  -e

这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。


mybatis也能方向生成代码,能方向生成实体类(po)、mapper接口和Mapper接口映射文件,能减少我们代码的工作量。详细步骤如下 1、下载mybatis生成架包工具MyBatis_Generator_1.3.1.zip,解压架包把features、plugins文件夹下的架包分别拷贝到eclipse安装目录下的features、plugins文件夹。重启eclipse就行。 解压后图片如下: Eclipse路径如图: 拷贝替换如图: 2、创建generatorConfig.xml文件,安装好mybatis 就能创建generatorConfig.xml 3、配置generatorConfig.xml配置文件,详细如下 [html] view plain copy <?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> <!-- <classPathEntry location="D:\\rep\\mysql\\mysql-connector-java\\5.1.19\\mysql-connector-java-5.1.19.jar" /> --> <classPathEntry location="D:\\repo\\com\\oracle\\ojdbc14\\10.2.0.1.0\\ojdbc14-10.2.0.1.0.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:orcl4" userId="xxx" password="xxxx" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> </javaTypeResolver> <javaModelGenerator targetPackage="com.pcmall.domain.sale.order" targetProject="pos-service/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis.mapper.sale.order" targetProject="pos-service/src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.pcmall.dao.sale.order" targetProject="pos-service/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="hs_zxzflx" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" selectByExampleQueryId="true" enableUpdateByExample="false"> <!-- <generatedKey column="ID" sqlStatement="oracle" identity="true" /> --> </table> </context> </generatorConfiguration> 4、右击generatorConfig.xml 点击Generate MyBatis/iBATIS Artifacts 生成对应接口、接口映射文件、实体类 5、查看结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值