SSM框架spring,pom,web的xml配置文件模板

SSM框架的spring配置文件一般分为三个(个人习惯):spring-dao.xml,spring-service.xml,spring-mvc.xml。基于maven的web项目中还有pom.xml,web.xml配置文件。特此整理了以上配置文件的常用模板。

1-1 spring-dao.xml




 		
	<context:property-placeholder location="classpath:[这里写dataSource.properties的路径]"/>
    
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
		<property name="dataSource" ref="dataSource"/>
		
		<property name="mapperLocations" value="classpath:这里写XXmapper.xml所在的文件夹路径"/>
        
		<property name="typeAliasesPackage" value="[这里写pojo类所在的包]"/>
		
		<property name="plugins">
			<list>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<props>
							<prop key="helperDialect">mysqlprop>
						props>
					property>
				bean>
			list>
		property>
	bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
		<property name="basePackage" value="[这里写dao接口所在的包]"/>
	bean>
beans>

1-2 spring-service.xml



<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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <context:component-scan base-package="[这里写service实现类所在的包]">context:component-scan>
    
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource" />
    bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
beans>

1-3 spring-mvc.xml



<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<mvc:annotation-driven/>
	
	<context:component-scan base-package="[这里写controller所在的包]"/>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="[这里写jsp所在文件夹的目录]" />
        <property name="suffix" value=".jsp" />
    bean>
    
    
    <mvc:default-servlet-handler/>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"/>
    bean>
beans>

2-1pom.xml(父工程)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>[工程groupId]groupId>
  <artifactId>[工程artifactId]artifactId>
  <packaging>pompackaging>
  <version>[版本号]version>
  <name>...name>
  <url>http://maven.apache.orgurl>
  
  <properties>
  	<junit.version>3.8.1junit.version>
 	<maven-compiler-plugin.version>3.8.0maven-compiler-plugin.version>
  	<tomcat7-maven-plugin.version>2.2tomcat7-maven-plugin.version>
  properties>
  
  
 <dependencyManagement>
  
  <dependencies>
  	
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit.version}version>
      
      <scope>testscope>
    dependency>
  dependencies>
  dependencyManagement>
  
  <build>
    <finalName>[项目最终名称]finalName>
     <plugins>
     	
         <plugin>
               <groupId>org.apache.maven.pluginsgroupId>
               <artifactId>maven-compiler-pluginartifactId> 
               <version>${maven-compiler-plugin.version}version>          
         	   <configuration>                
                 <source>11source>     
                 <target>11target>   
    		   configuration>      
         plugin> 
      plugins>
      
      <pluginManagement>
      	<plugins>
      		
			<plugin>
			    <groupId>org.apache.tomcat.mavengroupId>
			    <artifactId>tomcat7-maven-pluginartifactId>
			    <version>${tomcat7-maven-plugin.version}version>
			plugin>
      	plugins>
      pluginManagement>
  build>
  
  <modules>
 	
  	<module>[子工程的artifactId]module>
  modules>
project>

2-2pom.xml(子工程)


<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0modelVersion>
	
	<parent>
		<groupId>[父工程groupId]groupId>
		<artifactId>[父工程artifactId]artifactId>
		<version>[父工程版本号]version>
	parent>
  	<artifactId>[工程artifactId]artifactId>
 	<name>[工程名称]name>
 	<url>http://maven.apache.orgurl>
	<packaging>[打包方式:war或jar]packaging>
	<dependencies>
		[这里添加项目依赖,不声明依赖的版本号,例如下:]
		<dependency>
		    <groupId>org.springframeworkgroupId>
		    <artifactId>spring-beansartifactId>
		dependency>
	dependencies>
	<build>
		<finalName>[这里写最终项目名称]finalName>
		<plugins>
		
			<plugin>
				<groupId>org.apache.tomcat.mavengroupId>
				<artifactId>tomcat7-maven-pluginartifactId>
				<configuration>
					
					<path>/path>
					
					<port>9002port>
				configuration>
			plugin>
		plugins>
	build>
project>

3 web.xml


<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_3_0.xsd"
        version="3.0">
  
  <servlet>
    <servlet-name>dispatcherservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      
      <param-value>classpath:spring-*.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
    <async-supported>trueasync-supported>
  servlet>

  <servlet-mapping>
    <servlet-name>dispatcherservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    filter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>
  
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

你可能感兴趣的:(SSM)