分享

Spring3.0MVC和Hibernate基于annotation注解的整合

 狼志凌云 2011-10-08
springmvc和hibernate的annotation集合:
首先web.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns="http://java./xml/ns/javaee" xmlns:web="http://java./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   <display-name>hibernateAspringmvc</display-name>  
  4.   <context-param>  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>classpath*:applicationContext*.xml</param-value>  
  7.     </context-param>  
  8.     <listener>  
  9.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  10.     </listener>  
  11.   
  12.     <servlet>  
  13.         <!-- this is 'spring' name for your spring-servlet.xml -->  
  14.         <servlet-name>spring</servlet-name>  
  15.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  16.         <load-on-startup>1</load-on-startup>  
  17.     </servlet>  
  18.   
  19.     <servlet-mapping>  
  20.         <servlet-name>spring</servlet-name>  
  21.         <url-pattern>*.xl</url-pattern>  
  22.     </servlet-mapping>  
  23.   <welcome-file-list>  
  24.     <welcome-file>index.html</welcome-file>  
  25.     <welcome-file>index.htm</welcome-file>  
  26.     <welcome-file>index.jsp</welcome-file>  
  27.     <welcome-file>default.html</welcome-file>  
  28.     <welcome-file>default.htm</welcome-file>  
  29.     <welcome-file>default.jsp</welcome-file>  
  30.   </welcome-file-list>  
  31. </web-app>  


然后是applicationContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context"  
  4.     xmlns:tx="http://www./schema/tx" xmlns:jdbc="http://www./schema/jdbc"  
  5.     xmlns:p="http://www./schema/p"  
  6.     xsi:schemaLocation="http://www./schema/beans        
  7.     http://www./schema/beans/spring-beans-3.0.xsd        
  8.     http://www./schema/context        
  9.     http://www./schema/context/spring-context-3.0.xsd        
  10.     http://www./schema/tx        
  11.     http://www./schema/tx/spring-tx-3.0.xsd        
  12.     http://www./schema/jdbc        
  13.     http://www./schema/jdbc/spring-jdbc-3.0.xsd"   
  14.     default-autowire="byName" default-lazy-init="true">  
  15.     <!-- this pack must include xxx-servlet.xml's pack. -->  
  16.     <context:component-scan base-package="org.xlaohe1" />  
  17.     <bean id="dataSource"  
  18.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  19.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  20.         <property name="url" value="jdbc:mysql://localhost:3306/test" />  
  21.         <property name="username" value="root" />  
  22.         <property name="password" value="root" />  
  23.     </bean>  
  24.     <bean id="sessionFactory"  
  25.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  26.         <property name="dataSource" ref="dataSource" />  
  27.           <!-- 这几句在spring hibernate的注解整合中可以不需要 因为下面的2就是扫描指定路劲下的实体进行映射 -->  
  28.                 <!-- 1======================= -->  
  29.         <property name="namingStrategy">  
  30.             <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />  
  31.         </property>  
  32.         <property name="annotatedClasses"><!-- the must have. before this is mapping,now is entity -->  
  33.             <list>  
  34.                 <value>org.xlaohe1.model.User</value>  
  35.             </list>  
  36.         </property>  
  37.                 <!-- 1======================= -->  
  38.         <property name="hibernateProperties">  
  39.             <props>  
  40.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  41.                 <prop key="hibernate.show_sql">false</prop>  
  42.                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  
  43.                 <prop key="hibernate.cache.use_query_cache">false</prop>  
  44.                 <prop key="hibernate.jdbc.batch_size">50</prop>  
  45.                 <prop key="hibernate.cache.use_second_level_cache">false</prop>  
  46.             </props>  
  47.         </property>  
  48.                 <!-- 2======================= -->  
  49.                 <!-- 自动扫描指定位置下的实体文件进行映射 -->  
  50.         <property name="packagesToScan" value="org.xlaohe1.model" />  
  51.                  <!-- 2======================= -->  
  52.     </bean>  
  53.   
  54.     <bean id="transactionManager"  
  55.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  56.         <property name="sessionFactory" ref="sessionFactory" />  
  57.     </bean>  
  58.     <tx:annotation-driven transaction-manager="transactionManager" />  
  59.   
  60. </beans>    


spring-serlvet.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:context="http://www./schema/context"    
  4.     xmlns:p="http://www./schema/p"  
  5.     xmlns:mvc="http://www./schema/mvc"    
  6.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  7.     xsi:schemaLocation="http://www./schema/beans     
  8.       http://www./schema/beans/spring-beans-3.0.xsd     
  9.       http://www./schema/context     
  10.       http://www./schema/context/spring-context.xsd     
  11.       http://www./schema/mvc     
  12.       http://www./schema/mvc/spring-mvc-3.0.xsd">  
  13.     <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->     
  14.     <context:component-scan base-package="org.xlaohe1.web"/>     
  15.      
  16.     <!--启动Spring MVC的注解功能,完成请求和注解POJO的映射     
  17.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  -->  
  18.     <mvc:annotation-driven/>  
  19.      
  20.     <!--  对模型视图名称的解析,即在模型视图名称添加前后缀      
  21.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"      
  22.         p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>-->     
  23.              
  24.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">     
  25.             <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>     
  26.             <property name="prefix" value="/WEB-INF/jsp/"/>     
  27.             <property name="suffix" value=".jsp"/>     
  28.         </bean>     
  29.      
  30.      
  31.          
  32. </beans>    


entity:
Java代码 复制代码 收藏代码
  1. package org.xlaohe1.model;   
  2.   
  3. import javax.persistence.Column;   
  4. import javax.persistence.Entity;   
  5. import javax.persistence.GeneratedValue;   
  6. import javax.persistence.GenerationType;   
  7. import javax.persistence.Id;   
  8. import javax.persistence.Table;   
  9.   
  10.   
  11. @Entity  
  12. @Table(name = "users", catalog = "test")   
  13. public class User {   
  14.     private Integer id;   
  15.     private String username;   
  16.     private String password;   
  17.     private Integer age;   
  18.        
  19.     public User() {   
  20.         super();   
  21.     }   
  22.     @Id  
  23.     @GeneratedValue(strategy=GenerationType.AUTO)   
  24.     @Column(name = "id")   
  25.     public Integer getId() {   
  26.         return id;   
  27.     }   
  28.     public void setId(Integer id) {   
  29.         this.id = id;   
  30.     }   
  31.        
  32.     @Column(name = "username")   
  33.     public String getUsername() {   
  34.         return username;   
  35.     }   
  36.     public void setUsername(String username) {   
  37.         this.username = username;   
  38.     }   
  39.        
  40.     @Column(name = "password")   
  41.     public String getPassword() {   
  42.         return password;   
  43.     }   
  44.     public void setPassword(String password) {   
  45.         this.password = password;   
  46.     }   
  47.        
  48.     @Column(name = "age")   
  49.     public Integer getAge() {   
  50.         return age;   
  51.     }   
  52.     public void setAge(Integer age) {   
  53.         this.age = age;   
  54.     }   
  55.        
  56.        
  57. }  


userdaoimpl
Java代码 复制代码 收藏代码
  1. package org.xlaohe1.dao.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  6. import org.springframework.stereotype.Repository;   
  7. import org.xlaohe1.dao.IUserDao;   
  8. import org.xlaohe1.model.User;   
  9.   
  10. @Repository  
  11. public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {   
  12.   
  13.     @SuppressWarnings("unchecked")   
  14.     @Override  
  15.     public List<User> findAllUsers() {   
  16.         String hql = "FROM User";   
  17.         return getHibernateTemplate().find(hql);   
  18.     }   
  19.   
  20. }  


userserviceimpl
Java代码 复制代码 收藏代码
  1. package org.xlaohe1.service.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;   
  6. import org.springframework.stereotype.Service;   
  7. import org.springframework.transaction.annotation.Transactional;   
  8. import org.xlaohe1.dao.IUserDao;   
  9. import org.xlaohe1.model.User;   
  10. import org.xlaohe1.service.IUserService;   
  11.   
  12. @Service @Transactional  
  13. public class UserServiceImpl implements IUserService {   
  14.        
  15.     @Autowired IUserDao userDao;   
  16.   
  17.     @Override  
  18.     //@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)   
  19.     public List<User> findAllUsers() {   
  20.         return userDao.findAllUsers();   
  21.     }   
  22. }  


usercontroller
Java代码 复制代码 收藏代码
  1. package org.xlaohe1.web;   
  2.   
  3. import java.util.List;   
  4.   
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;   
  9. import org.springframework.stereotype.Controller;   
  10. import org.springframework.ui.ModelMap;   
  11. import org.springframework.web.bind.annotation.RequestMapping;   
  12. import org.springframework.web.servlet.ModelAndView;   
  13. import org.xlaohe1.model.User;   
  14. import org.xlaohe1.service.IUserService;   
  15.   
  16. @Controller  
  17. public class UserController {   
  18.     @Autowired  
  19.     IUserService userService;   
  20.   
  21.     public UserController() {   
  22.     }   
  23.   
  24.     @RequestMapping(value = "/show")   
  25.     public ModelAndView myMethod(HttpServletRequest request,   
  26.             HttpServletResponse response, ModelMap modelMap) throws Exception {   
  27.         List<User> ulst = userService.findAllUsers();   
  28.         modelMap.put("users", ulst);   
  29.         return new ModelAndView("showUser", modelMap);   
  30.   
  31.     }   
  32.        
  33.     @RequestMapping(value = "/t")   
  34.     public ModelAndView t() {   
  35.         return new ModelAndView("t");   
  36.     }   
  37.   
  38. }   
  39.    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约