ssm整合

主要就是四个文件以及web.xml文件配置

配置文件基本结构

在这里插入图片描述

applicationContext.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
       
       <!-- 配置注解扫描范围 -->
       <context:component-scan base-package="com.zdhf.service.impl,com.zdhf.test" />
       
       
        <!-- 读取database.properties文件 -->
       <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	       	<!-- 指定properties文件所在路径 -->
	       	<property name="location" value="classpath:database.properties"></property>
       </bean>
       
       <!-- Dbcp配置数据源 -->
      <!--  <context:property-placeholder location="classpath:database.properties"/> -->
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	       	<property name="driverClassName" value="${driver}"></property>
	       	<property name="url" value="${url}"></property>
	       	<property name="username" value="${user}"></property>
	       	<property name="password" value="${password}"></property>
       </bean> 
       
       <!-- 配置扫描保存sql语句的局部xml文件 -->
       <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
       		<!-- 指定数据源 -->
       		<property name="dataSource" ref="dataSource"></property>
       		<!-- 指定局部xml文件的位置 -->
       		<property name="mapperLocations" value="classpath*:com/zdhf/mapper/*.xml"></property>
       </bean>
       
       <!-- 扫描mapper接口类,并且将接口类与xml文件关联 -->
       <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       		<!-- 指定mapper接口类存放的位置 -->
       		<property name="basePackage" value="com.zdhf.mapper"></property>
       </bean>
       
</beans>

注意:配置的扫描范围以及读取链接数据库database.properties的位置,还有mapper.xml的位置、mapper的接口位置

database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_ssm?characterEncoding=utf-8
user=root
password=haoren123

注意:连接的数据库名字以及账号密码

log4j.properties

#log4j.rootLogger=debug, stdout
#
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %l %m %n

log4j.rootLogger=debug,logfile

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%p %d %F %M  %m%n

这个是写日志的

springmvc.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"
	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.2.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 自動扫描Spring的注解 -->
	<!-- 
		特别要注意:base-package="com.java.controller"> 一定不能配置为com.java,这个这样配置了,事务将不起作用,
		因为配置为com.java,将在SpringMvc中扫描@service,这样Spring的容器中将会有service的实例,而applicationContext.xml的扫描
		就不会产生service的实例,可以事务的AOP将无法扩展功能。同时注意:controller中注入service实例只能用接口,不然就就会报错。
	-->
	<context:component-scan base-package="com.zdhf.controller"></context:component-scan>
	
	<!-- 扫描springmvc中的注解 -->
	<!-- <mvc:annotation-driven></mvc:annotation-driven> -->
	
	<!-- 数据验证 -->
	<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
       	<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
    </bean>
    
    <!-- 上传文件 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		
		<property name="maxUploadSize" value="1048576"></property>
	</bean>
	
	
	<!-- 
	<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	 -->
</beans>

注意:扫描包的位置

web.xml

  <!-- 设置字符集编码过滤器 -->
  <filter>
  	<filter-name>char</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>char</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 指定applicationContext.xml位置 -->
  <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>
 

  
  <!-- springmvc的本质是servlet -->
  <servlet>
  	<servlet-name>action</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
  	
  
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>action</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

注意:在原有的web.xml中加入这一段即可。如果和我文件放的位置不同,记得修改自己的springmvc.xml的位置和applicationContext.xml的位置

最后自己编写测试类即可,但是在ssm使用junit测试会报错

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值