使用IDEA搭建ssm框架

使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架css

開源git源碼地址:前端

https://gitee.com/kaisheng100/ssmdemo.git

Spring

Spring框架是整個系統架構的核心,將前端請求數據的處理以及數據庫的數據操做整合在一塊兒,很是重要。java

SpringMVC

SpringMVC框架用於處理系統中數據的流轉及控制操做。 (從哪裏來,到哪裏去。多麼有哲理的一句話。)git

MyBatis

Mybatis框架主要處理業務和數據庫之間的數據交互,因此建立對象和管理對象生命週期的職責能夠委託Spring框架完成。web

1、建立項目

進入IDEA界面,點擊File >>> New >>>Project 按照以下圖示操做便可spring

在Maven選項卡里面找到對應的java web選項,而後咱們點下一步

這一步填入組織等信息,這裏比較隨意,按照本身的需求進行填寫 sql

這裏我早已配置好本地Maven倉庫,所以直接默認便可。若是沒進行配置本地默認倉庫的話,請網上查找對應的資料進行配置

輸入Project name,和須要保存的路徑,而後finish
稍等片刻,idea自動建立須要點時間。

注意這裏修改下 數據庫

建立目錄結構不在敘述,建立結果以下圖所示,pom依賴配置參照git源碼 spring-mvc

配到這裏,項目建立部分已經完成!!!

集成Spring框架

Spring框架是整個系統架構的核心,將前端請求數據的處理以及數據庫的數據操做整合在一塊兒,很是重要。tomcat

在web.xml配置以下

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring核心配置文件 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

<!-- 處理POST提交亂碼問題 -->
<filter>
	<filter-name>encoding</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>encoding</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 指定配置文件位置和名稱 若是不設置,默認找/WEB-INF/<servlet-name>-servlet.xml -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/springmvc.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>
複製代碼

ContextLoaderListener 全部監聽器主要用於監聽對象或服務器狀態的變化,側重於變化

在tomcat服務器啓動的時候,監聽到後,按照上下文參數的配置初始化spring容器

param-name中的contextConfigLocation不可更改,spring框架會根據這個id找到配置文件,一旦更改容器沒法初始化

applicationContext.xml配置的即爲spring的初始化策略 SpringMVC環境構建時須要讀取servlet初始化參數init-param, 從classpath中讀取配置文件spring/springmvc.xml

spring容器 和 springmvc容器初始化完成會將 spring容器設置爲springmvc容器的父容器

在applicationContext.xml配置以下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context" 
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:aop="http://www.springframework.org/schema/aop" 
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <!-- 加載配置文件 -->
   <context:property-placeholder location="classpath:conf/db.properties" />
   
   <!--druid鏈接池  -->
   	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${druid.driver}" />
		<property name="url" 			 value="${druid.url}" />
		<property name="username" 	     value="${druid.username}" />
		<property name="password" 		 value="${druid.password}" />
	</bean>
  
	<!-- 配置Mybatis工廠 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	
	<!-- Mapper動態代理開發  掃包  給定包下的接口文件名和映射文件名必須相同  建立接口的實現類-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xiao.mapper" />
	</bean>
	
	<!-- 開啓事物 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 事物註解驅動-->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
</beans>
複製代碼

在springmvc.xml中配置以下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 
 	<!-- 配置掃描註解  @Controller @Service -->
 	<context:component-scan base-package="com.xiao" />
 
 	<!-- SpringMVC使用<mvc:annotation-driven>自動加載RequestMappingHandlerMapping和RequestMappingHandlerAdapter -->
 	<mvc:annotation-driven />
 
 	<!-- 配置靜態資源映射 -->
	<mvc:resources location="/js/" mapping="/js/**"/>
	<mvc:resources location="/css/" mapping="/css/**"/>
 
 	<!-- 配置視圖解析器 -->
 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<!-- 配置邏輯視圖的前綴 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 配置邏輯視圖的後綴 -->
		<property name="suffix" value=".jsp" />
		
 	</bean>
 	
 	<!-- 定義文件上傳解析器 -->
	<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		設定默認編碼
		<property name="defaultEncoding" value="UTF-8"></property>
		設定文件上傳的最大值5MB,5*1024*1024
		<property name="maxUploadSize" value="5242880"></property>
	</bean> -->
 
  
</beans>
複製代碼
相關文章
相關標籤/搜索