UserDAO.java
package com.bebig.dao;

import com.bebig.model.User;

public interface UserDAO {
    
public void save(User u);
}

UserDAOImpl.java
package com.bebig.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.bebig.dao.UserDAO;
import com.bebig.model.User;

@Repository
public class UserDAOImpl implements UserDAO {
    
//由Spring框架来注入
    private SessionFactory sessionFactory;

    
public SessionFactory getSessionFactory() {
        
return sessionFactory;
    }


    @Resource
    
public void setSessionFactory(SessionFactory sessionFactory) {
        
this.sessionFactory = sessionFactory;
    }


    @Override
    
public void save(User u) {
        Session s 
= sessionFactory.getCurrentSession();
        s.beginTransaction();
        s.save(u);
        s.getTransaction().commit();
        
        System.out.println(
"a user saved!");
    }


}

User.java
package com.bebig.model;

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

@Entity
@Table(name 
= "tab_User")
public class User {
    
private int id;
    
private String name;

    @Id
    @GeneratedValue
    
public int getId() {
        
return id;
    }


    
public void setId(int id) {
        
this.id = id;
    }


    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
    }

}

UserService.java
package com.bebig.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.bebig.dao.UserDAO;
import com.bebig.model.User;

@Service(
"userService")
public class UserService {
    
private UserDAO userDAO;

    
public void add(User u) {
        userDAO.save(u);

    }


    @Resource
    
public void setUserDAO(UserDAO userDAO) {
        
this.userDAO = userDAO;
    }


    
public UserDAO getUserDAO() {
        
return userDAO;
    }


    
public void init() {
        System.out.println(
"init.");
    }


    
public void destory() {
        System.out.println(
"destory.");
    }

}

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="
http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>

    
<!-- a service object; we will be profiling its methods -->
    
<context:annotation-config />
    
<context:component-scan base-package="com.bebig" />

    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method
="close">
        
<property name="driverClassName" value="${jdbc.driverClassName}" />
        
<property name="url" value="${jdbc.url}" />
        
<property name="username" value="${jdbc.username}" />
        
<property name="password" value="${jdbc.password}" />
    
</bean>
    
<context:property-placeholder location="classpath:jdbc.properties" />

    
<!-- 让Spring帮我们注入SessionFactory -->
    
<bean id="sessionFactory"
        class
="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        
<property name="dataSource" ref="dataSource" />
        
<property name="annotatedClasses"><!-- 将实体类注入annotatedClasses属性 -->
            
<list>
                
<value>com.bebig.model.User</value>
            
</list>
        
</property>
        
<!-- hibernate的一些配置 -->
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                
<prop key="hibernate.show_sql">true</prop>
                
<prop key="hibernate.format_sql">true</prop>
                
<prop key="hibernate.current_session_context_class">thread</prop>
            
</props>
        
</property>
    
</bean>

</beans>
jdbc.properties
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url
=jdbc:sqlserver://localhost:1433;DatabaseName=spring
jdbc.username=sa
jdbc.password
=******
测试用例:
UserServiceTest.java
package com.bebig.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bebig.model.User;

public class UserServiceTest {

    @Test
    
public void testAdd() throws Exception {
        ClassPathXmlApplicationContext cxt 
= new ClassPathXmlApplicationContext(
                
"beans.xml");
        UserService service 
= (UserService) cxt.getBean("userService");
        User u 
= new User();
        u.setName(
"Test");
        service.add(u);
    }


}

注:把Hibernate要用到的JAR包加进来。