spring mvc系列文章 - springmvc spring mybatis ibatis freemark整合开发(2.0版:基于注解)

基于注解的话,配置文件就简单多了,只需一个核心配置文件即可。


1 applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"        
	xmlns:tx="http://www.springframework.org/schema/tx"         
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"         
	xmlns:context="http://www.springframework.org/schema/context"        
	xsi:schemaLocation="         
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd         
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"        
	default-autowire="byName">  
  
	<!-- 引入配置文件    
		<bean id="propertyConfigurer"
			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location">
				<value>classpath:mysql.properties</value>
			</property>
		</bean>
		-->
	
	<!--创建jdbc数据源 --> 
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
		<property name="url" value="jdbc:mysql://localhost:3306/test"/>  
		<property name="username" value="root"/>
		<property name="password" value="jiaojun"/>  
	</bean>  

	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
		<property name="dataSource" ref="dataSource" />  
	</bean>  
    
	<!-- 创建SqlSessionFactory,同时指定数据源 -->    
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
		<!--<property name="configLocation" value="classpath:mybatis-config.xml" /> --> 
		<property name="dataSource" ref="dataSource" />  
	</bean>  
    
	<!--以注解方式扫描Spring Bean --> 
	<context:annotation-config /> 
	<!--配置扫描SpringBean的时候 都需要扫描哪些目录下 --> 
	<context:component-scan base-package="com.springmvc"></context:component-scan> 
    
	<!-- 可通过注解控制事务 -->  
	<tx:annotation-driven />  
  
	<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
	<!-- -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
		<property name="basePackage" value="com.springmvc.dao" />  
	</bean> 
    
	<!-- jsp视图配置 -->              
	<!--   
		<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
			<property name="prefix" value="/"/>  
			<property name="suffix" value=".jsp"/>  
		</bean>  
		--> 
    
	<!-- freemarker视图解析配置 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<property name="suffix" value=".ftl" />
		<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
		<property name="contentType" value="text/html;charset=UTF-8"></property>
		<property name="requestContextAttribute" value="request" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
	</bean>
    
    
	<!-- freemark初始化配置项 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/template/" />
		<property name="freemarkerSettings">
			<props>
				<!-- 如果模板不经常更新,此属性设置更新延迟时间 -->
				<prop key="template_update_delay">60</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="number_format">#.###</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="classic_compatible">true</prop>
				<prop key="template_exception_handler">ignore</prop>
			</props>
		</property>
	</bean>
    
</beans>  



源码下载地址: http://download.csdn.net/detail/sl0007/4496354

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值