1、概述
本文旨在搭建Spring MVC+Hibernate开发框架,通过一个简单的demo讲解Spring MVC的相关配置文件,以及通过注解方式实现简单功能。
开发框架:Spring+Spring MVC+Hibernate(Spring所用的版本为3.0.5)。
数据库:MySQL(数据库名称test,demo工程所用的表名为user_info)。
2、开发框架搭建
2.1 创建工程
在Eclipse的Java EE版本或MyEclipse中创建一个Dynamic Web Project。并创建如下包:
(1)com.dao:系统的DAO;
(2)com.model:表的实体类(使用Hibernate),在该工程中不配置.hbm.xml映射文件,采取注解的方式;
(3)com.service:业务逻辑接口类和实现类;
(4)com.web:Spring MVC的Controllor类;
(5)com.config:Spring和Spring MVC的配置文件。
创建成功后包结构如下所示:
springmvctest
src
----com
----amigo
----dao
----model
----service
----web
----config
WebContent
----META-INF
----WEB-INF
----lib
----classes
2.2 引入相关包
需要将Spring、Spring MVC、Hibernate、MySQL驱动、log4j、c3p0数据源等的相关包引入。lib目录下的jar包如下:
antlr-2.7.6.jar
aopalliance.jar
asm-attrs.jar
asm.jar
c3p0-0.9.0.jar
cglib-2.1.3.jar
commons-beanutils-1.8.0.jar
commons-beanutils-bean-collections-1.8.0.jar
commons-betwixt-0.8.jar
commons-collections-2.1.1.jar
commons-digester-2.1.jar
commons-discovery-0.2.jar
commons-httpclient.jar
commons-logging.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-validator.jar
hibernate3.jar
jaas.jar
javassist.jar
jaxen-1.1-beta-7.jar
jaxrpc.jar
jboss-archive-browsing.jar
jdbc2_0-stdext.jar
jta.jar
log4j-1.2.11.jar
mysql-connector-java-5.0.4-bin.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.instrument-3.0.5.RELEASE.jar
org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.jms-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.oxm-3.0.5.RELEASE.jar
org.springframework.test-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
saaj.jar
wsdl4j.jar
xerces-2.6.2.jar
xml-apis.jar
2.3 配置文件
2.3.1 配置web.xml
在web.xml中需要配置Spring的配置文件(applicationContext.xml)和Spring MVC配置文件(spring-mvc.xml),配置指定所有.do的请求都由Spring的DispatcherServlet类进行处理。
web.xml文件的参考配置如下:
01 | <?xml version= " 1.0 " encoding= " UTF-8 " ?> |
04 | <display - name> springmvctest </display - name> |
05 | <welcome - file - list> |
06 | <welcome - file> index.html </welcome - file> |
07 | <welcome - file> index.htm </welcome - file> |
08 | <welcome - file> index.jsp </welcome - file> |
09 | </welcome - file - list> |
11 | <param - name> contextConfigLocation </param - name> |
12 | <param - value> classpath:config / applicationContext.xml </param - value> |
15 | <listener - class > org.springframework.web.context.ContextLoaderListener |
19 | <servlet - name> spring - mvc </servlet - name> |
20 | <servlet - class > org.springframework.web.servlet.DispatcherServlet </servlet - class > |
22 | <param - name> contextConfigLocation </param - name> |
23 | <param - value> classpath:config / spring - mvc.xml </param - value> |
25 | <load - on - startup> </load - on - startup> |
28 | <servlet - name> spring - mvc </servlet - name> |
29 | <url - pattern>* . do </url - pattern> |
32 | <filter - name> encodingFilter </filter - name> |
33 | <filter - class > org.springframework.web.filter.CharacterEncodingFilter </filter - class > |
35 | <param - name> encoding </param - name> |
36 | <param - value> UTF - </param - value> |
39 | <param - name> forceEncoding </param - name> |
40 | <param - value> true </param - value> |
44 | <filter - name> encodingFilter </filter - name> |
45 | <url - pattern> /*</url-pattern> |
2.3.2 配置 spring 的配置文件
Spring的配置文件applicationContext.xml文件中主要配置对Hibernate的事务的管理,该配置文件的参考配置如下:
01 | <?xml version= " 1.0 " encoding= " UTF-8 " ?> |
15 | <context:annotation - config /> |
17 | <!-- 扫描annotation类,过滤Service,Repository --> |
18 | <context:component - scan base - package = " com.amigo " > |
19 | <context:include - filter type= " annotation " expression= " org.springframework.stereotype.Service " /> |
20 | <context:include - filter type= " annotation " expression= " org.springframework.stereotype.Repository " /> |
21 | </context:component - scan> |
23 | <bean id= " dataSource " class = " com.mchange.v2.c3p0.ComboPooledDataSource " destroy - method= " close " > |
24 | <property name= " driverClass " > |
25 | <value> com.mysql.jdbc.Driver </value> |
27 | <property name= " jdbcUrl " > |
30 | <property name= " user " > |
33 | <property name= " password " > |
34 | <value> 123456 </value> |
36 | <property name= " maxPoolSize " > |
39 | <property name= " minPoolSize " > |
42 | <property name= " initialPoolSize " > |
45 | <property name= " maxIdleTime " > |
49 | <bean id= " sessionFactory " class = " org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean " > |
50 | <property name= " dataSource " ref= " dataSource " /> |
51 | <property name= " packagesToScan " value= " com.amigo.model* " ></property> |
52 | <property name= " hibernateProperties " > |
54 | <prop key= " hibernate.dialect " > org.hibernate.dialect.MySQLDialect </prop> |
55 | <prop key= " show_sql " > true </prop> |
56 | <prop key= " hibernate.jdbc.batch_size " > </prop> |
61 | <!-- 不破坏数据库,注册SessionFactory --> |
62 | <bean id= " transactionManager " class = " org.springframework.orm.hibernate3.HibernateTransactionManager " > |
63 | <property name= " sessionFactory " ref= " sessionFactory " ></property> |
65 | <bean id= " transactionInterceptor " |
66 | class = " org.springframework.transaction.interceptor.TransactionInterceptor " > |
67 | <property name= " transactionManager " ref= " transactionManager " ></property> |
68 | <property name= " transactionAttributes " > |
70 | <prop key= " save* " > PROPAGATION_REQUIRED </prop> |
71 | <prop key= " update* " > PROPAGATION_REQUIRED </prop> |
72 | <prop key= " delete* " > PROPAGATION_REQUIRED </prop> |
73 | <prop key= " find* " > PROPAGATION_REQUIRED </prop> |
74 | <prop key= " get* " > PROPAGATION_REQUIRED </prop> |
75 | <prop key= " execute* " > PROPAGATION_REQUIRED </prop> |
76 | <prop key= " load* " > PROPAGATION_REQUIRED </prop> |
77 | <prop key= " merge* " > PROPAGATION_REQUIRED </prop> |
78 | <prop key= " add* " > PROPAGATION_REQUIRED </prop> |
83 | class = " org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator " > |
84 | <property name= " beanNames " > |
86 | <value>* Service </value> |
89 | <property name= " interceptorNames " > |
91 | <value> transactionInterceptor </value> |
2.3.3 配置 Spring MVC 配置文件
Spring MVC的配置文件spring-mvc.xml中主要是Controller的配置信息,该文件的参考配置如下:
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
11 | default -lazy-init= "true" > |
12 | <context:annotation-config /> |
13 | <!-- 使Spring支持自动检测组件,如注解的Controller --> |
14 | <context:component-scan base- package = "com.amigo.web" /> |
16 | <bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" |
20 | <bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> |
21 | <!-- 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 --> |
22 | <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > |
23 | <property name= "messageConverters" > |
25 | <bean class = "org.springframework.http.converter.StringHttpMessageConverter" > |
2.4 创建数据库和表
创建test数据库和user_info表的SQL语句如下(为了简便,user_info只有一个USER_NAME字段):
3 | CREATE TABLE user_info ( |
4 | USER_NAME varchar ( 32 ) NOT NULL , |
5 | PRIMARY KEY ( USER_NAME ) |
6 | ) ENGINE=InnoDB DEFAULT CHARSET= utf8; |
3、实例代码
3.1 DAO层
BaseHibernateDao类的代码如下所示:
01 | package com.amigo.dao; |
03 | import javax.annotation.Resource; |
05 | import org.hibernate.HibernateException; |
06 | import org.hibernate.Session; |
07 | import org.hibernate.SessionFactory; |
08 | import org.springframework.dao.DataAccessException; |
09 | import org.springframework.dao.DataAccessResourceFailureException; |
10 | import org.springframework.dao.support.DaoSupport; |
11 | import org.springframework.orm.hibernate3.HibernateTemplate; |
12 | import org.springframework.orm.hibernate3.SessionFactoryUtils; |
14 | public class BaseHibernateDao extends DaoSupport { |
15 | private SessionFactory sessionFactory; |
16 | private HibernateTemplate hibernateTemplate; |
17 | public SessionFactory getSessionFactory() { |
18 | return sessionFactory; |
21 | @Resource (name= "sessionFactory" ) |
22 | public void setSessionFactory(SessionFactory sessionFactory) { |
23 | this .sessionFactory=sessionFactory; |
24 | this .hibernateTemplate=createHibernateTemplate(sessionFactory); |
27 | public Session getSession() { |
28 | if ( this .sessionFactory== null ) { |
29 | throw new HibernateException( "Session Create Fail,SessionFactory is null!" ); |
31 | return this .sessionFactory.getCurrentSession(); |
34 | protected HibernateTemplate createHibernateTemplate( |
35 | SessionFactory sessionFactory) { |
36 | return new HibernateTemplate(sessionFactory); |
40 | protected void checkDaoConfig() throws IllegalArgumentException { |
41 | if ( this .hibernateTemplate== null ) { |
42 | throw new IllegalArgumentException( "'sessionFactory' or 'hibernateTemplate' is required" ); |
46 | protected final Session getSession( boolean allowCreate) |
47 | throws DataAccessResourceFailureException, IllegalStateException { |
48 | return (!allowCreate ? SessionFactoryUtils.getSession( |
49 | getSessionFactory(), false ) : SessionFactoryUtils.getSession( |
52 | this .hibernateTemplate.getEntityInterceptor(), this .hibernateTemplate.getJdbcExceptionTranslator())); |
55 | protected final DataAccessException convertHibernateAccessException( |
56 | HibernateException ex) { |
57 | return this .hibernateTemplate.convertHibernateAccessException(ex); |
60 | protected final void releaseSession(Session session) { |
61 | SessionFactoryUtils.releaseSession(session, getSessionFactory()); |
62 | if ( null !=session)session= null ; |
65 | public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) { |
66 | this .hibernateTemplate=hibernateTemplate; |
69 | public final HibernateTemplate getHibernateTemplate() { |
70 | return this .hibernateTemplate; |
USER_INFO表的Dao类UserInfoDao类的代码如下所示:
2 | import org.springframework.stereotype.Repository; |
5 | public class UserInfoDao extends BaseHibernateDao { |
3.2 业务逻辑层
接口类IHelloService的代码如下:
1 | package com.amigo.service; |
2 | public interface IHelloService { |
3 | public int addUser(String userName) throws Exception;; |
实现类HelloService类的代码如下:
01 | package com.amigo.service; |
02 | import javax.annotation.Resource; |
04 | import org.apache.commons.logging.Log; |
05 | import org.apache.commons.logging.LogFactory; |
06 | import org.springframework.stereotype.Repository; |
07 | import org.springframework.stereotype.Service; |
09 | import com.amigo.dao.UserInfoDao; |
10 | import com.amigo.model.UserInfo; |
12 | @Service ( " helloService " ) |
14 | public class HelloService implements IHelloService ; |
3.3 控制层
控制类HelloControllor类接收userName参数,并调用相应的Service类将用户名保存到USER_INFO表中,该类的代码如下:
01 | package com.amigo.web; |
03 | import javax.annotation.Resource; |
04 | import javax.servlet.http.HttpServletRequest; |
05 | import javax.servlet.http.HttpServletResponse; |
07 | import org.apache.commons.logging.Log; |
08 | import org.apache.commons.logging.LogFactory; |
09 | import org.springframework.stereotype.Controller; |
10 | import org.springframework.web.bind.annotation.RequestMapping; |
11 | import org.springframework.web.bind.annotation.ResponseBody; |
12 | import com.amigo.service.IHelloService; |
15 | @RequestMapping ( " /test " ) |
16 | public class HelloControllor { |
17 | private static final Log log=LogFactory.getLog(HelloControllor. class ); |
19 | private IHelloService helloService; |
21 | public IHelloService getHelloService() { |
26 | public void setHelloService(IHelloService helloService) { |
27 | this .helloService=helloService; |
30 | @RequestMapping ( "/hello.do" ) |
32 | String sayHello(HttpServletRequest request, HttpServletResponse response) throws Exception { |
33 | request.setCharacterEncoding( "UTF-8" ); |
34 | String userName=request.getParameter( "userName" ); |
35 | log.info( "userName=" + userName); |
36 | int resultCode=helloService.addUser(userName); |
37 | String rspInfo= "你好!" + userName + ",操作结果码=" + resultCode; |
38 | response.setHeader( "Content-type" , "text/html;charset=UTF-8" ); |
39 | response.getOutputStream().write(rspInfo.getBytes( "UTF-8" )); |
@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径;如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问路径;
4、测试
测试时可以通过访问http://localhost:8080/springmvctest/test/hello.do?userName=amigo777,通过userName参数将用户名添加到USER_INFO表中。
从实例代码可以看出,POJO、DAO层、Service层和Controller层都是采用注解的方式将service、dao注入的,减少了配置量,方便了开发工作。