SSM框架整合案例

  • 首先看一下整合后的项目总目录:


Dao层:

1、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>

	
	
</configuration>

2、applicationContext-dao.xml:

  • 数据库连接
  • SqlSessionFactory对象,需要spring和mybatis整合包
  • 配置mapper文件扫描器
<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 引入外部数据库连接配置文件:jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 配置数据源:dbcp连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 连接池的最大数据库连接数 -->
		<property name="maxActive" value="10" />
		<!-- 最大空闲数 -->
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- 配置SqlSessionFactory:初始化mybatis核心配置文件信息 -->
	<bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定mybatis核心配置文件路径 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
		<!-- 指定数据源 :连接数据库-->
		<property name="dataSource" ref="dataSource"/>
		<!-- 配置别名包扫描: 默认pojo对象别名为类名,要扫描多个包用逗号隔开-->
		<property name="typeAliasesPackage" value="com.cyn.ssm.pojo"/>
	</bean>
	
	<!-- *配置Mapper(Dao)包扫描:将Dao层下的mapper接口注册到Spring容器中并加载映射配置文件【必须满足规范】,要扫描多个包用逗号隔开 -->
	<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 设置mapper包扫描 -->
		<property name="basePackage" value="com.cyn.ssm.mapper"/>
	</bean>
	
	
</beans>

Service层:

1.applicationContext-service.xml:

  • 设置包扫描器,扫描@Service注解的类
  • 配置事务管理器
<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- *配置Service类包的组件扫描:将Service层下的类注册到Spring容器中 -->
	<context:component-scan base-package="com.cyn.ssm.service.impl"/>
	
	<!-- 配置事务管理器 -->
	<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 指定数据源:对连接池内的数据库操作进行事务监控和管理 -->
		<property name="dataSource" ref="dataSource"/>	
	</bean>
	<!-- 配置事务的增强(切面类) -->
	<tx:advice id = "txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为:对service接口中符合以下通配符格式的方法名进行事务管理 -->
			<tx:method name="save*" propagation="REQUIRED" isolation = "DEFAULT"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!-- aop的配置:将切面类注册到目标类中进行增强 -->
	<aop:config>
		<!-- 指定目标类 -->
		<aop:pointcut expression="execution(* com.cyn.ssm.service.*.*(..))" id = "pointcut1"/>
		<!-- 配置切面:将切面类注册到目标类  -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
	</aop:config>
	
	
</beans>

Controller层:

1.springmvc.xml:

  • 设置包扫描器,扫描@Controller注解的类
  • 配置注解驱动
  • 配置视图解析器
  • 配置全局异常处理器
<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    
   
    <!-- 配置注解驱动:基于注解的映射器和适配器配置 -->
	<mvc:annotation-driven/>
    <!--*配置组件扫描:将Controller层下的类注册到Spring容器中(基于注解的处理器) -->
    <context:component-scan base-package="com.cyn.ssm.controller"/>
    
    <!-- 配置视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<!-- 配置jsp路径的前缀 -->
    	<property name="prefix" value="/WEB-INF/pages/" />
    	<!-- 配置jsp路径的后缀 -->    
        <property name="suffix" value=".jsp" />   
    </bean>
    
    <!-- 配置全局异常处理器 -->
    <bean class="com.cyn.ssm.exception.UserException"/>
    
</beans> 

Web.xml:

  • 配置spring
  • 配置过滤器
  • 配置前端控制器
<?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" 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">

  	<welcome-file-list>
    	<welcome-file>index.jsp</welcome-file>
  	</welcome-file-list>
  	
  	<!-- 配置spring:初始化Spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 指定spring配置文件路径:通配符*指定Spring全部整合配置文件 -->
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>

	<!-- 配置监听器:加载并初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置过滤器,解决post请求提交的中文乱码问题 -->
	<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>forceRequestEncoding</param-name>
		<param-value>true</param-value>
		</init-param>
		<init-param>
		<param-name>forceResponseEncoding</param-name>
		<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  	
  	
  	<!-- 配置springMVC前端控制器 -->
	<servlet>  
    	<servlet-name>springmvc</servlet-name>  
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    	<!-- 配置springmvc加载的核心配置文件所在路径,如果不配置,默认加载的是
    	/WEB-INF/【servlet-name】-servlet.xml -->
    	<init-param>  
        	<param-name>contextConfigLocation</param-name>  
        	<param-value>classpath:spring/springmvc.xml</param-value>  
    	</init-param>  
    	<load-on-startup>1</load-on-startup>  
	</servlet>  
    <!-- 配置springMVC的url请求映射 -->     
	<servlet-mapping>  
    	<servlet-name>springmvc</servlet-name>
    	<!-- 
    	第一种:*.mvc,访问以*.mvc结尾由DispatcherServlet进行解析
    	第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置DispatcherServlet
    	进行解析,使用此种方式可以实践RESTful风格的url【拦截所有的请求排除jsp!】
    	第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时无法找到handler,会报错!
    	 -->
    	<url-pattern>*.mvc</url-pattern>  
	</servlet-mapping>
</web-app>

引入外部属性文件:

1、jdbc.properties:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull
jdbc.username = root
jdbc.password = 123456

2、log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

附项目地址: 

猜你喜欢

转载自blog.csdn.net/qq_37230121/article/details/83819072