欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMvc+MyBatis+Freemarker 配置

程序员文章站 2022-06-22 18:18:38
...


SpringMvc+MyBatis+Freemarker 配置
            
    
    博客分类: mybatissping freemarker 
 1、引入Jar包:

spring所有jar文件

mybatis:mybatis-3.1.1-SNAPSHOT.jar、mybatis-generator-core-1.3.1.jar、mybatis-spring-1.1.1-SNAPSHOT.jar

连接池:commons-dbcp.jar

freemarker-2.3.26.jar

其他:commons-collections-3.1.jar、commons-logging-1.1.1.jar、commons-pool.jar、mysql-connector-java-5.1.40-bin.jar、servlet-api.jar

 

2、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">
  <display-name></display-name>	
  	<servlet>
    	<servlet-name>spring</servlet-name>
    	<servlet-class>
        	org.springframework.web.servlet.DispatcherServlet
    	</servlet-class>
    	<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
    	<servlet-name>spring</servlet-name>
   		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<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>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
 
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

 3、spring-mvc.xml

<!-- spring自动扫描注解的组件 -->
	<context:annotation-config />
	<context:component-scan base-package="com.zf.action,com.zf.action.*" />
	<context:component-scan base-package="com.zf.service,com.zf.service.*" />
	<context:component-scan base-package="com.zf.dao,com.zf.dao.*" />

 

4、spring-mybatis.xml

<!-- 用于直指定配置文件的位置信息,在dataSource中可以使用 -->
	<context:property-placeholder location="classpath:jdbc.properties"
		ignore-unresolvable="true" />

	<!-- 配置数据源,用了dbcp数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClass}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.userName}" />
		<property name="password" value="${jdbc.password}" />


		<property name="initialSize" value="${dbcp.initialSize}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="minIdle" value="${dbcp.minIdle}" />
		<property name="maxWait" value="500" />
		<property name="poolPreparedStatements" value="false" />
		<property name="timeBetweenEvictionRunsMillis" value="900000" />
		<property name="minEvictableIdleTimeMillis" value="900000" />
	</bean>


	<!-- 创建SqlSessionFactory,同时指定数据源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--configLocation属性指定mybatis的核心配置文件 -->
		<property name="mapperLocations">
			<list>
				<value>classpath:com/zf/dao/*.xml</value>
			</list>
		</property>
	</bean>

	<!-- MapperScannerConfigurer配置 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。可指定多个包,包与包之间用逗号或分号分隔 -->
		<property name="basePackage" value="com.zf.dao" />
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource" />

 5、jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/pf_xyslga_web?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.userName=root
jdbc.password=root
dbcp.initialSize=5
dbcp.maxActive=20
dbcp.maxIdle=5  
dbcp.minIdle=1

 6、spring-freemarker.xml

<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/" />
		<property name="defaultEncoding" value="UTF-8" />

		<property name="freemarkerSettings">
			<props>
				<prop key="classic_compatible">true</prop><!-- 如果为null则转为空值,不需要再用!处理 -->
				<prop key="whitespace_stripping">true</prop><!-- 去掉多余的空格,非常有用 -->
				<prop key="template_update_delay">0</prop><!-- 模版更新时间,这里配置是1秒更新一次,正式环境,模版不会改变,可以将这个值设很大,提高效率.就是被这个参数害死了.老是不更新. -->
				<prop key="default_encoding">utf-8</prop>
				<prop key="url_escaping_charset">utf-8</prop>
				<prop key="number_format">0</prop>
				<prop key="object_wrapper">freemarker.template.ObjectWrapper.BEANS_WRAPPER
				</prop><!-- Beans包裹,可以直接使用对象 -->
			</props>
		</property>

	</bean>
	<!-- freemarker视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="viewNames">
			<array>
				<value>*.ftl</value>
			</array>
		</property>
		<property name="contentType" value="text/html;charset=UTF-8" />
		<!-- 此变量值为pageContext.request, 页面使用方法:rc.contextPath -->
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<property name="allowRequestOverride" value="true" />
	</bean>

 

相关标签: freemarker