daoimpl类package com.jj.spring.dao.impl;import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import javax.annotation.Resource;
import javax.sql.DataSource;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.jj.spring.dao.IUserDAO;
import com.jj.spring.model.User;
import com.jj.spring.util.HibernateUtil;@Component("u")public class UserDAOimpl implements IUserDAO{
    private SessionFactory sessionfactory;
public UserDAOimpl() {

}
public SessionFactory getSessionfactory() {
return sessionfactory;
}
   @Resource(name="factory")
public void setSessionfactory(SessionFactory sessionfactory) {
this.sessionfactory = sessionfactory;
} @Override
public void add(User u) throws Exception {
   Session session=sessionfactory.getCurrentSession();

 session.save(u);
} @Override
public void delete() throws Exception {
// TODO Auto-generated method stub

} @Override
public List selectAll() throws Exception {
// TODO Auto-generated method stub
List<User> users=new ArrayList<User>();

return users;
} @Override
public void selectById(int id) throws Exception {
// TODO Auto-generated method stub

}}
userservice类package com.jj.spring.dao.service;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import com.jj.spring.dao.IUserDAO;
import com.jj.spring.model.User;
@Component("userservice")
public class UserService implements IUserDAO{ private IUserDAO userdao;
public UserService() {
// TODO Auto-generated constructor stub
} public IUserDAO getUserdao() {
return userdao;
}
@Resource(name="u")
public void setUserdao(IUserDAO userdao) {
this.userdao = userdao;
}

@Transactional
public void add(User u) throws Exception {
userdao.add(u);

} @Override
public void delete() throws Exception {
// TODO Auto-generated method stub

} @Override
public List selectAll() throws Exception {
  return userdao.selectAll();

} @Override
public void selectById(int id) throws Exception {
// TODO Auto-generated method stub

}}
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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
             http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.jj"></context:component-scan>
    
     
    <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="jdbc.properties"/>
    
   <bean id="factory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
      <list>
        <value>com.jj.spring.model.User</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.format_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
      
      
      </props>
    </property>
  </bean><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="factory" />
  
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
 <!--  more bean definitions go here --></beans>
jdbc。property配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hibernate
jdbc.username=root
jdbc.password=123
测试类
package com.jj.spring.test;import static org.junit.Assert.*;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jj.spring.dao.service.UserService;
import com.jj.spring.model.User;public class ServiceTest { @Test
public void test() {
ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");
UserService userservice=(UserService)app.getBean("userservice");
User u=new User();
u.setName("jj");
u.setAge(44);
try {
userservice.add(u);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
我用的是hibernate3.6.10和spring3.1.1