一. spring JPA struts 整合开发(1) - spring集成JPA
[url="http://czj4451.iteye.com/blog/1535749"]二. spring JPA struts 整合开发(2) - spring集成struts[/url]
这里的JPA实现是hibernate。
[size=medium]1. 在IDE中新建一个web工程,Resource设置成utf-8[/size]
[size=medium]2. 导入以下的jar包到/WEB-INF/lib目录下:[/size]
Hibernate 3安装包下的:
hibernate3.jar
lib\required\*.jar
lib\test\slf4j-log4j12.jar
spring 2.5 安装包下的:
dist\spring.jar
dist\modules\spring-webmvc-struts.jar
lib\jakarta-commons\common-logging.jar commons-dbcp.jar commons-pool.jar
lib\aspectj\aspectjweaver.jar aspectjrt.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
[size=medium]3. spring配置文件:[/size]
拷贝一个beans.xml的模板到src目录下,
beans.xml内容:
[size=medium]4. JPA配置文件[/size]
在src目录下创建META-INF子目录并新建persistence.xml文件:
[size=medium]5. 实体bean[/size]
创建com.john.bean.Person类
[size=medium]6. 业务bean[/size]
创建com.john.service.PersonServiceBean类
抽取PersonService接口。
[size=medium]7. junit test[/size]
新建junit.test.PersonServiceTest测试类:
整理自:传智播客spring教程
[url="http://czj4451.iteye.com/blog/1535749"]二. spring JPA struts 整合开发(2) - spring集成struts[/url]
这里的JPA实现是hibernate。
[size=medium]1. 在IDE中新建一个web工程,Resource设置成utf-8[/size]
[size=medium]2. 导入以下的jar包到/WEB-INF/lib目录下:[/size]
Hibernate 3安装包下的:
hibernate3.jar
lib\required\*.jar
lib\test\slf4j-log4j12.jar
spring 2.5 安装包下的:
dist\spring.jar
dist\modules\spring-webmvc-struts.jar
lib\jakarta-commons\common-logging.jar commons-dbcp.jar commons-pool.jar
lib\aspectj\aspectjweaver.jar aspectjrt.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
[size=medium]3. spring配置文件:[/size]
拷贝一个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>
[size=medium]4. JPA配置文件[/size]
在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>
[size=medium]5. 实体bean[/size]
创建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;
}
}
[size=medium]6. 业务bean[/size]
创建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接口。
[size=medium]7. junit test[/size]
新建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教程