spring JPA struts 整合开发(1) - spring集成JPA
一.springJPAstruts整合开发(1)-spring集成JPA
二.springJPAstruts整合开发(2)-spring集成struts
这里的JPA实现是hibernate。
1.在IDE中新建一个web工程,Resource设置成utf-8
2.导入以下的jar包到/WEB-INF/lib目录下:
Hibernate3安装包下的:
hibernate3.jar
lib\required\*.jar
lib\test\slf4j-log4j12.jar
spring2.5安装包下的:
dist\spring.jar
dist\modules\spring-webmvc-struts.jar
lib\jakarta-commons\common-logging.jarcommons-dbcp.jarcommons-pool.jar
lib\aspectj\aspectjweaver.jaraspectjrt.jar
lib\cglib\cglib-nodep-2.1_3.jar
lib\j2ee\common-annotations.jar
lib\log4j\log4j-1.2.15.jar
下载struts-1.3.8-lib.zip,解压目录下的所有jar
3.spring配置文件:
拷贝一个beans.xml的模板到src目录下,
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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <context:annotation-config/> <!-- JPA entity manager factory bean, like the session factory of hibernate --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <!-- Retrieved the persistence information from persistence.xml --> <property name="persistenceUnitName" value="sparkle" /> </bean> <!-- Transaction manager --> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
4.JPA配置文件
在src目录下创建META-INF子目录并新建persistence.xml文件:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="sparkle" transaction-type="RESOURCE_LOCAL"> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123456"/> <property name="hibernate.max_fetch_depth" value="3"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> </persistence>
5.实体bean
创建com.john.bean.Person类
// It is recommended to let the bean implement Serializable interface and override equals and hashCode methods @Entity public class Person implements { private Integer id; private String name; public Person() { } public Person(String name) { this.name = name; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length=30, nullable=false) public String getName() { return name; } public void setName(String name) { this.name = name; } }
6.业务bean
创建com.john.service.PersonServiceBean类
@Transactional public class PersonSeriveBean { @PersistenceContext EntityManager em; public void save(Person person) { em.persist(person); } public void update(Person person) { em.merge(person); } public void delete(Integer id) { em.remove(em.getReference(Person.class, id)); } @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true) public Person getPerson(Integer id) { return em.find(Person.class, id); } @Override @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true) public List<Person> getPersons() { return em.createQuery("select p FROM Person p").getResultList(); }
抽取PersonService接口。
7.junittest
新建junit.test.PersonServiceTest测试类:
public class PersonServiceTest { private static PersonService personService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); personService = (PersonService)ctx.getBean("personService"); } catch (Exception e) { e.printStackTrace(); } } @Test public void testSave() { personService.save(new Person("John")); } // Other test methods are omitted }
整理自:传智播客spring教程