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

阅读更多
springmvc和hibernate的annotation集合:
首先web.xml


  hibernateAspringmvc
  
		contextConfigLocation
		classpath*:applicationContext*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

	
		
		spring
		org.springframework.web.servlet.DispatcherServlet
		1
	

	
		spring
		*.xl
	
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  


然后是applicationContext.xml


	
	
	
		
		
		
		
	
	
		
          
                
		
			
		
		
			
				org.xlaohe1.model.User
			
		
                
		
			
				org.hibernate.dialect.MySQLDialect
				false
				org.hibernate.cache.EhCacheProvider
				false
				50
				false
			
		
                
                
		
                 
	

	
		
	
	

  


spring-serlvet.xml


      
      
  
    
    
  
      
          
          
              
              
              
          
  
  
      
  


entity:
package org.xlaohe1.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "users", catalog = "test")
public class User {
	private Integer id;
	private String username;
	private String password;
	private Integer age;
	
	public User() {
		super();
	}
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id")
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(name = "username")
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "password")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name = "age")
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	
}


userdaoimpl
package org.xlaohe1.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.xlaohe1.dao.IUserDao;
import org.xlaohe1.model.User;

@Repository
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

	@SuppressWarnings("unchecked")
	@Override
	public List findAllUsers() {
		String hql = "FROM User";
		return getHibernateTemplate().find(hql);
	}

}


userserviceimpl
package org.xlaohe1.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xlaohe1.dao.IUserDao;
import org.xlaohe1.model.User;
import org.xlaohe1.service.IUserService;

@Service @Transactional
public class UserServiceImpl implements IUserService {
	
	@Autowired IUserDao userDao;

	@Override
	//@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
	public List findAllUsers() {
		return userDao.findAllUsers();
	}
}


usercontroller
package org.xlaohe1.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.xlaohe1.model.User;
import org.xlaohe1.service.IUserService;

@Controller
public class UserController {
	@Autowired
	IUserService userService;

	public UserController() {
	}

	@RequestMapping(value = "/show")
	public ModelAndView myMethod(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		List ulst = userService.findAllUsers();
		modelMap.put("users", ulst);
		return new ModelAndView("showUser", modelMap);

	}
	
	@RequestMapping(value = "/t")
	public ModelAndView t() {
		return new ModelAndView("t");
	}

}
 





有意见和建议请留下..

你可能感兴趣的:(spring,hibernate,springmvc,annotation)