JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Application Server » GAE  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 Google App Engine 整合 Spring 及 JPA
J2





發文: 42
積分: 6
於 2010-07-26 14:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
STEP 1. 環境準備
1. 先準備AppEngine SDK1.3.5版以上
2. 本範例是用Spring 2.5, 建議是用一樣的版本, 比較不會有問題,下載之後把下面這些JAR檔, 放到Lib裡
1
2
3
4
5
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
commons-logging-1.0.4.jar
spring.jar


STEP 2. 建立Entity, DAO, Servlet
1. 建立本範例中的Entity: fattom.riddle.entity.User
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package fattom.riddle.entity;
 
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
 
@Entity
public class User implements Serializable{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}


2.建立DAO: fattom.riddle.dao.UserDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package fattom.riddle.dao;
 
import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import fattom.riddle.entity.User;
 
public class UserDao extends JpaDaoSupport {
 
  public void persist(Object user) {
    this.getJpaTemplate().persist(user);
  }
  
  public void remove(Object user) {
    User result = this.getJpaTemplate().find(User.class, ((User)user).getId());
    if(result != null)
      this.getJpaTemplate().remove(result);
  }
  @SuppressWarnings("unchecked")
  public void removeAll() {
    List<User> result = this.getJpaTemplate().find("select e from fattom.riddle.entity.User e");
    for(User user:result) {
      this.getJpaTemplate().remove(user);
    }
  }
 
  @SuppressWarnings("unchecked")
  public List<User> findAll() {
    List<User> result = this.getJpaTemplate().find("select e from fattom.riddle.entity.User e");
    result.size();
    return result; 
    }
}


3. 建立Servlet: fattom.riddle.TestAction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package fattom.riddle;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import fattom.riddle.dao.UserDao;
import fattom.riddle.entity.User;
 
@SuppressWarnings("serial")
public class TestAction extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
 
    PrintWriter out = response.getWriter();
    response.setContentType("text/plain");
 
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    UserDao userDao = (UserDao) context.getBean("userDao");
 
    if (request.getParameter("name") != null) {
      User tmpUser = new User();
      tmpUser.setName(request.getParameter("name"));
      userDao.persist(tmpUser);
      
    }
 
    List<User> users = userDao.findAll();
    for (User user : users) {
      out.println(user.getId() + "/" + user.getName());
    }
    
    if (request.getParameter("action") != null && "delete".equals(request.getParameter("action"))) {
      for (User user : users) {
        userDao.remove(user);
      }
    }
 
    out.println("Hello, GAE and Spring world");
  }
}


STEP 3. 設定相關XML
1. META-INF/persistence.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">
 
    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
 
</persistence>


2. WEB-INF/applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" >
        <property name="persistenceUnitName" value="transactions-optional"/>
    </bean> 
    
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
 
   <aop:config proxy-target-class="true"> 
        <aop:advisor pointcut="execution(* fattom.riddle..dao.*.*(..))" advice-ref="txAdvice"/>
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="*" propagation="REQUIRED" /> 
        </tx:attributes>
    </tx:advice>
  
    <bean id="userDao" class="fattom.riddle.dao.UserDao" scope="singleton">
    </bean>
</beans>


3. WEB-INF/web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  <servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>fattom.riddle.TestAction</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


最後, 再寫個 index.html 就可以測試看看有沒有成功囉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Hello App Engine</title>
  </head>
 
  <body>
    <h1>Hello App Engine!</h1>
  <form action="test" method="get">
    add new user, name = <input type="text" name="name"></input>
    <input type="submit" value="OK"></input>
  </form>
  <br>
    <a href="./test?action=delete">delete all users</a> 
  </br>
  </body>
</html>


reply to postreply to post
作者 Re:Google App Engine 整合 Spring 及 JPA [Re:J2]
J2





發文: 42
積分: 6
於 2010-07-27 16:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
附上
Source Code下載連結
原始BLOG文章連結(排版比較醜..@@||| )

--
不會在blogger貼美美的code, 所以請大家直接看這邊的文章就好


reply to postreply to post
» JWorld@TW »  Application Server » GAE

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8