打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring MVC注解配置结合Hibernate的入门教程及其代码实例

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 " ?>
02 
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>
10 <context - param>
11 <param - name> contextConfigLocation </param - name>
12 <param - value> classpath:config / applicationContext.xml </param - value>
13 </context - param>
14 <listener>
15 <listener - class> org.springframework.web.context.ContextLoaderListener
16 </listener - class>
17 </listener>
18 <servlet>
19 <servlet - name> spring - mvc </servlet - name>
20 <servlet - class> org.springframework.web.servlet.DispatcherServlet </servlet - class>
21 <init - param>
22 <param - name> contextConfigLocation </param - name>
23 <param - value> classpath:config / spring - mvc.xml </param - value>
24 </init - param>
25 <load - on - startup> </load - on - startup>
26 </servlet>
27 <servlet - mapping>
28 <servlet - name> spring - mvc </servlet - name>
29 <url - pattern>* . do </url - pattern>
30 </servlet - mapping>
31 <filter>
32 <filter - name> encodingFilter </filter - name>
33 <filter - class> org.springframework.web.filter.CharacterEncodingFilter </filter - class>
34 <init - param>
35 <param - name> encoding </param - name>
36 <param - value> UTF - </param - value>
37 </init - param>
38 <init - param>
39 <param - name> forceEncoding </param - name>
40 <param - value> true </param - value>
41 </init - param>
42 </filter>
43 <filter - mapping>
44 <filter - name> encodingFilter </filter - name>
45 <url - pattern> /**/ /*</url-pattern>
46 </filter-mapping>
47</web-app>

2.3.2 配置 spring 的配置文件

Spring的配置文件applicationContext.xml文件中主要配置对Hibernate的事务的管理,该配置文件的参考配置如下:

01<?xml version=" 1.0 " encoding=" UTF-8 " ?>
06 xsi:schemaLocation="
07 http: // www.springframework.org/schema/beans
08 http: // www.springframework.org/schema/beans/spring-beans-3.0.xsd
09 http: // www.springframework.org/schema/context
10 http: // www.springframework.org/schema/context/spring-context-3.0.xsd
11 http: // www.springframework.org/schema/aop
12 http: // www.springframework.org/schema/aop/spring-aop-3.0.xsd
13 http: // www.springframework.org/schema/task
14 http: // www.springframework.org/schema/task/spring-task-3.0.xsd">
15 <context:annotation - config />
16 
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>
22 
23 <bean id=" dataSource " class=" com.mchange.v2.c3p0.ComboPooledDataSource " destroy - method=" close ">
24 <property name=" driverClass ">
25 <value> com.mysql.jdbc.Driver </value>
26 </property>
27 <property name=" jdbcUrl ">
28 <value> jdbc:mysql: // localhost/test</value>
29 </property>
30 <property name=" user ">
31 <value> root </value>
32 </property>
33 <property name=" password ">
34 <value> 123456 </value>
35 </property>
36 <property name=" maxPoolSize ">
37 <value> </value>
38 </property>
39 <property name=" minPoolSize ">
40 <value> </value>
41 </property>
42 <property name=" initialPoolSize ">
43 <value> </value>
44 </property>
45 <property name=" maxIdleTime ">
46 <value> </value>
47 </property>
48 </bean>
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 ">
53 <props>
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>
57 </props>
58 </property>
59 </bean>
60 
61 <!-- 不破坏数据库,注册SessionFactory -->
62 <bean id=" transactionManager " class=" org.springframework.orm.hibernate3.HibernateTransactionManager ">
63 <property name=" sessionFactory " ref=" sessionFactory "></property>
64 </bean>
65 <bean id=" transactionInterceptor "
66 class=" org.springframework.transaction.interceptor.TransactionInterceptor ">
67 <property name=" transactionManager " ref=" transactionManager "></property>
68 <property name=" transactionAttributes ">
69 <props>
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>
79 </props>
80 </property>
81 </bean>
82 <bean
83 class=" org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator ">
84 <property name=" beanNames ">
85 <list>
86 <value>* Service </value>
87 </list>
88 </property>
89 <property name= " interceptorNames ">
90 <list>
91 <value> transactionInterceptor </value>
92 </list>
93 </property>
94 </bean>
95 </beans>

2.3.3 配置 Spring MVC 配置文件

Spring MVC的配置文件spring-mvc.xml中主要是Controller的配置信息,该文件的参考配置如下:

01<?xml version="1.0"encoding="UTF-8"?>
06 xsi:schemaLocation="
07http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
09http://www.springframework.org/schema/mvc
10http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
11 default-lazy-init="true">
12 <context:annotation-config />
13 <!-- 使Spring支持自动检测组件,如注解的Controller -->
14 <context:component-scan base-package="com.amigo.web"/>
15 
16 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
17 p:prefix="/WEB-INF"
18 p:suffix=".jsp" />
19  
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">
24 <list>
25 <bean class="org.springframework.http.converter.StringHttpMessageConverter">
26 </bean>
27 </list>
28 </property>
29 </bean>
30 </beans>

2.4 创建数据库和表

创建test数据库和user_info表的SQL语句如下(为了简便,user_info只有一个USER_NAME字段):

1CREATE DATABASE test;
2 USER test;
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类的代码如下所示:

01package com.amigo.dao;
02 
03 import javax.annotation.Resource;
04 
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;
13 
14 public class BaseHibernateDao extends DaoSupport {
15 private SessionFactory sessionFactory;
16 private HibernateTemplate hibernateTemplate;
17 public SessionFactory getSessionFactory() {
18 return sessionFactory;
19 }
20 
21 @Resource(name="sessionFactory")
22 public void setSessionFactory(SessionFactory sessionFactory) {
23 this.sessionFactory=sessionFactory;
24 this.hibernateTemplate=createHibernateTemplate(sessionFactory);
25 }
26 
27 public Session getSession() {
28 if (this.sessionFactory==null) {
29 throw new HibernateException("Session Create Fail,SessionFactory is null!");
30 }
31 return this.sessionFactory.getCurrentSession();
32 }
33 
34 protected HibernateTemplate createHibernateTemplate(
35 SessionFactory sessionFactory) {
36 return new HibernateTemplate(sessionFactory);
37 }
38 
39 @Override
40 protected void checkDaoConfig() throws IllegalArgumentException {
41 if (this.hibernateTemplate==null) {
42 throw new IllegalArgumentException("'sessionFactory' or 'hibernateTemplate' is required");
43 }
44 }
45  
46 protected final Session getSession(boolean allowCreate)
47 throws DataAccessResourceFailureException, IllegalStateException {
48 return (!allowCreate ? SessionFactoryUtils.getSession(
49 getSessionFactory(), false) : SessionFactoryUtils.getSession(
50 getSessionFactory(),
51 
52 this.hibernateTemplate.getEntityInterceptor(), this.hibernateTemplate.getJdbcExceptionTranslator()));
53 }
54 
55 protected final DataAccessException convertHibernateAccessException(
56 HibernateException ex) {
57 return this.hibernateTemplate.convertHibernateAccessException(ex);
58 }
59 
60 protected final void releaseSession(Session session) {
61 SessionFactoryUtils.releaseSession(session, getSessionFactory());
62 if(null!=session)session=null;
63 }
64 
65 public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
66 this.hibernateTemplate=hibernateTemplate;
67 }
68 
69 public final HibernateTemplate getHibernateTemplate() {
70 return this.hibernateTemplate;
71 }
72}

USER_INFO表的Dao类UserInfoDao类的代码如下所示:

1package com.amigo.dao;
2 import org.springframework.stereotype.Repository;
3 
4@Repository
5 public class UserInfoDao extends BaseHibernateDao {
6}

3.2 业务逻辑层

接口类IHelloService的代码如下:

1package com.amigo.service;
2 public interface IHelloService {
3 public int addUser(String userName) throws Exception;;
4}

实现类HelloService类的代码如下:

01package com.amigo.service;
02 import javax.annotation.Resource;
03 
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;
08 
09 import com.amigo.dao.UserInfoDao;
10 import com.amigo.model.UserInfo;
11 
12@Service( " helloService " )
13@Repository
14 public class HelloService implements IHelloService ;
15 }
16}

3.3 控制层

   控制类HelloControllor类接收userName参数,并调用相应的Service类将用户名保存到USER_INFO表中,该类的代码如下:

01package com.amigo.web;
02 
03 import javax.annotation.Resource;
04 import javax.servlet.http.HttpServletRequest;
05 import javax.servlet.http.HttpServletResponse;
06 
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;
13 
14@Controller
15@RequestMapping( " /test " )
16 public class HelloControllor {
17 private static final Log log=LogFactory.getLog(HelloControllor.class);
18 
19 private IHelloService helloService;
20 
21 public IHelloService getHelloService() {
22 return helloService;
23 }
24 
25 @Resource
26 public void setHelloService(IHelloService helloService) {
27 this.helloService=helloService;
28 }
29 
30 @RequestMapping("/hello.do")
31 public @ResponseBody
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"));
40 return "";
41 }
42}

@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径;如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问路径;

4、测试

测试时可以通过访问http://localhost:8080/springmvctest/test/hello.do?userName=amigo777,通过userName参数将用户名添加到USER_INFO表中。

从实例代码可以看出,POJO、DAO层、Service层和Controller层都是采用注解的方式将service、dao注入的,减少了配置量,方便了开发工作。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Spring3整合Hibernate3
hibernate在spring中的使用
在Spring+hebernate中 Cannot get a connection, pool error Timeout waiting for idle object异常的解决办法
Spring数据库访问之ORM(二)
Spring整合Hibernate图文步骤
HibernateTemplate的使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服