The Flex, Spring and BlazeDS full stack – Part 2

实现to-do list服务端

 

上篇Part 1部分我们描述了使用flex-mojos构建和配置一个典型的Flex应用模块全过程,在这一部分,在这一部分,我会继续完善的范例项目(to-do list application),并描述如何应用Spring,Hibernate和MySQL创建后端模块。在第三部分我会描述如何使用BlazeDS连接前后端应用。

预备条件
有Spring,Hibernate和MySQL经验,熟悉MySQL的数据库,Java和XML会加快学习步伐。

创建WAR模块
本节我们开始建立示例应用to-do list的后端程序。首先,打开命令行中并定位到第一篇文件建立的todolist目录,运行如下命令:
mvn archetype:create -DgroupId=cn.org.pomer.samples -DartifactId=todolist-web -DarchetypeArtifactId=maven-archetype-webapp

上述命令在根模块todolist中创建一个新的todolist子模块。下一步,编辑新建模块的POM,添加如下的dependencies:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.5.2</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.5.4</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.5.4</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.6.ga</version>
</dependency>

 以上加入的是Spring和Hibernate的依赖包。
最后,更新上面的POM Java编译器plug-in为1.5,添加以下plug-in配置到POM的project/build/plugins节点:

 

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
</plugin>

 

运行命令mvn install安装jar,安装成功后,运行命令mvn eclispe:eclipse为todolist-web构建eclipse工程。构建好eclipse工程后,导入todolist-web项目,配置M2_REPO这个Classpath Variables,配置步骤:window >> preferences >> Java >> Build Path >> Classpath Variables,新建一个 M2_REPO 的变量,变量值指向你系统的Maven2的数据仓库位置。如我的位置是C:/Documents and Settings/HeQing/.m2/repository。

至此,我们已经为进一步开发建立好了服务端模块雏形。接下来,我们建立可以在eclipse环境中为to-do list编写Java代码。

编写java接口
前面,我们建立了to-do list应用模块,现在编写服务接口。在项目路径src/main/java中新建cn.org.pomer.samples.todolist.business包,接建TodoService接口,代码如下:

package cn.org.pomer.samples.todolist.business;
import java.util.List;
import cn.org.pomer.samples.todolist.domain.TodoItem;
public interface TodoService {
  void remove(TodoItem todoItem) throws Exception;
  TodoItem save(TodoItem todoItem) throws Exception;
  TodoItem findById(TodoItem todoItem) throws Exception;
  List<TodoItem> getList() throws Exception;
}

 

TodoService接口实现:

package cn.org.pomer.samples.todolist.business;
import java.util.List;
import cn.org.pomer.samples.todolist.domain.TodoItem;
import cn.org.pomer.samples.todolist.domain.TodoItemRepository;
public class TodoServiceImpl implements TodoService {
  private TodoItemRepository todoItemRepository;
  public void setTodoItemRepository(TodoItemRepository todoItemRepository) {
    this.todoItemRepository = todoItemRepository;
  }
  public TodoItem save(TodoItem item) throws Exception {
    try {
      this.todoItemRepository.save(item);
      return item;
    } catch (Exception e) {
      throw new Exception("Could not save item because: " + e.getCause());
    }
  }
  public void remove(TodoItem item) throws Exception {
    try {
      this.todoItemRepository.remove(item);
    } catch (Exception e) {
      throw new Exception("Could not delete item because " + e.getMessage());
    }
  }
  public TodoItem findById(TodoItem item) throws Exception {
    try {
      return this.todoItemRepository.findById(item);
    } catch (Exception e) {
      throw new Exception("Could not find item because " + e.getMessage());
    }
  }
  public List<TodoItem> getList() throws Exception {
    try {
      return this.todoItemRepository.getList();
    } catch (Exception e) {
      throw new Exception("Could not list items because " + e.getMessage());
    }
  }
}

 

TodoServiceImpl实现访问了包cn.org.pomer.samples.todolist.domain下的TodoItemRepository,以下是其接口代码:

package cn.org.pomer.samples.todolist.domain;
import java.util.List;
public interface TodoItemRepository {
  void remove(TodoItem todoItem);
  TodoItem save(TodoItem todoItem);
  TodoItem findById(TodoItem todoItem) throws Exception;
  List<TodoItem> getList();
}

 

如下是基于spring HibernateDaoSupport的hibernate持久层框架的实现:

package cn.org.pomer.samples.todolist.domain.hibernate;
import java.util.List;
import cn.org.pomer.samples.todolist.domain.TodoItem;
import cn.org.pomer.samples.todolist.domain.TodoItemRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class TodoItemHibernateDao extends HibernateDaoSupport implements TodoItemRepository {
  public TodoItem save(TodoItem todoItem) {
    getHibernateTemplate().saveOrUpdate(todoItem);
    return todoItem;
  }
  public void remove(TodoItem todoItem) {
    getHibernateTemplate().delete(todoItem);
  }
  public TodoItem findById(TodoItem todoItem) throws Exception {
    long id = todoItem.getId();
    todoItem = (TodoItem) getHibernateTemplate().get(TodoItem.class, todoItem.getId());
    if (todoItem == null)
      throw new Exception("Could not find an item with id " + id);
    return todoItem;
  }
  @SuppressWarnings("unchecked")
  public List<TodoItem> getList() {
    return (List<TodoItem>) getHibernateTemplate().loadAll(TodoItem.class);
  }
}

 

最后,建立以上用到的TodoItem POJO:

package cn.org.pomer.samples.todolist.domain;
public class TodoItem {
  private long id;
  private String title;
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
}

 

下一章节,我们将为to-do list建立MySQL数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值