ssm整合

添加spring架包

ssm整合_第1张图片

添加mybatis架包

ssm整合_第2张图片

添加springmvc架包

只要添加spring-webmvc-4.2.5.RELEASE.jar这个架包

web.xml配置

特别说明一下:我这里没用c3p0连接池,用的是阿里的druid,还有就是spring配置文件分为springmvc.xml和spring.xml,降低耦合。

  
	<filter>
		<description>字符集过滤器description>
		<filter-name>encodingFilterfilter-name>
			<filter-class>
			    org.springframework.web.filter.CharacterEncodingFilter
			filter-class>
		<init-param>
			<description>字符集编码description>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>encodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	  
    
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:spring.xmlparam-value>
	context-param>
	<listener>
	    <listener-class>
	        org.springframework.web.context.ContextLoaderListener
	    listener-class>
	listener>
    
    
	<servlet>
		<servlet-name>dispatcherServletservlet-name>
			<servlet-class>
			    org.springframework.web.servlet.DispatcherServlet
			servlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc.xmlparam-value>
		init-param>
		
		<load-on-startup>1load-on-startup>
	servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServletservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	
	<filter>
	    <filter-name>HiddenHttpMethodFilterfilter-name>
		<filter-class>
		     org.springframework.web.filter.HiddenHttpMethodFilter
		filter-class>
	filter>
	<filter-mapping>
	    <filter-name>HiddenHttpMethodFilterfilter-name>
	    <url-pattern>/*url-pattern>
	filter-mapping>
	
   
   <servlet>
      <servlet-name>DruidStatViewservlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
  servlet>
  <servlet-mapping>
      <servlet-name>DruidStatViewservlet-name>
      <url-pattern>/druid/*url-pattern>
  servlet-mapping>

spring.xml配置

这里有个特别注意的事项:在spring.xml里面要排除2个注解包,不扫描他们,因为springmvc.xml也配置扫描器,那么程序在启动的时候会扫描2次,所以我在spring.xml里面将
排除@Controller注解的包和@ControllerAdvice注解的包。

 
	<context:component-scan base-package="com.znsd.ssm">
		
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	context:component-scan>
   
   
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
     <property name="url" value="jdbc:mysql://localhost:3306/vote" />
     <property name="username" value="root"/>
     <property name="password" value="100" />

     <property name="filters" value="stat" />

     <property name="maxActive" value="20" />
     <property name="initialSize" value="1" />
     <property name="maxWait" value="60000" />
     <property name="minIdle" value="1" />

     <property name="timeBetweenEvictionRunsMillis" value="60000" />
     <property name="minEvictableIdleTimeMillis" value="300000" />

     <property name="testWhileIdle" value="true" />
     <property name="testOnBorrow" value="false" />
     <property name="testOnReturn" value="false" />

     <property name="poolPreparedStatements" value="true" />
     <property name="maxOpenPreparedStatements" value="20" />

     <property name="asyncInit" value="true" />
 bean>
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource" />
		
		<property name="mapperLocations" value="classpath:com/znsd/ssm/dao/imp/*.xml" />
		
		<property name="configLocation" value="classpath:myBatis.xml">property>
	bean>
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="com.znsd.ssm.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	bean>
	
	
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	bean>
	
    
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="search*" propagation="REQUIRED" read-only="true" />
			<tx:method name="query*" propagation="REQUIRED" read-only="true" />
			
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="submit*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			
			<tx:method name="*" propagation="REQUIRED" read-only="false" />
		tx:attributes>
	tx:advice>
	
	 
	<aop:config>
	    <aop:pointcut id="serviceMethod" expression="execution(* com.znsd.ssm.service.*.*.*(..))" />
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	aop:config>

spirngmvc.xml配置

<context:component-scan base-package="com.znsd.ssm" use-default-filters="false">
        
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
   context:component-scan>
   
    
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="prefix" value="/WEB-INF/views/" />
	    <property name="suffix" value=".html" />
	bean>
	
	
	<mvc:default-servlet-handler />
	
	
	<mvc:annotation-driven />

mybatis.xml配置

<configuration>
    
	<settings>
	    
	    <setting name="lazyLoadingEnabled" value="true"/>
	    
	    <setting name="aggressiveLazyLoading" value="true"/>
	settings>
configuration>

最后还有一点要补充的:就是将ssm整合好了,返回json数据格式的时候会报错 NO serializer found for class,数据可以查询的出,在页面显示不出来。

具体原因是因为json解析的时候handler,实体类没有get方法,导致报错NO serializer found for class(不能序列化这个类),
解决方法一:在实体类前面添加@JsonIgnoreProperties(value={“handler”}),这个的意思是忽略掉handler属性,这个是序列化的时候自动生成的一个属性。
解决方法二:添加get方法。

你可能感兴趣的:(spring,springMVC,Mybatis)