码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc+spring+mybatis整合

时间:2016-08-10 14:34:19      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:ssm

1 导入相关jar包

2 编写配置文件

web.xml----配置spring springmvc

                 <!-- spring配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context</listener-class>
  </listener>
  
                 <!-- springMVC配置文件 -->
  <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:mvc.xml</param-value>
  		</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>

mvc.xml-----springmvc使用注解,如果需要json或其他处理需填写相关配置

<?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: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.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd" > 
	
	<!-- spring组件扫描该包下的注解 -->
	<context:component-scan base-package="com.wc.controller"/>
</beans>

appilicationContext---------spring

<?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: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/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd" > 
  <!-- 读取属性文件 -->
  <bean class="org.springframework.beans.factory.config.PlaceholderConfigurerSupport">
  	<property name="location" value="classpath:db.properties" />
  </bean>
  <!-- 配置datasource -->
  <bean id="dataSourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  	<property name="driverClassName" value="${driver}"></property>
  	<property name="url" value="${url}"></property>
  	<property name="username" value="${username}"></property>
  	<property name="password" value="${password}"></property>
  </bean>
  <!-- 配置工厂 -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  		<property name="dataSource" ref="dataSourse"></property>
  		<property name="configLocation" value="classpath:mybatis-config.xml" ></property>
  </bean>
  <!-- 配置声明式事务 -->
  <!-- 事务管理器 -->
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   	<property name="dataSourse" ref="dataSourse" />
  </bean>
  <tx:advice id="txAdvice" transaction-manager="txManager">
  	<tx:attributes>
  		<tx:method name="save" propagation="REQUIRED"/>
  		<tx:method name="get" read-only="true"/>
  		<tx:method name="*" propagation="REQUIRED"/>
  	</tx:attributes>
  </tx:advice>
  <aop:config>
  	<aop:pointcut expression="exuetion(* com.wc.service.impl.*.*(..))" id="pointcut"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>  
  </aop:config>
  
  <context:component-scan base-package="com.wc"></context:component-scan> 
  
</beans>

mybatis-config.cml----------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>
  <typeAliases>
  	<package name="com.wc.vo"/>
  </typeAliases>
  <mappers>
   <!-- 所有mapper文件填写位置 -->
  </mappers>
</configuration>


springmvc+spring+mybatis整合

标签:ssm

原文地址:http://sdqdwc.blog.51cto.com/11678553/1836473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!