ITKeyword,专注技术干货聚合推荐

注册 | 登录

spring mvc系列文章 - springmvc spring mybatis ibatis freemark整合开发(1.0版)

sl0007 分享于 2012-08-09

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

基于注解的话,配置文件就简单多了,只需一个核心配置文件即可。 1 applicationContext.xml <xml version="1.0" encoding="UTF-8"> <beans xmlns="http://ww

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

采用maven管理项目,eclipse开发:


1 采用的目录结构



2 所需的所有相关jar

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>51auto</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	
	
	<dependencies>
		
		<!-- start spring 核心依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId> 
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency> 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency> 
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- end spring 核心依赖包 -->
		
		<!-- start spring mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency> 
		<!-- end spring mvc -->
		  
		  
		<!-- start spring 集成Ibatis -->
		<!--
			<dependency>
				<groupId>cglib</groupId>
				<artifactId>cglib-nodep</artifactId>
				<version>2.2</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-orm</artifactId>
				<version>3.0.5.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				<version>3.0.5.RELEASE</version>
			</dependency> 
			<dependency>
				<groupId>org.apache.ibatis</groupId>
				<artifactId>ibatis-sqlmap</artifactId>
				<version>2.3.4.726</version>
			</dependency>
			<dependency>
				<groupId>org.aspectj</groupId>
				<artifactId>aspectjrt</artifactId>
				<version>1.6.8</version>
			</dependency>
			<dependency>
				<groupId>org.aspectj</groupId>
				<artifactId>aspectjweaver</artifactId>
				<version>1.6.8</version>
			</dependency>
			<dependency>
				<groupId>commons-dbcp</groupId>
				<artifactId>commons-dbcp</artifactId>
				<version>1.3</version>
			</dependency> 	
			-->	
		<!-- end spring 集成Ibatis -->
		
		<!-- start spring 集成Mybatis -->
		<dependency>
		  <groupId>org.mybatis</groupId>
		  <artifactId>mybatis-spring</artifactId>
		  <version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.9</version>
		</dependency>
		<!-- end spring 集成Mybatis -->
		
		<!-- start web工程依赖包 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- end web工程依赖包 -->
		
		<!-- start spring集成freemark -->
		<dependency>
		  <groupId>org.freemarker</groupId>
		  <artifactId>freemarker</artifactId>
		  <version>2.3.16</version>
		</dependency>
		<!-- end spring集成freemarker -->
		
	</dependencies>
	
	
	<build>
		<finalName>springmvc</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.25</version>
				<configuration>
					<reload>manual</reload>
					<scanIntervalSeconds>0</scanIntervalSeconds>
					<contextPath>/</contextPath>
					<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
					<connectors>
						<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
							<port>8080</port>
							<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
					<encoding>UTF-8</encoding>
					<!-- <failOnError>false</failOnError> -->
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


3 web.xml

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

<web-app 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 web-app_2_5.xsd"
	version="2.5">
	<display-name>Archetype Created Web Application</display-name>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/applicationContext-*.xml
		</param-value>
	</context-param>
	
	<!-- -->
	<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>
	
	<!-- -->   
	<servlet>    
	    <servlet-name>springmvc</servlet-name>    
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
	    <load-on-startup>1</load-on-startup>    
	</servlet>  
	  
	<servlet-mapping>    
	    <servlet-name>springmvc</servlet-name>    
	    <url-pattern>*.do</url-pattern>  
	</servlet-mapping>  

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<listener>   
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
	</listener> 

	<!--<error-page>
		<error-code>500</error-code>
		<location>/500.jsp</location>
	</error-page>

	<error-page>
		<error-code>404</error-code>
		<location>/404.jsp</location>
	</error-page>
	-->
</web-app>



4 springmvc-servlet.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">  
     
    <!-- 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="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>  
    
</beans>  


5 mybatis-config.xml

<?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>  
        <typeAlias alias="Student" type="com.springmvc.domain.Student"/>  
    </typeAliases>  
      
    <mappers>  
        <mapper resource="mybatis_sqlmap/StudentMapper.xml"/>  
    </mappers>  
</configuration> 


6 applicationContext-common.xml

[xhtml]  view plain copy print ?
  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"    
  4.       xmlns:aop="http://www.springframework.org/schema/aop"        
  5.       xmlns:tx="http://www.springframework.org/schema/tx"         
  6.       xmlns:jdbc="http://www.springframework.org/schema/jdbc"         
  7.       xmlns:context="http://www.springframework.org/schema/context"        
  8.       xsi:schemaLocation="         
  9.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         
  10.       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         
  11.       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd         
  12.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         
  13.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"        
  14.      default-autowire="byName">  
  15.   
  16.     <context:property-placeholder location="classpath:oracle.properties"/>  
  17.       
  18.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  19.         <property name="driverClassName" value="${driver}"/>  
  20.         <property name="url" value="${url}"/>  
  21.         <property name="username" value="${username}"/>  
  22.         <property name="password" value="${password}"/>  
  23.     </bean>  
  24.       
  25.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  26.         <property name="dataSource" ref="dataSource" />  
  27.     </bean>  
  28.       
  29.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  30.         <property name="configLocation" value="classpath:mybatis-config.xml" />  
  31.         <property name="dataSource" ref="dataSource" />  
  32.     </bean>  
  33. </beans>  
 

 

7 applicationContext-beans.xml

[xhtml]  view plain copy print ?
  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"    
  4.       xmlns:aop="http://www.springframework.org/schema/aop"        
  5.       xmlns:tx="http://www.springframework.org/schema/tx"         
  6.       xmlns:jdbc="http://www.springframework.org/schema/jdbc"         
  7.       xmlns:context="http://www.springframework.org/schema/context"        
  8.       xsi:schemaLocation="         
  9.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         
  10.       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         
  11.       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd         
  12.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         
  13.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"        
  14.      default-autowire="byName">  
  15.        
  16.     <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  17.         <property name="mapperInterface" value="com.test.dao.StudentMapper"/>  
  18.         <property name="sqlSessionFactory" ref="sqlSessionFactory"/>        
  19.     </bean>  
  20.       
  21.     <bean id="studentService" class="com.test.service.impl.StudentServiceImpl">  
  22.         <property name="studentMapper" ref="studentMapper"/>  
  23.     </bean>  
  24. </beans>  
 

 

8 applicationContext-actions.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 name="studentController" class="com.springmvc.action.StudentController">  
		<property name="studentService" ref="studentService"/>  
	</bean>  
    
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
		<property name="mappings">  
			<props>  
				<prop key="/studentController.do">studentController</prop>  
			</props>  
		</property>  
	</bean>  
</beans>  



9 Student.java

[java]  view plain copy print ?
  1. package com.test.bean;  
  2.   
  3. public class Student {  
  4.     private int id;  
  5.     private String name;  
  6.   
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.   
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.   
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22. }  
 

 

10 StudentMapper.java

[java]  view plain copy print ?
  1. package com.test.dao;  
  2.   
  3. import com.test.bean.Student;  
  4.   
  5. public interface StudentMapper {  
  6.     public Student queryStudentById(int id);  
  7. }  
 

 

11 StudentMapper.xml

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="com.test.dao.StudentMapper">  
  4.     <select id="queryStudentById" parameterType="int" resultType="Student">  
  5.         select s_id id,s_name name from student where s_id = #{id}  
  6.     </select>  
  7. </mapper>  
 

 

12 StudentService.java

[java]  view plain copy print ?
  1. package com.test.service;  
  2.   
  3. import com.test.bean.Student;  
  4.   
  5. public interface StudentService {  
  6.     public Student queryStudentById(int id);  
  7. }  
 

 

13 StudentServiceImpl.java

[java]  view plain copy print ?
  1. package com.test.service.impl;  
  2.   
  3. import com.test.bean.Student;  
  4. import com.test.dao.StudentMapper;  
  5. import com.test.service.StudentService;  
  6.   
  7. public class StudentServiceImpl implements StudentService {  
  8.     private StudentMapper studentMapper = null;  
  9.   
  10.     public void setStudentMapper(StudentMapper studentMapper) {  
  11.         this.studentMapper = studentMapper;  
  12.     }  
  13.   
  14.     public Student queryStudentById(int id) {  
  15.         return studentMapper.queryStudentById(id);  
  16.     }  
  17.   
  18. }  
 

 14 StudentMapper.xml

<?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>  
        <typeAlias alias="Student" type="com.springmvc.domain.Student"/>  
    </typeAliases>  
      
    <mappers>  
        <mapper resource="mybatis_sqlmap/StudentMapper.xml"/>  
    </mappers>  
</configuration> 


15 StudentCntroller

[java]  view plain copy print ?
  1. package com.test.web;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.springframework.web.servlet.ModelAndView;  
  7. import org.springframework.web.servlet.mvc.Controller;  
  8.   
  9. import com.test.bean.Student;  
  10. import com.test.service.StudentService;  
  11.   
  12. public class StudentController implements Controller {  
  13.     private StudentService studentService = null;  
  14.   
  15.     public void setStudentService(StudentService studentService) {  
  16.         this.studentService = studentService;  
  17.     }  
  18.   
  19.     public ModelAndView handleRequest(HttpServletRequest request,  
  20.             HttpServletResponse response) throws Exception {  
  21.         Student student = studentService.queryStudentById(100);  
  22.         String name = student.getName();  
  23.         Student s = new Student();  
  24.         s.setName(name);  
  25.         return new ModelAndView("test", "student", s);  
  26.     }  
  27. }  
  

 

 16 index.jsp

[xhtml]  view plain copy print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title></title>  
  7. </head>  
  8. <body>  
  9.     <form action="studentController.do" method="post">  
  10.         <input type="submit" value="Query"/>  
  11.     </form>  
  12. </body>  
  13. </html>  

 

17 test.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title></title>  
</head>  
<body>  
    freemarker:${student.name}  ${student.id}
</body>  
</html>  


 

 主体类使用的:http://blog.csdn.net/geloin/article/details/7536968,此文对配置文件做了适当的改进!


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

推荐:整合Spring MVC,mybatis,hibernate,freemarker框架实现的自定义注解Validator验证机制实现对敏感词过滤的代码分享

整合Spring MVC,mybatis,hibernate,freemarker框架实现的自定义注解Validator验证机制实现对敏感词过滤的代码分享 博客分类: java spring hibernate 自定义注

采用maven管理项目,eclipse开发: 1 采用的目录结构 2 所需的所有相关jar pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。