spring2.5+hibernate基于xml配置的实例

spring+hibernate有基于标签的,有基于xml配置的。现在一般来说都用基于标签的,xml的已经不流行了,但好不容易看到这里,还是记录下来,万一以后需要呢

1、web.xml 



 
        dispatcherServlet
        
        
            org.springframework.web.servlet.DispatcherServlet 
        
        
        
            contextConfigLocation
            /WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml
        
        1
    
    
    
        dispatcherServlet
        *.do
    


2、hib-config.xml




   
	
	
			
	  
              
              
              
              
            
      

     
             
                 
           
             
                 
               	
                     
                       org.hibernate.dialect.MySQLDialect  
                     
                   true
                   update
               
           
           
		
			com.sxt.po
		
     


	


  
   
        
  




	


 

	 
	 
 
 
	 
		 
		 
		     
	 
 


3、dao-config.xml



	
	
	  
	
	
	


4、service-config.xml



    
	
		
	
	

5、web-confg.xml



	
	
    
        
        
    
  
   
    
        
        
        
    


    
        
            
                userController  
            
        
    


	





6、UserController.java

package com.sxt.action;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sxt.service.UserService;

public class UserController implements Controller {

	private UserService userService;
	
	
	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		System.out.println("HelloController.handleRequest()");
		//返回给页面的数据
		req.setAttribute("a", "aaaa");
		
		userService.add(req.getParameter("uname")); 
		return new ModelAndView("index");
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	
}

7、UserDao.java

package com.sxt.dao;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sxt.po.User;

public class UserDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public void add(User u){
		System.out.println("UserDao.add()");
		hibernateTemplate.save(u);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
}

8、User.java

package com.sxt.po;

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

@Entity
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String uname;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
}


9、UserService.java

package com.sxt.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sxt.dao.UserDao;
import com.sxt.po.User;

public class UserService {
	
	private UserDao userDao;
	
	public void add(String uname){
		System.out.println("UserService.add()");
		User u = new User();
		u.setUname(uname);
		userDao.add(u);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}

10、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
		

${requestScope.a}



11、reg.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'reg.jsp' starting page
    
	
	
	    
	
	
  
  
  
	
用户名:


需要用到的jar包

spring2.5+hibernate基于xml配置的实例_第1张图片

数据库是mysql数据库,表名如下:

CREATE TABLE `user` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `uname` varchar(24) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


注意下面两个错误

org.hibernate.HibernateException: The database returned no natively generated identity value
数据库返回的没有本地生成的标识值的错误。

alter table t_news change  id id  int not null  auto_increment;


java.sql.SQLException: Field 'id' doesn't have a default value

ID的自增长没有加上去,加上就OK了


示例下载:http://download.csdn.net/detail/chexitianxia/9574046

你可能感兴趣的:(spring)