SSM框架經過mybatis-generator自動生成代碼

1、首先eclipse配置好maven環境,而且建立好一個SSM框架的工程java

2、在pom.xml中添加pluginmysql

    <build>
        <finalName>ssm_web</finalName>
        <pluginManagement> 
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.40</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        </pluginManagement> 
    </build>

注意:web

一、pom中添加插件要在<build></build>標籤裏面,而且在這裏指定數據庫驅動,那麼在下一步配置generatorConfig.xml的時候就不用在指定數據庫驅動的本地路徑;sql

二、src/main/resources/mybatis-generator/generatorConfig.xml指定的是generatorConfig.xml配置文件的路徑,你們能夠根據本身的實際狀況調整;數據庫

3、generatorConfig.xml配置文件mybatis

<?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="E:/maven/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"/> --> <context id="my" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/crm" userId="root" password="root" /> <javaModelGenerator targetPackage="com.xdw.model" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.xdw.mapping" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.xdw.dao" targetProject="F:/javawebworkspace/ssm_web/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="house_type" domainObjectName="HouseType"> <!-- <property name="useActualColumnNames" value="true"/> --> </table> </context> </generatorConfiguration>

配置講解:app

一、classPathEntry location在這裏能夠不用配置,由於以前pom中已經配置了數據庫驅動;框架

二、jdbcConnection按照本身的數據庫配置相應的驅動類,URL,用戶名和密碼;dom

三、javaModelGenerator,sqlMapGenerator,javaClientGenerator配置相應要生成的pojo類,DAO類和mapper對應的xml文件,targetPackage屬性指定包名,targetProject指定本身工程的路徑,eclipse

四、table標籤配置數據庫表和實體類的映射,tableName屬性指定表名,domainObjectName指定生成的pojo類名;<property name="useActualColumnNames" value="true"/>若是配置的話,那麼生成的實體類的屬性名稱就和數據庫

  表的每一個字段的名稱同樣,若是不配置,會將數據表裏面的字段名稱經過_鏈接的字段自動生成經典的駝峯表示法,好比我這裏有個字段名爲type_id,生成的屬性名稱爲typeId;

   建議你們在數據庫建表的時候採用_將單詞分隔;

 

4、建立好以後點擊maven build

 

彈出

在goals中填入mybatis-generator:generate,而後點擊Run

也能夠用maven命令行輸入mvn mybatis-generator:generate

 

5、執行結果以下:

相關文章
相關標籤/搜索