Idea 創建分佈式 Maven 項目步驟 和 SSM基本配置

Idea (2017.2版) 創建分佈式 Maven 項目步驟 & SSM(SpringMVC + Spring + MyBatis) 基本配置;分佈式項目主要用於比較大型的、多人合作的項目,小項目可能就沒必要這樣了。

前言:CSDN上看到一篇高閱讀量的介紹Idea 分佈式項目創建步驟的博客,比較複雜,但自己實際操作發現並沒必要那麼複雜,可能是因爲Idea 的較新版本做了改進。

參考博客:https://blog.csdn.net/liudongdong0909/article/details/52270604

1)Maven 直接用的Idea 內置版本;創建空白工程ssm_distrituted。

(更新:有個更好的方式:直接把主工程作爲父工程,其它兩個子工程直接繼承父工程即可。)


2)File - New - Module,用Maven - archetype-quickstart 創建父工程模塊ssm_parent,用於管理公用Jar 包及其版本,只需要pom.xml 添加依賴即可;name 標籤前加:<packaging>pom</packaging>;更新pom 後,需要在模塊名上右鍵 -  Maven - Reimport,然後纔會生效。


3)File - New - Module,用Maven - archetype-webapp 創建子工程模塊ssm_web,注意要選父工程


4)File - New - Module,用Maven - archetype-webapp 創建子工程模塊ssm_service,這個子工程負責服務層和數據層(當然數據層也可以再分離出來),是作爲Jar 包給子工程ssm_web 用的,過程同第3步,只是還需要將packaging 改成jar,然後只需要把ssm_service 的依賴座標添加到ssm_web 的pom.xml 即可。配置正確的話,按住Ctrl,鼠標點擊依賴內容(如"com.qf") 是會跳轉到ssm_service 的pom 文件的。

        <dependency>
            <groupId>com.qf</groupId>
            <artifactId>ssm_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

  • 可用如下方法在ssm_web 模塊 手動添加模塊依賴:


  • 可在Maven 窗口手動創建(install) 模塊的jar 包


  • 還有種笨辦法:手動改ssm_service 的target 目錄到ssm_web 的target 目錄,可以運行。

5)配置Tomcat,配好後即可啓動,訪問靜態資源。

6)以下是SSM 主要配置,按框架分;SpringMVC 在ssm_web: 

  • 在web.xml 配置 編碼過濾器核心 DispatcherServlet 並指定配置文件位置
	<filter>
		<filter-name>encodingfilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingfilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置springmvc的核心servlet -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  • 在spring-mvc.xml 配置包掃描(子容器管理Controller Bean) & 註解驅動(開啓後會註冊常用的bean),靜態資源忽略(也可配置<mvc:resources>),視圖解析器(前綴/後綴)
	<!-- 掃描controller -->
	<context:component-scan base-package="com.qf.ssm.controller"/>

	<!-- 註解驅動 -->
	<mvc:annotation-driven/>
	
	<!-- 靜態servlet 處理靜態資源文件 -->
	<mvc:default-servlet-handler/>
	
	<!-- 配置視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前綴 -->
		<property name="prefix" value="/"/>
		<!-- 配置後綴 -->
		<property name="suffix" value=".jsp"/>
	</bean>

7)Spring 

  • 在web.xml 配置容器初始化監聽器classpath 後加* 表示除了掃描本工程的的classes 目錄,還會掃關聯Jar 工程的classes 目錄。
	<!-- 告訴spring容器,spring的配置文件的位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/applicationContext-*.xml</param-value>
	</context-param>
	
	<!-- 初始化spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  • 在service 層配置applicationContext.xml:包掃描(父容器管理除Controller 以外的Bean)、數據源sqlSessionFactory (用於整合MyBatis) & dao層自動創建代理(即可不用dao 實現類)
	<context:component-scan base-package="com.qf.ssm.service"/> 
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
		<property name="url" value="jdbc:mysql:///mydb"/>
	</bean>
	
    <!-- 掃描所有的dao層的接口,不需要指定 sqlSessionFactory -->
    <mybaits:scan base-package="com.qf.ssm.dao"/>

    <!--也可用比較長的寫法 -->
    <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qf.dao"/>
        <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"></property>
    </bean>-->
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!-- 數據源 -->
    	<property name="dataSource" ref="dataSource"/>
    	<!-- 映射文件的掃描 -->
    	<property name="mapperLocations" value="classpath*:com/qf/ssm/dao/mapper/*Mapper.xml"/>
    	<!-- 配置mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    </bean> 
    ***** 上面映射文件掃描的classpath 不加*的話,會報錯:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.qf.ssm.dao.IUserDao.queryByUserName,因爲找不到Mapper.xml 文件加* 表示也會掃描jar 包內的classspath。

8)MyBatis,在servcie (dao) 層

  • mybatis-config.xml 配置插件別名,這兩項也可放在applicationContext.xml 中配置。
	<!-- 掃描別名 -->
	<typeAliases>
		<package name="com.qf.ssm.entity"/>
	</typeAliases>
    <!-- 分頁插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
            <!--reasonable 默認false,設爲true 時,當pageNum(當前頁) 小於1時,會自動爲其賦值爲1。-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
  • 映射文件(也可把SQL 用註解寫在IDao 方法上):resultMapSQL
<mapper namespace="com.qf.ssm.dao.IUserDao">
	<select id="queryByUserName" resultType="user">
		select * from user where username = #{username}
	</select>
</mapper>

9)按文件分:

1) log4j
2) jdbc.properties
3) springmvc.xml
a) 視圖解析器
b) 靜態資源忽略
c) 包掃描自定義bean(controller),註解驅動
4) applicationContext.xml

a) 包掃描自定義bean(service,dao)
b) 數據源
c) 事務管理器
d) 事務策略
e) AOP的配置
f) Spring和MyBatis整合(sqlSessionFactoryBean)

5) mybatis-config.xml / mapper.xml
a) 插件; 別名
6) web.xml
a) Springmvc 編碼過濾器
b) Spring 監聽器 -初始化Spring 容器 ContextLoaderListener,需要context-param

c) SpringMVC核心組件 DispatcherServlet,需要init-param(同context-param 配的兩項一樣

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