●pom.xml
・struts関連を設定
・hibernateとJPA関連を設定
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>local.my</groupId>
<artifactId>Struts2Sample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2Sample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>7.0.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.166</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<!-- AOP dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>Struts2Sample</finalName>
</build>
</project>
●web.xml
・struts2のservletを設定。
・Springコンテナの設定
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<!-- Springコンテナ用の設定ファイルパス定義-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<filter>
<!-- struts2サーブレット -->
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- struts2サーブレットの配置 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<!-- Springコンテナ用のリスナー -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
●applicationContext-JPA.xml
・Springコンテナのbeanを設定
<?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"
>
<!-- Intercepterから使用するJPAファクトリクラス -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Struts2Sample" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- JPAの例外をSpringの例外に変換してくれる -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- PersistenceContextアノテーションにEntityManagerをインジェクションしてくれる -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- @Repositry・@Serviceを検索するパス -->
<context:component-scan base-package="local.my.dao" />
<!-- <context:annotation-config></context:annotation-config> -->
<!-- アノテーションベースのトランザクション制御 -->
<tx:annotation-driven />
</beans>
●persistence.xml
JPAとHibernateとの連携
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="Struts2Sample">
<class>local.my.entity.UserEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:/home/????????/h2db"/>
<property name="hibernate.connection.username" value="myh2db"/>
<property name="hibernate.connection.password" value="myh2db"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
●struts.xml
・struts2とspringコンテナの連携
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.devMode" value="true"/>
</struts>
●UserEntity.java
package local.my.entity;
import java.io.Serializable;
/**
* Entity implementation class for Entity: UserEntity
*
*/
@Entity
@Table(name="User")
public class UserEntity implements Serializable {
@Id
@Column(length=10)
private String userId;
private int age;
@Column(length=20)
private String userName;
@Column(length=20)
private String password;
private static final long serialVersionUID = 1L;
public UserEntity() {
}
public UserEntity(String userId) {
super();
this.userId = userId;
}
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
●UserEntityManager.java
・@PersistenceContextでEntityManagerをSpringコンテナから注入
・@RepositoryでSpringコンテナに登録、使用する側は、@AutoWiredで注入する。
package local.my.dao;
import java.util.ArrayList;
@Repository
public class UserEntitiyManager {
@PersistenceContext
private EntityManager em;
/**
* @param em the em to set
*/
public void setEm(EntityManager em) {
this.em = em;
}
@Transactional
public void add(UserEntity e)
{
em.persist(e);
}
private Query getUsers()
{
return em.createNativeQuery("select * from User", UserEntity.class);
}
private Query getUser(String userId)
{
return em.createNativeQuery("select * from user where userID = :userId").setParameter("userId", userId);
}
//@Transactional(readOnly=true)
public boolean isLogin(String userId, String password)
{
boolean isLoginOk = false;
//前提
assert(userId!=null);
assert(password!=null);
Query q = getUser(userId);
try
{
UserEntity u = (UserEntity) q.getSingleResult();
if(password.equals(u.getPassword()))
{
isLoginOk = true;
}
}
finally
{
}
return isLoginOk;
}
@Transactional
public String[] getList()
{
Collection<UserEntity> l= getUsers().getResultList();
ArrayList<String> c = new ArrayList<String>();
for(UserEntity u : l)
{
c.add(u.getUserId() + ":" + u.getUserName());
}
return c.toArray(new String[0]);
}
}
[0回]
PR