先写上jpa实现,以后补上Hibernate实现,并加以对比
package junit.test;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import javax.sql.DataSource;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itcast.product.ProductType;import com.itcast.service.product.ProductService;import com.itcast.service.product.impl.ProductServiceBean;public class TestProduct { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void testrun(){ /*jpa实现存储*/// EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");// EntityManager em = factory.createEntityManager();// em.getTransaction().begin();// em.persist(new ProductType());// em.getTransaction().commit();// em.close();// factory.close(); /*通过Spring注入的jpa实现*/ ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); ProductService ps = (ProductService)cxt.getBean("productServiceBean"); ps.save(new ProductType()); }}
相关资源:Spring2.5+struts1.2+JPA集成