Spring基于HibernateTransactionManager的事物管理

Dao:

        package com.test.dao;

        import com.test.model.Customer;

        public interface UserDao {
 

                       public void Add(Customer c);
          }

DaoImpl:                 

           package com.test.daoImpl;

             import org.hibernate.HibernateException;
            import org.hibernate.classic.Session;
            import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
            import com.test.dao.UserDao;
            import com.test.model.Customer;

            public class UserDaoImpl extends HibernateDaoSupport  implements UserDao    {
 
                     public void Add(Customer c){
                                   Session session = null;
                                   try {
                                        getHibernateTemplate().save(c);
                                   } catch (HibernateException e) {
                                        // TODO Auto-generated catch block
                                      e.printStackTrace();
                        }
             }
        }

Service:

                package com.test.service;

                import com.test.dao.UserDao;

                public class AddService {
                         private UserDao userDao;

                         public UserDao getUserDao() {
                                   return userDao;
                         }

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

Test:

                   package com.test.service;

                  import org.springframework.context.support.ClassPathXmlApplicationContext;

                  import com.test.dao.UserDao;
                  import com.test.daoImpl.UserDaoImpl;
                 import com.test.model.Customer;

                 public class AddServiceTest {
                             public static void main(String args[]){
                                              ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  
                                                UserDao impl = (UserDaoImpl)ctx.getBean("addservice");
  
                                                 Customer c = new Customer();
                                                 c.setName("注释换");
                                                 c.setPassword("zhushihuan");
                                                 impl.Add(c);
  
 }
}

ApplicationContext.xml:Spring基于HibernateTransactionManager的事物管理_第1张图片

Spring基于HibernateTransactionManager的事物管理_第2张图片

 

你可能感兴趣的:(DAO,spring,session,Class,import,interface)