SpringMVC4.1.6整合MyBatis3.4.1的标准配置

工程结构


web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 防止出现如下异常
	JNDI lookup for name [spring.liveBeansView.mbeanDomain] 
	threw NamingException with message: Name [spring.liveBeansView.mbeanDomain] is not bound in this Context. 
	Unable to find [spring.liveBeansView.mbeanDomain].. Returning null.
	 -->
	<context-param>
	  <param-name>spring.profiles.active</param-name>
	  <param-value>dev</param-value>
 	</context-param>
 	<context-param>
	  <param-name>spring.profiles.default</param-name>
	  <param-value>dev</param-value>
 	</context-param>
 	<context-param>
	  <param-name>spring.liveBeansView.mbeanDomain</param-name>
	  <param-value>dev</param-value>
 	</context-param>
<!-- 配置Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 配置接听器的目的:让Spring容器随着web的开启而开启 -->
	<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>
		<!-- 
			contextConfigLocation配置springmvc加载的配置文件(配置处理映射器,适配器等)
			如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml
		 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<!-- 乱码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



SqlMapConfig.xml的配置(MyBatis的配置)

<?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>
	<!-- 由于springmvc和mybatis的整合进行mapper扫描,这里不需要配置
		必须遵循:mapper.xml和mapper.java文件同名且在同一个目录
	-->
	<mappers>
		<package name="cn.itcast.ssm.mapper"/>
	</mappers>
</configuration>
applicationContext.xml的配置(Spring的配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/mvc
			http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           
    <!-- 加载配置文件 -->  
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 ,数据库链接信息,两种数据源都可以
    jdbc:	org.springframework.jdbc.datasource.DriverManagerDataSource
    dbcp:	org.apache.commons.dbcp.BasicDataSource 需要制定method="close"
    -->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   		<property name="driverClassName" value="${jdbc.driver}"/>
   		<property name="url" value="${jdbc.url}"/>
   		<property name="username" value="${jdbc.username}"/>
   		<property name="password" value="${jdbc.password}"/>
   </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>
			<!-- 配置哪些方法使用什么样的事务,配置事务的传播特性,
			propagation="REQUIRED",如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 -->
		
			<tx:method name="insertUser" propagation="REQUIRED" rollback-for="Throwable"/>		
			<tx:method name="deleteUserById" propagation="REQUIRED" rollback-for="Throwable"/><!-- 当deleteUserById发生异常时回滚 -->
			<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/> <!-- 配置所有方法回滚 -->
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* cn.itcast.ssm.service.impl.*.*(..))" id="pointcut"/><!-- 切入点 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	<!-- 声明式事务配置 结束 -->
	
    <!-- 配置SqlSessionFactory,目的是的到SqlSessionFactory对象-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载SqlMapConfig.xml配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>
	
	<!-- 配置Mapper的扫描器,相当于SqlMapConfig.xml文件中的
	<mappers>
		<package name="cn.itcast.ssm.mapper"/>
	</mappers>
	 -->
	 <!--  
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		 如果需要扫描多个包,中间用逗号隔开
		<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	-->
	<import resource="config/spring/user.xml"/>
  
</beans>

user.xml的配置(在applicationContext.xml中导入的Spring文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/mvc
			http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           
   <!-- UserDaoImpl接口继承了SqlSessionDaoSupport,所以要注入sqlSessionFactory --> 
   <bean id="UserDao" class="cn.itcast.ssm.dao.impl.UserDaoImpl">
   		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
   </bean>
   <bean id="UserService" class="cn.itcast.ssm.service.impl.UserServiceImpl">
   		<property name="userdao" ref="UserDao"></property>
   </bean>
   
<!-- 配置handler -->
	<bean id="insertUser" class="cn.itcast.ssm.controller.ItemController1" scope="prototype">
		<property name="userService" ref="UserService"></property>
	</bean>
<!--	<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>-->

<!-- 配置控制器映射器,控制器适配器 -->
<!--	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!--	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
	<!--使用mvc:annotation-driven代替上面的注解映射器和注解适配器 
		mvc:annotation-driven默认加载了很多参数绑定方法,
	 -->
  <mvc:annotation-driven/>
  
  <!-- 配置视图解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
db.properties的配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/springmvc
jdbc.username=root
jdbc.password=mysqladmin

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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


其余的java比较简单,此处省略,本文章重点是SpringMVC与Mybatis的整合


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值