SSM開發環境的搭建(方式二)

liddhome發表於2017-01-10

1.導包。

2.配置web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSMenvironment2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- 載入spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- springmvc前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation配置springmvc載入的配置檔案(配置處理器對映器、介面卡等等) 如果不配置contextConfigLocation,預設載入的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-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>
		<!-- 第一種:*.do,訪問以.do結尾 由DispatcherServlet進行解析 
			第二種:/,所以訪問的地址都由DispatcherServlet進行解析,對於靜態檔案的解析需要配置不讓DispatcherServlet進行解析 
			使用此種方式可以實現 RESTful風格的url 
			第三種:/*,這樣配置不對,使用這種配置,最終要轉發到一個jsp頁面時, 仍然會由DispatcherServlet解析jsp地址,不能根據jsp頁面找到handler,會報錯。 -->
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
  
  <!-- post亂碼過慮器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
</web-app>

3.在src下建立包。

說明:bean包放javabean類,exception包自定義exception類,interceptor包放攔截器類(如果需要的話),mapper包放XxxMapper.java類和XxxMapper.xml配置檔案(記住這兩個檔案的名字一定要相同),service包放Service類,web包放Controller類,test包放test類(若需要的話),config包放spring和mybatis的配置檔案。

4.config中配置檔案的編寫。

config包中的結構(不一定非要一樣)。

sqlMapConfig.xml(mybatis的配置檔案)。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
	<!-- 全域性setting配置,根據需要新增 -->
	<!-- 全域性配置引數,需要時再設定 -->
	<settings>
		<!-- 開啟延遲載入 的開關 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 將積極載入改為消極載入即按需要載入 -->
		<setting name="aggressiveLazyLoading" value="false"/>
		<!-- 開啟二級快取 -->
		<setting name="cacheEnabled" value="true"/>
	</settings>
	<!-- 配置別名 -->
	<typeAliases>
		<!-- 批量掃描別名 -->
		<package name="com.tyust.bean"/>
	</typeAliases>
	<!-- 配置mapper
	由於使用spring和mybatis的整合包進行mapper掃描,這裡不需要配置了。
	必須遵循:mapper.xml和mapper.java檔案同名且在一個目錄 
	 -->

	<!-- <mappers>
	
	</mappers> -->
</configuration>

springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 可以掃描controller、service、...
	這裡讓掃描controller,指定controller的包
	 -->
	<context:component-scan base-package="com.tyust.web"></context:component-scan>
	
	<!-- 靜態資源解析
	包括 :js、css、img、..
	 -->
	 
	<!--  <mvc:resources location="/js/" mapping="/js/**"/>
	 <mvc:resources location="/img/" mapping="/img/**"/>
 -->	
		
	<!--註解對映器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
	<!--註解介面卡 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
	
	<!-- 使用 mvc:annotation-driven代替上邊註解對映器和註解介面卡配置
	mvc:annotation-driven預設載入很多的引數繫結方法,
	比如json轉換解析器就預設載入了,如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
	實際開發時使用mvc:annotation-driven
	 -->
	<!-- <mvc:annotation-driven conversion-service="conversionService"
	validator="validator"/> -->
	

	<!-- 檢視解析器
	解析jsp解析,預設使用jstl標籤,classpath下的得有jstl的包
	 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路徑的字首,如下路徑是把jsp檔案放到WEB-INF下,若不在那裡邊就不用寫WEB-INF了 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<!-- 配置jsp路徑的字尾 -->
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 自定義引數繫結 -->
	<!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		轉換器
		<property name="converters">
			<list>
				日期型別轉換
				<bean class="com.tyust.web.converter.CustomDateConverter"/>
			</list>
		</property>
		
	
	</bean> -->
	
	<!-- 校驗器 -->
	<!-- <bean id="validator"
		class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		hibernate校驗器
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
		指定校驗使用的資原始檔,在檔案中配置校驗錯誤資訊,如果不指定則預設使用classpath下的ValidationMessages.properties
		<property name="validationMessageSource" ref="messageSource" />
	</bean> -->
<!-- 校驗錯誤資訊配置檔案 -->
	<!-- <bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		資原始檔名
		<property name="basenames">   
       	 <list>    
            <value>classpath:CustomValidationMessages</value> 
       	 </list>   
    	</property>
		資原始檔編碼格式
		<property name="fileEncodings" value="utf-8" />
		對資原始檔內容快取時間,單位秒
		<property name="cacheSeconds" value="120" />
	</bean> -->
	<!-- 全域性異常處理器
	只要實現HandlerExceptionResolver介面就是全域性異常處理器
	 -->
	<!-- <bean class="com.tyust.exception.ExceptionResolver"></bean> -->
	
	
	<!-- 檔案上傳 -->
	<!-- <bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		設定上傳檔案的最大尺寸為5MB
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean> -->
	
	<!--攔截器 -->
     <!-- <mvc:interceptors>
	多個攔截器,順序執行
	登陸認證攔截器
	<mvc:interceptor>
		<mvc:mapping path="/**"/>
		<bean class=""></bean>
	</mvc:interceptor>
	<mvc:interceptor>
		 /**表示所有url包括子url路徑
		<mvc:mapping path="/**"/>
		<bean class=""></bean>
	</mvc:interceptor>
	<mvc:interceptor>
		<mvc:mapping path="/**"/>
		<bean class=""></bean>
	</mvc:interceptor>
     </mvc:interceptors> -->
	
	
	
</beans>

applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 載入db.properties檔案中的內容,db.properties檔案中key命名要有一定的特殊規則 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置資料來源 ,dbcp -->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="#{jdbc.driver}" />
		<property name="url" value="#{jdbc.url}" />
		<property name="username" value="#{jdbc.username}" />
		<property name="password" value="#{jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 資料庫連線池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 載入mybatis的全域性配置檔案 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
	</bean>
	<!-- mapper掃描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 掃描包路徑,如果需要掃描多個包,中間使用半形逗號隔開 -->
		<property name="basePackage" value="com.tyust.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>


</beans>

applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<context:component-scan base-package="com.tyust.service"/>
</beans>

applicationContext.transcation.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 事務管理器 
	對mybatis運算元據庫事務控制,spring使用jdbc的事務控制類
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 資料來源
	dataSource在applicationContext-dao.xml中配置了
	 -->
	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<!-- 傳播行為 -->
		<tx:method name="save*" propagation="REQUIRED"/>
		<tx:method name="delete*" propagation="REQUIRED"/>
		<tx:method name="insert*" propagation="REQUIRED"/>
		<tx:method name="update*" propagation="REQUIRED"/>
		<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
	</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
	<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tyust.service.impl.*.*(..))"/>
</aop:config>

</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis
jdbc.username=root
jdbc.password=root

log4j.properties

# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5.測試mybatis連線資料庫是否成功。

6.開發。

相關文章