使用myabtis連接PostgerSQL逆向生成代碼

        這裏使用的是maven插件的形式生成代碼,之前一直使用代碼生成一直錯誤,使用maven還比較簡單,這裏就介紹一個使用maven插件的形式逆向生成代碼。

        1.在pom文件中添加插件,如下代碼:

<build>
        <finalName>${web-app-name}</finalName>
        <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<includes>
						<include>**/SwaggerTest.java</include>
					</includes>
				</configuration>
			</plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/SwaggerTest.java</include>
                    </includes>
                    <systemPropertyVariables>
                        <io.springfox.staticdocs.outputDir>${project.build.directory}/swagger</io.springfox.staticdocs.outputDir>
                        <io.swagger.json.uris>/v2/api-docs?group=UI,/v2/api-docs?group=API</io.swagger.json.uris>
                        <io.swagger.json.output.name>acrc-ui-v1.json,acrc-api-v1.json</io.swagger.json.output.name>
                    </systemPropertyVariables>
                 </configuration>
            </plugin>


			<!--Mybatis代碼自動生成器-->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<dependencies>
					<dependency>
						<groupId> mysql</groupId>
						<artifactId> mysql-connector-java</artifactId>
						<version> 5.1.39</version>
					</dependency>
					<dependency>
						<groupId>org.mybatis.generator</groupId>
						<artifactId>mybatis-generator-core</artifactId>
						<version>1.3.5</version>
					</dependency>
					<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
					<dependency>
						<groupId>org.postgresql</groupId>
						<artifactId>postgresql</artifactId>
						<version>42.2.5</version>
					</dependency>


				</dependencies>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<phase>package</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<!--允許移動生成的文件 -->
					<verbose>true</verbose>
					<!-- 是否覆蓋 -->
					<overwrite>true</overwrite>
					<!-- 自動生成的配置,指定generator文件的地址-->
					<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
				</configuration>
			</plugin>

        </plugins>

       2.在src/main/resources目錄下面生成存放mybatis-generator.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>
    <!-- postgresql jar存放地址-->
    <classPathEntry
            location="D:\software\apache-maven-3.6.1\repo\org\postgresql\postgresql\42.1.4\postgresql-42.1.4.jar" />
    <context id="context1">
        <!--去掉註釋-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql://10.19.167.170:7017/ctm03acrc_ctm03acrcdb"
                        userId="ctm03acrc_ctm03acrcdb_user"
                        password="MWZwfTj8" />
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.hikvision.cms.acrc.core.biz.model"
                            targetProject="src/main/java" >
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.hikvision.cms.acrc.core.biz.dao"
                         targetProject="src/main/java" >
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator
                targetPackage="com.hikvision.cms.acrc.core.biz.dao"
                targetProject="src/main/java"
                type="XMLMAPPER" >
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成哪些表-->
        <table tableName="tb_remote_config"
               domainObjectName="RemoteConfig">
        </table>

    </context>
</generatorConfiguration>

        3.在IDEA中找到maven插件管理,雙擊mybatis插件即可:

發佈了221 篇原創文章 · 獲贊 30 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章