【SQshop 搭建的第一天】Idea maven 聚合項目初體驗 && mybatis逆向工程

   ----2018/7/13  

前幾天去面試了,結果不太理想。(面試官問的都能答得上來,那邊也是比較滿意的。結果。。。 TM是培訓機構。。不過想想現在還是大二,明年大三還有課,可能找工作還是太早了。)所以打算照着寫個企業級的電商項目。。  等以後去面試,也有個拿得出的項目,也習慣一下企業項目的開發模式。話不多說了。開始。。。。


一  搭建idea maven聚合項目

①瞭解一般聚合項目的結構

傳統的工程結構:


Maven 聚合項目結構:

parent: jar包集中管理

common:導入工具類的jar包,繼承parent

manage:工程包,繼承parent

mapper: 導入mybatis,與數據庫有關的jar包

service:導入spring相關的jar包

controller繼承service,service繼承mapper,mapper繼承pojo

注意:controller這個module必須指定打包格式爲war包

<packaging>war</packaging>

如下圖:

這裏就不寫具體的步驟了。                                                   ----圖片引用傳智的淘淘商城案例


parent下的pom.xml(需要導入的jar包就不放出來了。這裏就寫下插件的配置)

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <!-- 資源文件拷貝插件 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
                <!--配置逆向mybatis工程-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

二  idea下mybatis逆向工程實現

generatorSqlMapCustom項目github地址:

https://github.com/xuliugen/generatorSqlmapCustom.git


ps:去github搜索generator可以搜出的

逆向工程:就是通過已有,建好的數據庫,去自動創建mapper ,java對象

這裏有個意見:就是逆向工程的創建,還是重新創建個空項目,在空項目中實現。   這樣就不會把項目搞的超級亂。。。

1.創建java項目

2.導入許需要的jar包


3.引用generatorConfig.xml配置文件(github項目中有)

<?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>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/sqshop" userId="root"
			password="197219">
		</jdbcConnection>
		<!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和 
			NUMERIC 類型解析爲java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO類的位置 -->
		<javaModelGenerator targetPackage="cn.sleepq123.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
			<!-- 從數據庫返回的值被清理前後的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="cn.sleepq123.mapper"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.sleepq123.mapper"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定數據庫表 -->
		<table schema="" tableName="user"></table>
		<table schema="" tableName="admin"></table>
		<table schema="" tableName="product"></table>
		<table schema="" tableName="shopcart"></table>
		<table schema="" tableName="words"></table>


	</context>
</generatorConfiguration>

4.導入src下的GeneratorSqlMap.java.   (無需修改,前面配置文件配置過的話,現在直接運行就OK)

5.結果(自動生成了這麼多文件,舒服呀)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章