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

Mybatis 和 Spring配置

时间:2015-11-23 13:31:59      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

一、使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了、

一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 ) 技术分享

 

二、 web.xml配置文件

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>WeShare</display-name>
	<welcome-file-list>
		<welcome-file>/jumper.html</welcome-file>
	</welcome-file-list>
	<!-- 加载spring容器配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置Spring容器加载配置文件路径 (主要配置都在这里面) -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml
  </param-value>
	</context-param>

	<!-- 配置Spring核心控制器 -->
	<servlet>
		<servlet-name>web</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 不指定 <init-param> 会自动找web.xml相同路径下 web-servlet.xml文件 -->
		<!-- 
		<init-param> 
			<param-name>contextConfigLocation</param-name> 
			<param-value>/WEB-INF/dispatcher.xml</param-value> 
		</init-param> 
			-->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>web</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>web</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>

  <!-- dwr 添加配置  -->
  <servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<description></description>
			<param-name>debug</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>
  <servlet-mapping>
       <servlet-name>dwr-invoker</servlet-name>
       <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>


	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/common/jsp/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>403</error-code>
		<location>/common/jsp/error403.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/common/jsp/error404.jsp</location>
	</error-page>

</web-app>

三、 <!-- 配置Spring核心控制器 -->  <servlet>   <servlet-name>web</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <!-- 不指定 <init-param> 会自动找web.xml相同路径下 web-servlet.xml文件 -->   <!--   <init-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/dispatcher.xml</param-value>   </init-param>    -->   <load-on-startup>1</load-on-startup>  </servlet>  这个我使用的是默认的 web-servlet.xml文件. 如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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:p="http://www.springframework.org/schema/p" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.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">
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
 	
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    	<property name="prefix" value="/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
          
    <context:component-scan base-package="com.weshare.**.web"/>
     <!-- /hrtiaoxin/src/java/com/guohualife/   hr/pfc/   web/controller/HrDeptMarkController.java -->
</beans>

 

四: applicationContext.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.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">


<!-- properties配置文件 -->
	<bean id="config"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 是否忽略不可解析的 -->
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<!-- 多个locations, 单个location <value> -->
		<property name="locations">
			<list>
				<value>/WEB-INF/config/config.properties</value>
				<value>/WEB-INF/config/urlAddress.properties</value>
			 <!-- 
				<value>/WEB-INF/platform/config/config.properties</value>
				<value>/WEB-INF/config/config.properties</value>
				<value>/WEB-INF/hr/config/config.properties</value>
				 -->
			</list>
		</property>
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"   p:defaultEncoding="UTF-8" ></bean>
	
	<!-- 加载 其他xml文件 -->
	<import resource="/config/aC-common.xml" />
	<import resource="/config/aC-interceptor.xml" />
	<import resource="/config/aC-properties.xml" />
	<import resource="/config/aC-quartz-config.xml" />

</beans>


 

包含的其他4个xml文件:

4.1 : aC-common.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.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">

	<context:component-scan base-package="com.weshare.*.api.dao"></context:component-scan>
	<context:component-scan base-package="com.weshare.*.api.service"></context:component-scan>
	<context:component-scan base-package="com.weshare.common.utils"></context:component-scan>
	

	<bean id="weshareDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${common.db.driver}" />
		<property name="url" value="${common.db.url}" />
		<property name="username" value="${common.db.username}" />
		<property name="password" value="${common.db.password}" />
		<!-- 最大连接数据库连接数 -->
		<property name="maxActive" value="500" />
		<!-- 最大等待连接中的数量   0标识没有限制 -->
		<property name="maxIdle" value="10" />
		<!-- 最大等待毫秒数  超时报错 -->
		<property name="maxWait" value="500" />
		<property name="defaultAutoCommit" value="true" />
		<!-- 是否自我中断 -->
		<property name="removeAbandoned" value="true" />
		<property name="removeAbandonedTimeout" value="60" />
	</bean>
	
	<bean id="weshareSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<property name="dataSource">
            <ref bean="weshareDataSource" />
        </property>
        <!-- MyBatis 的 XML 配置文件路径 -->
        <property name="configLocation" value="/WEB-INF/config/mybatisSqlMapConfig.xml" />
		<!-- 扫描自动生成的xml文件 --><!-- Mybatis XML映射文件 -->
		
        <property name="mapperLocations"  >
        	<list><!-- Mybatis XML映射文件 -->
        		<value>classpath*:com/weshare/common/generated/xml/*.xml</value>
        		<!-- 扫描自己写的xml文件--> 
        		<value>classpath*:com/weshare/*/api/xml/*.xml</value>
        	</list>	
        </property>
        
    </bean>
    
    <bean id="weshareSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    	<constructor-arg index="0" ref="weshareSessionFactory"></constructor-arg>
    </bean>
	
	<!-- 注册单个  mybatisGenerator  自动生成的 接口文件-->
	<!-- 
	<bean id="TestMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="com.weshare.common.generated.dao.TestMapper" />
        <property name="sqlSessionTemplate" ref="weshareSqlSessionTemplate" ></property>  
    </bean>
     -->
     <!-- 扫描mybatisGenerator 自动生成的  所有接口-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<property name="basePackage" value="com.weshare.common.generated.dao" ></property>
	</bean>
	
	
	
	<!-- 数据库事务策略-->
	<bean id="weshareTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="weshareDataSource" />
        </property>
    </bean>
	
	<tx:advice id="weshareTxAdvice" transaction-manager="weshareTransactionManager">
		<tx:attributes>
		<!-- 
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="ins*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			 -->
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<aop:config proxy-target-class="true">
		<aop:advisor pointcut="execution( * com.weshare.*.api.service.*.*(..))" advice-ref="weshareTxAdvice" />
		
	</aop:config>
	

</beans>

 

4.2 aC-interceptor.xml

这里拦截器只是拦截到controller , 具体拦截到action‘,后面会有写到, 这里的配置只是参考。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 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:p="http://www.springframework.org/schema/p"
	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/tx 
       http://www.springframework.org/schema/tx/spring-tx.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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">




 <mvc:interceptors>  
        <mvc:interceptor>  
            <!--设置拦截的路径  mvc:mapping指定到哪个action ,  用mappingURL匹配方法-->  
            <mvc:mapping path="/dynamic/dynamic.do" />  
            <bean class="com.weshare.common.web.LoginInterceptorController"> 
             	<property name="mappingURL" value="^.*checklogin$" />
            </bean>  
        </mvc:interceptor>  
    </mvc:interceptors>  
 
 
 
 

</beans>
       

4.3 aC-properties.xml

这个xml作用是在启动项目的时候给org.springframework.beans.factory.config.PropertiesFactoryBean  赋值,这样在代码中可以使用下面方法获得这些值

@Resource
	private Properties imageUrlProperties;

 

imageUrlProperties.getProperty("dynamicUrl")

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.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">


	<!-- platform properties -->
	<bean id="imageUrlProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="singleton" value="true" />
		<property name="properties">
			<props>
				<prop key="aliyuming">${aliyuming}</prop>
				<prop key="ACCESS_ID">${ACCESS_ID}</prop>
				<prop key="ACCESS_KEY">${ACCESS_KEY}</prop>
				<prop key="bucketDynamicAndHeadimages">${bucketDynamicAndHeadimages}</prop>
				<prop key="faceurl.pre">${faceurl.pre}</prop>
				<prop key="imagesUrl.pre">${imagesUrl.pre}</prop>
				<prop key="dynamicUrl">${dynamicUrl}</prop>
				<prop key="headUrl">${headUrl}</prop>
				<prop key="edge.dynamic">${edge.dynamic}</prop>
				<prop key="edge.small">${edge.small}</prop>
				<prop key="edge.middle">${edge.middle}</prop>
				<prop key="edge.big">${edge.big}</prop>
			</props>
		</property>
	</bean>


</beans>

4aC-quartz-cofig.xml, 这个是批处理定时任务的xml配置方法,  在这里我并没有使用, 我使用的总是注解的方式, 后面会讲到。

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/fex http://www.springframework.org/schema/fex/spring-fex-1.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xmlns="http://www.springframework.org/schema/beans">
	<!-- 下面是使用注解配置的方法 -->
	<context:component-scan base-package="com.weshare.*.api.batch" />
	<task:executor pool-size="5" id="executor" />
	<task:scheduler pool-size="10" id="scheduler" />
	<task:annotation-driven scheduler="scheduler"	executor="executor" />
		
		
		
		
		
	<!-- 下面是 使用xml配置的方法 -->

	<!-- <bean id="personBatch" class="com.weshare.person.api.batch.PersonBatch" 
		/> -->

	<!-- 启动触发器的配置开始 -->

	<!-- <bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
		<property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property> 
		</bean> -->

	<!-- 启动触发器的配置结束 -->

	<!-- quartz-2.x的配置 -->

	<!-- <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
		<property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property 
		name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->

	<!-- 调度的配置结束 -->

	<!-- job的配置开始 -->

	<!-- <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
		<property name="targetObject"> <ref bean="personBatch" /> </property> <property 
		name="targetMethod"> <value>testMethod</value> </property> </bean> -->

	<!-- job的配置结束 -->
</beans>

 

五:注解方式

下面deleteDynamic方法的 调用地址为: localhost:8080/xx工程名/dynamic/admin.do?action=deleteDynamic

 

技术分享

 

Mybatis 和 Spring配置

标签:

原文地址:http://www.cnblogs.com/jinzhiming/p/4988026.html

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