mybatis-generator 代碼自動生成插件

Hibernate 能夠選擇MyEclipse Datebase Explorer 或者是 Hibernate-tools 等工具來自動生成映射文件和實體類。html

mybatis 固然也要有!下面簡單介紹一個代碼自動生成插件:mybatis-generator。java

mybatis-generator有三種用法:命令行、eclipse插件、maven插件。mysql

做爲一個使用idea開發的程序猿來講勢必選擇maven插件了。其餘兩種方式就再也不此介紹了,由於我沒有用過啊~~~sql

僅僅須要簡單的三步就能夠啦,就是這麼簡單,趕忙輕鬆愉快的get~~mybatis

1.pom.xml中引入插件app

        <plugins>
            <!--mybatis自動生成代碼插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite> <!--容許覆蓋生成的文件-->
                    <verbose>true</verbose> <!--容許移動生成的文件-->
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.36</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

 

2.配置generatorConfig.xml文件dom

<?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" >
<!-- 配置生成器 -->
<!--具體參數參考:http://www.cnblogs.com/wql025/p/5037860.html-->
<generatorConfiguration>
    <context id="sango" targetRuntime="MyBatis3">
        <!--<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>-->

        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://10.***.***.***:3306/sango_db"
                        userId="root" password="123">
        </jdbcConnection>

        <!--實體,查詢模型生成-->
        <javaModelGenerator targetPackage="com.selicoco.sango.model.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成mapper.xml-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" >
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.selicoco.sango.model.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <table schema="public" tableName="user_info" domainObjectName="UserInfo"/>

    </context>
</generatorConfiguration>

3.開始使用eclipse

選中 mybatis-generator:mybatis-generate 雙擊就OK啦maven

 

 

生成後結果:ide

相關文章
相關標籤/搜索