醇醛酸酯 的博客

心之所愿,无所不成

eclipse工具 maven 整合ssm(三):SSM配置

eclipse工具 maven 整合ssm(三):SSM配置

在maven项目中,SSM的配置文件和不用maven工具时是一样的,配置文件放在src/main/resources文件夹下

1.spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:context="http://www.springframework.org/schema/context"
	    xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:p="http://www.springframework.org/schema/p"
       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.1.xsd
       					   http://www.springframework.org/schema/mvc 
       					   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
       					   http://www.springframework.org/schema/context
       					   http://www.springframework.org/schema/context/spring-context-4.1.xsd
       					   http://www.springframework.org/schema/tx
       					   http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
     <!-- 1. 加载外部配置文件db.properties-->
     <context:property-placeholder location="classpath:db.properties"/>
    
    
    <!-- 配置freeMark的模板路径 -->
	<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/templates/" />  
		<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性 -->
			<props>
            <prop key="template_update_delay">0</prop><!--刷新模板的周期,单位为秒 -->
            <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
            <prop key="locale">UTF-8</prop><!-- 本地化设置 -->
            <prop key="url_escaping_charset">UTF-8</prop>
            <prop key="classic_compatible">true</prop>
            </props>
		
		</property>
	</bean>
	
     <!--2. 配置数据源 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     	<property name="driverClassName" value="${spring.datasource.driverClassName}"></property>
     	<property name="url" value="${spring.datasource.url}"></property>
     	<property name="username" value="${spring.datasource.username}"></property>
     	<property name="password" value="${spring.datasource.password}"></property>
     </bean>
     
     <!-- 3. 配置SqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     		<property name="dataSource" ref="dataSource"></property>
     		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
     		<!-- 注意:当mapper接口与mapper.xml文件不在同一包时,需要指定xml所在路径 -->
     		<property name="mapperLocations" value="classpath:h5/mapper/*.xml"></property>
     </bean>
     
     <!-- 3.1 配置SqlSessionTemplate(可省略) -->
     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
     		<constructor-arg ref="sqlSessionFactory"></constructor-arg>
     </bean>
     <!-- 4.1 配置Dao(基于mapper代理方式) -->
     <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     
     		<!-- 扫描的包的名称,多个包之间","隔开 -->
     		<property name="basePackage" value="h5.dao"></property>
     		<!-- 注入SqlSessionFactory,可省略,底层做自动装配 -->
     		<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> -->
     </bean>
     
     <!-- 5.配置事务管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     		<property name="dataSource" ref="dataSource"></property>
     </bean>
      <!-- 初始缓存数据,初始化spring中定义的bean的前后都会用此实现类 -->
	 <bean class="h5.util.RunMain" init-method="init"></bean>
	 
     
     <!-- 5.1开启事务注解 -->
     <tx:annotation-driven transaction-manager="transactionManager"/>
     
     <!-- 5.2配置Service -->
	<context:component-scan base-package="h5.service"></context:component-scan>
    <context:component-scan base-package="h5.cache"></context:component-scan>
	
</beans>

2.数据库的配置文件db.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.0.0.1:3306/dfg?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=qwe
spring.datasource.password=123456

3.springmvc配置文件springmvc-servelt.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:context="http://www.springframework.org/schema/context"
       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.1.xsd
       					   http://www.springframework.org/schema/mvc 
       					   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
       					   http://www.springframework.org/schema/context
       					   http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	
	<!-- 处理器适配器、处理器映射器 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 处理器:批量扫描所有 zjht下的@Controller、@Service、@Repository、@Component等组件-->
	<!-- <context:component-scan base-package="zjht"></context:component-scan> -->
	<context:component-scan base-package="h5.controller"></context:component-scan>
	<context:component-scan base-package="h5.dao"></context:component-scan>
	<context:component-scan base-package="h5.service.Impl"></context:component-scan> 
	<context:component-scan base-package="h5.util"></context:component-scan> 
    <context:component-scan base-package="test"></context:component-scan> 
	
	
	<!-- 配置freeMarker视图解析器 --> 
	<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
		<property name="contentType" value="text/html; chartset=UTF-8" />
		<property name="cache" value="true" />
		<property name="prefix" value=""/>
		<property name="suffix" value=".ftl" />
		<property name="order" value="1" />
		<property name="allowRequestOverride" value="false"></property>
		<property name="allowSessionOverride" value="false"></property>
		<!-- <property name="checkTemplateLocation" value="true"></property> -->
		<!-- <property name="enabled" value="true"></property> -->
		<property name="exposeRequestAttributes" value="false"></property>
		<property name="exposeSessionAttributes" value="false"></property>
		<property name="exposeSpringMacroHelpers" value="true"></property>
		<!-- <property name="templateLoaderPath" value="false"></property> -->
	</bean>
	
	 
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="prefix" value="/"/>
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

4.mybatis配置文件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>

	<!-- 映射文件(定义SQL语句的映射文件) -->
	<!-- 由spring接管,在applicationContext.xml中的4.1 -->
	<mappers>
		<!-- <package name="h5..mapper"/> -->
	</mappers>
</configuration>

5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
       version="3.1" metadata-complete="true">
       
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
  
  <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:springmvc-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <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>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
   
    <welcome-file-list>
       	 <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
       
</web-app>





阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30938705/article/details/79281378
文章标签: maven ssm java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭