spring3mvc+mybatis 環境搭建

1.配置applicationContext配置文件

<?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:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        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.1.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.1.xsd
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" 
                default-autowire="byName"> 
    
    <!-- 掃描 com.eightspace.baccy包下的帶註解的組件-->
    <context:component-scan base-package="app/manage"/>
    
    <!-- 配置數據源,jdbc.pool數據庫連接池 -->
    <bean id="abstractDataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="false" />
        <property name="validationQuery" value="SELECT 1" />
        <property name="validationInterval" value="30000" />
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <property name="maxActive" value="50" />
        <property name="minIdle" value="10" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="3000" />
        <property name="initialSize" value="5" />
        <property name="removeAbandonedTimeout" value="600" />
        <property name="removeAbandoned" value="false" />
        <property name="logAbandoned" value="true" />
        <property name="minEvictableIdleTimeMillis" value="30000" />
        <property name="jmxEnabled" value="true" />
        <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" /> 
         <property name="url" value="jdbc:postgresql://127.0.0.1/database" />       
      <property name="username" value="database" />
        <property name="password" value="123456" />
    </bean>

    <!-- Spring整合Mybatis:創建 sqlSessionFactory-->
    <bean id="sqlSessionFactory" 
      class="org.mybatis.spring.SqlSessionFactoryBean"
      p:dataSource-ref="abstractDataSource"
      p:configLocation="classpath:SqlMapConfig.xml"
      p:mapperLocations="classpath:app/db/sql/*.xml"/>

    <!-- 定義Mybatis模板 -->
    <bean id="sqlMapClientTemplate" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg ref="sqlSessionFactory"/>
    </bean> 
     
    <!-- 開啓事務(基於註解) -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="abstractDataSource"/>
    </bean>
    
    <!--spring容器啓動時啓動類 -->  
    <bean id="setup" class="com.eightspace.baccy.apps.Setup" init-method="init" destroy-method="destroy" />
    
 
</beans>
 
2.配置dispatcher-servlet.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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

	<context:component-scan base-package="app/manage">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
	</context:component-scan>
         <span style="color: rgb(51, 51, 51); font-family: Tahoma, 'Microsoft Yahei'; font-size: 14px; line-height: 26px;"><!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->   </span>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/page/" p:suffix=".jsp" /> <!-- 視圖文件的前綴和後綴 -->   
 <!-- view是用什麼顯示,這裏是jsp,還可以用velocity之類的 -->  
           <!--支持頁面上傳,可添加屬性  maxUploadSize 限制大小 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="UTF-8" />
             <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 --> 
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
           <!-- 用於將對象轉換爲 JSON--> 
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes" value="application/json" />
	</bean>
 
</beans>                

3.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>projectName</display-name>
 <welcome-file-list>
   <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
 <!-- context-param <span style="font-family: Arial; font-size: 14px; line-height: 26px;">該Listener指定要加載的xml</span>-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
  	
	<!-- listener -->
    <listener>
   		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	
	<!-- servlet配置 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>	
	
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<!-- 過濾器配置 -->
	
	<!-- 編碼utf-8 -->
	<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>
    	<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	
	<!-- 頁面cookie驗證 -->
	<filter>
		<filter-name>pageFilter</filter-name>
		<filter-class>app.core.PageFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>pageFilter</filter-name>
		<url-pattern>/page/*</url-pattern>
	</filter-mapping>
</web-app>

4.添加SqlMapConfig.xml文件

<?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>
	<settings>
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>
</configuration>

5.添加需要jar包

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