高手们 电影天堂:Spring3.0MVC和Hibernate基于annotation注解的整合

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 16:32:21
springmvc和hibernate的annotation集合:
首先web.xml
Xml代码  
  1.  version="1.0" encoding="UTF-8"?>  
  2.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   hibernateAspringmvc  
  4.     
  5.         contextConfigLocation  
  6.         classpath*:applicationContext*.xml  
  7.       
  8.       
  9.         org.springframework.web.context.ContextLoaderListener  
  10.       
  11.   
  12.       
  13.           
  14.         spring  
  15.         org.springframework.web.servlet.DispatcherServlet  
  16.         1  
  17.       
  18.   
  19.       
  20.         spring  
  21.         *.xl  
  22.       
  23.     
  24.     index.html  
  25.     index.htm  
  26.     index.jsp  
  27.     default.html  
  28.     default.htm  
  29.     default.jsp  
  30.     
  31.   


然后是applicationContext.xml
Xml代码  
  1.  version="1.0" encoding="UTF-8"?>  
  2.  xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans        
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        
  8.     http://www.springframework.org/schema/context        
  9.     http://www.springframework.org/schema/context/spring-context-3.0.xsd        
  10.     http://www.springframework.org/schema/tx        
  11.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd        
  12.     http://www.springframework.org/schema/jdbc        
  13.     http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"   
  14.     default-autowire="byName" default-lazy-init="true">  
  15.       
  16.      base-package="org.xlaohe1" />  
  17.      id="dataSource"  
  18.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  19.          name="driverClassName" value="com.mysql.jdbc.Driver" />  
  20.          name="url" value="jdbc:mysql://localhost:3306/test" />  
  21.          name="username" value="root" />  
  22.          name="password" value="root" />  
  23.       
  24.      id="sessionFactory"  
  25.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  26.          name="dataSource" ref="dataSource" />  
  27.             
  28.                   
  29.          name="namingStrategy">  
  30.              class="org.hibernate.cfg.ImprovedNamingStrategy" />  
  31.           
  32.          name="annotatedClasses">  
  33.               
  34.                 org.xlaohe1.model.User  
  35.               
  36.           
  37.                   
  38.          name="hibernateProperties">  
  39.               
  40.                  key="hibernate.dialect">org.hibernate.dialect.MySQLDialect  
  41.                  key="hibernate.show_sql">false  
  42.                  key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider  
  43.                  key="hibernate.cache.use_query_cache">false  
  44.                  key="hibernate.jdbc.batch_size">50  
  45.                  key="hibernate.cache.use_second_level_cache">false  
  46.               
  47.           
  48.                   
  49.                   
  50.          name="packagesToScan" value="org.xlaohe1.model" />  
  51.                    
  52.       
  53.   
  54.      id="transactionManager"  
  55.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  56.          name="sessionFactory" ref="sessionFactory" />  
  57.       
  58.      transaction-manager="transactionManager" />  
  59.   
  60.     


spring-serlvet.xml
Xml代码  
  1.  version="1.0" encoding="UTF-8"?>  
  2.  xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"    
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  8.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  9.       http://www.springframework.org/schema/context     
  10.       http://www.springframework.org/schema/context/spring-context.xsd     
  11.       http://www.springframework.org/schema/mvc     
  12.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.          
  14.      base-package="org.xlaohe1.web"/>     
  15.      
  16.     

    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 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 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 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.