Freemark与Spring MVC的整合

1、项目的视图存放位置2、整合spring MVC 的配置,web.xml的配置如下[html] view plain copyprint?xml version="1.0" encoding="UTF-8"?>  web-app version="3.0" xmlns="http://java.sun.com/xml/ns/java
摘要由CSDN通过智能技术生成

1、项目的视图存放位置


2、整合spring MVC 的配置,web.xml的配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.     <display-name></display-name>  
  7.     <!-- 加载spring容器 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:applicationContext-*.xml</param-value>  
  11.     </context-param>  
  12.     <listener>  
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  14.     </listener>  
  15.     <!-- springMVC前端控制器 -->  
  16.     <servlet>  
  17.         <servlet-name>springmvc</servlet-name>  
  18.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  19.         <init-param>  
  20.             <param-name>contextConfigLocation</param-name>  
  21.             <param-value>classpath:springmvc.xml</param-value>  
  22.         </init-param>  
  23.     </servlet>  
  24.     <servlet-mapping>  
  25.         <servlet-name>springmvc</servlet-name>  
  26.         <url-pattern>*.action</url-pattern>  
  27.     </servlet-mapping>  
  28.   
  29.     <!--主页的配置 -->  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jsp</welcome-file>  
  32.     </welcome-file-list>  
  33. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	<display-name></display-name>
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</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:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

	<!--主页的配置 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
2、 FreeMarker与SpringMVC整合,首先,在springmvc的配置文件普通视图之前 ,加入freemarker的视图。Spring MVC的配置文件,

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  8.         http://www.springframework.org/schema/mvc   
  9.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  10.         http://www.springframework.org/schema/context   
  11.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  12.         http://www.springframework.org/schema/aop   
  13.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  14.         http://www.springframework.org/schema/tx   
  15.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  16.   
  17.     <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->  
  18.     <context:component-scan base-package="com.controller"></context:component-scan>  
  19.     <!--静态资源的处理 -->  
  20.     <mvc:resources location="/resources/" mapping="/resources/**" />  
  21.     <!--mvc的注解开发 -->  
  22.     <mvc:annotation-driven></mvc:annotation-driven>  
  23.       
  24.     <!-- freemarker config -->  
  25.     <bean id="freemarkerConfig"  
  26.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  27.         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />  
  28.         <!-- 设置FreeMarker环境属性 -->  
  29.         <property name="freemarkerSettings">  
  30.             <props>  
  31.                 <!--刷新模板的周期,单位为秒 -->  
  32.                 <prop key="template_update_delay">20</prop>  
  33.                 <!--模板的编码格式 -->  
  34.                 <prop key="default_encoding">UTF-8</prop>  
  35.                 <!--本地化设置 -->  
  36.                 <prop key="locale">UTF-8</prop>  
  37.                 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
  38.                 <prop key="time_format">HH:mm:ss</prop>  
  39.                 <prop key="number_format">0.####</prop>  
  40.                 <prop key="boolean_format">true,false</prop>  
  41.                 <prop key="whitespace_stripping">true</prop>  
  42.                 <prop key="tag_syntax">auto_detect</prop>  
  43.                 <prop key="url_escaping_charset">UTF-8</prop>  
  44.             </props>  
  45.         </property>  
  46.   
  47.     </bean>  
  48.   
  49.     <!-- View resolvers can also be configured with ResourceBundles or XML files.   
  50.         If you need different view resolving based on Locale, you have to use the   
  51.         resource bundle resolver. -->  
  52.     <bean id="viewResolver"  
  53.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  54.         <property name="cache" value="true" />  
  55.         <property name="prefix" value="" />  
  56.         <property name="suffix" value=".ftl" />  
  57.         <property name="contentType" value="text/html; charset=UTF-8"></property>  
  58.         <property name="viewClass">  
  59.             <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView  
  60.             </value>  
  61.         </property>  
  62.     </bean>  
  63.   
  64.     <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->  
  65.     <bean  
  66.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  67.         <!-- 配置jsp路径的前缀 -->  
  68.         <property name="prefix" value="/WEB-INF/jsp/" />  
  69.         <!-- 配置jsp路径的后缀 -->  
  70.         <property name="suffix" value=".jsp" />  
  71.     </bean>  
  72.   
  73. </beans>  
<?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:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
	<context:component-scan base-package="com.controller"></context:component-scan>
	<!--静态资源的处理 -->
	<mvc:resources location="/resources/" mapping="/resources/**" />
	<!--mvc的注解开发 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- freemarker config -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<!-- 设置FreeMarker环境属性 -->
		<property name="freemarkerSettings">
			<props>
				<!--刷新模板的周期,单位为秒 -->
				<prop key="template_update_delay">20</prop>
				<!--模板的编码格式 -->
				<prop key="default_encoding">UTF-8</prop>
				<!--本地化设置 -->
				<prop key="locale">UTF-8</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="number_format">0.####</prop>
				<prop key="boolean_format">true,false</prop>
				<prop key="whitespace_stripping">true</prop>
				<prop key="tag_syntax">auto_detect</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
			</props>
		</property>

	</bean>

	<!-- View resolvers can also be configured with ResourceBundles or XML files. 
		If you need different view resolving based on Locale, you have to use the 
		resource bundle resolver. -->
	<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="contentType" value="text/html; charset=UTF-8"></property>
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView
			</value>
		</property>
	</bean>

	<!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路径的前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 配置jsp路径的后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

3、Spring相关配置文件的加载

      3.1 db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/student  
  3. jdbc.username=root  
  4. jdbc.password=1234  
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=1234

      3.2 applicationContext-dao.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.   
  16.     <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->  
  17.     <context:property-placeholder location="classpath:db.properties" />  
  18.   
  19.     <!-- 配置数据源,dbcp -->  
  20.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  21.         destroy-method="close">  
  22.         <property name="driverClassName" value="${jdbc.driver}" />  
  23.         <property name="url" value="${jdbc.url}" />  
  24.         <property name="username" value="${jdbc.username}" />  
  25.         <property name="password" value="${jdbc.password}" />  
  26.         <property name="maxActive" value="30" />  
  27.         <property name="maxIdle" value="5" />  
  28.     </bean>  
  29.   
  30.     <!-- sqlSessionFactory -->  
  31.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  32.         <!-- 数据库连接池 -->  
  33.         <property name="dataSource" ref="dataSource" />  
  34.         <!-- 加载mybatis的全局配置文件 -->  
  35.         <property name="configLocation" value="classpath:sqlMapConfig.xml" />  
  36.     </bean>  
  37.   
  38.     <!-- mapper扫描器 -->  
  39.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  40.         <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->  
  41.         <property name="basePackage" value="com.mapper"></property>  
  42.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
  43.     </bean>  
  44.   
  45. </beans>  
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置数据源,dbcp -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>

	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
	</bean>

	<!-- mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
		<property name="basePackage" value="com.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

</beans>

      3.3 applicationContext-transaction.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.   
  16.     <!-- 事务管理器 对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类 -->  
  17.     <bean id="transactionManager"  
  18.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.         <!-- 数据源 dataSource在applicationContext-dao.xml中已经配置 -->  
  20.         <property name="dataSource" ref="dataSource" />  
  21.     </bean>  
  22.     <!-- 通知 -->  
  23.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  24.         <tx:attributes>  
  25.             <!-- 传播行为 -->  
  26.             <tx:method name="save*" propagation="REQUIRED" />  
  27.             <tx:method name="delete*" propagation="REQUIRED" />  
  28.             <tx:method name="insert*" propagation="REQUIRED" />  
  29.             <tx:method name="update*" propagation="REQUIRED" />  
  30.             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
  31.             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />  
  32.             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />  
  33.         </tx:attributes>  
  34.     </tx:advice>  
  35.   
  36.     <!-- aop -->  
  37.     <aop:config>  
  38.         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.*.*(..))" />  
  39.     </aop:config>  
  40. </beans>  
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 事务管理器 对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 dataSource在applicationContext-dao.xml中已经配置 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- aop -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.*.*(..))" />
	</aop:config>
</beans>

      3.4 applicationContext-service.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.     <context:spring-configured />  
  16.     <!--扫描Service的package -->  
  17.     <context:component-scan base-package="com.service" />  
  18.   
  19.   
  20. </beans>  
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<context:spring-configured />
	<!--扫描Service的package -->
	<context:component-scan base-package="com.service" />


</beans>

4、Mybatis相关的配置文件

      4.1 sqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration  
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  5. <configuration>  
  6.   
  7.     <!-- 全局setting配置 ,根据需要再添加 -->  
  8.   
  9.     <!-- 配置别名 -->  
  10.     <typeAliases>  
  11.         <!-- 批量扫描别名 -->  
  12.         <package name="com.entity" />  
  13.     </typeAliases>  
  14.   
  15.     <!-- 配置mapper   
  16.     由于使用spring和mybatis的整合包进行整合,这了无需配置   
  17.     但必须遵循:mapper.java和mapper.xml文件同名且在同一目录 -->  
  18.     <!--<mappers></mappers> -->  
  19. </configu  
<?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>

	<!-- 全局setting配置 ,根据需要再添加 -->

	<!-- 配置别名 -->
	<typeAliases>
		<!-- 批量扫描别名 -->
		<package name="com.entity" />
	</typeAliases>

	<!-- 配置mapper 
	由于使用spring和mybatis的整合包进行整合,这了无需配置 
	但必须遵循:mapper.java和mapper.xml文件同名且在同一目录 -->
	<!--<mappers></mappers> -->
</configu

      4.2 NavMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.   
  6. <!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->  
  7. <mapper namespace="com.mapper.NavMapper">  
  8.   
  9.     <select id="findNavById" parameterType="int" resultType="nav">  
  10.         SELECT  
  11.         *  
  12.         FROM nav WHERE id=#{id}  
  13.     </select>  
  14.   
  15.     <select id="findAll" resultType="nav">  
  16.         SELECT  
  17.         * FROM  
  18.         nav  
  19.     </select>  
  20.   
  21.     <insert id="add" parameterType="nav">  
  22.         insert into nav(name)  
  23.         values(#{name})  
  24.     </insert>  
  25.   
  26.     <update id="update" parameterType="nav">  
  27.         update nav set name=#{name}  
  28.         where id=#{id}  
  29.     </update>  
  30.     <delete id="delete" parameterType="int">  
  31.         delete from nav where id=#{id}  
  32.     </delete>  
  33. </mapper>  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
<mapper namespace="com.mapper.NavMapper">

	<select id="findNavById" parameterType="int" resultType="nav">
		SELECT
		*
		FROM nav WHERE id=#{id}
	</select>

	<select id="findAll" resultType="nav">
		SELECT
		* FROM
		nav
	</select>

	<insert id="add" parameterType="nav">
		insert into nav(name)
		values(#{name})
	</insert>

	<update id="update" parameterType="nav">
		update nav set name=#{name}
		where id=#{id}
	</update>
	<delete id="delete" parameterType="int">
		delete from nav where id=#{id}
	</delete>
</mapper>

      4.3 NavMapper.JavaNavMapper.xml


5、log4j相关的配置文件

  1. # Global logging configuration  
  2. log4j.rootLogger=DEBUG, stdout  
  3. # Console output...  
  4. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  5. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  6. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n  
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# 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
6、关于页面的放置


7、NavController的实现

  1. import java.util.List;  
  2. import java.util.Map;  
  3. import javax.annotation.Resource;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import com.entity.Nav;  
  7. import com.service.NavService;  
  8.   
  9. @Controller  
  10. @RequestMapping("/navController")  
  11. public class NavController {  
  12.     @Resource  
  13.     private NavService navService;  
  14.   
  15.     public void setNavService(NavService navService) {  
  16.         this.navService = navService;  
  17.     }  
  18.   
  19.     @RequestMapping("/jsp")  
  20.     public String toJspPage() {  
  21.         return "index";  
  22.     }  
  23.   
  24.     @RequestMapping("/freemark")  
  25.     public String toFreemarkPage(Map<String, Object> model) {  
  26.         List<Nav> navs = navService.findAll();  
  27.         model.put("navs", navs);  
  28.         return "top";  
  29.     }  
  30. }  
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.entity.Nav;
import com.service.NavService;

@Controller
@RequestMapping("/navController")
public class NavController {
	@Resource
	private NavService navService;

	public void setNavService(NavService navService) {
		this.navService = navService;
	}

	@RequestMapping("/jsp")
	public String toJspPage() {
		return "index";
	}

	@RequestMapping("/freemark")
	public String toFreemarkPage(Map<String, Object> model) {
		List<Nav> navs = navService.findAll();
		model.put("navs", navs);
		return "top";
	}
}

8、访问测试



9、源码下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值