分类:
2009-04-21 10:48:51
1. 一个集成spring+hibernate的简单的Flex工程
2. 从数据库查找指定的用户
=============================================
DB.sql
CREATE TABLE `user` (
`UserID` int(11) NOT NULL auto_increment,
`UserName` varchar(20) default NULL,
`Password` varchar(20) default NULL,
`Email` varchar(20) default NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
=============================================
remoting-config.xml:
==============================================================
services-config.xml:
==============================================================
web.xml: 和struts整合spring一样
============================================================
applicationContext.xml
org.hibernate.dialect.MySQLDialect
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
lazy-init="default" autowire="default" dependency-check="default">
============================================================
SpringFactory.java文件
package flex.samples.factories;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
public class SpringFactory implements FlexFactory
{
private static final String SOURCE = "source";
public void initialize(String id, ConfigMap configMap) {}
public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
{
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
}
public Object lookup(FactoryInstance inst)
{
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
return factoryInstance.lookup();
}
static class SpringFactoryInstance extends FactoryInstance
{
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
{
super(factory, id, properties);
}
public String toString()
{
return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
}
public Object lookup()
{
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try
{
return appContext.getBean(beanName);
}
catch (NoSuchBeanDefinitionException nexc)
{
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
catch (BeansException bexc)
{
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName + "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
}
=============================================
User.java
package com.jy.vo;
// default package
/**
* User entity.
*
* @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer userId;
private String userName;
private String password;
private String email;
// Constructors
/** default constructor */
public User() {
}
/** full constructor */
public User(String userName, String password, String email) {
this.userName = userName;
this.password = password;
this.email = email;
}
// Property accessors
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
=============================================
User.hbm.xml
"">
=============================================
UserDAO.java
package com.jy.vo;
// default package
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* A data access object (DAO) providing persistence and search support for User
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see .User
* @author MyEclipse Persistence Tools
*/
public class UserDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UserDAO.class);
// property constants
public static final String USER_NAME = "userName";
public static final String PASSWORD = "password";
public static final String EMAIL = "email";
protected void initDao() {
// do nothing
}
public List
List
return list;
}
public void save(User transientInstance) {
log.debug("saving User instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByUserName(Object userName) {
return findByProperty(USER_NAME, userName);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findAll() {
log.debug("finding all User instances");
try {
String queryString = "from User";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserDAO) ctx.getBean("UserDAO");
}
}
=============================================
IUserServiceImpl.java
package com.jy.imp;
import java.util.List;
import com.jy.vo.UserDAO;
import com.jy.vo.User;
public class IUserServiceImpl {
private UserDAO userDAO;
/*通过ID搜索返回一个User对象
* @param id 需要查找的用户ID
* @return User对象
*
*/
public User getUser(Integer userId){
List
if(list.isEmpty() || list.size() < 1)
return null;
return (User) list.get(0);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
=============================================
HelloWorld2.java
package com.jy.test;
import com.jy.imp.IUserServiceImpl;
import com.jy.vo.User;
public class HelloWorld2 {
private IUserServiceImpl userService;
/*
* 该方法通过用户ID返回用户名
* @param id 用记ID
* @return String “你好 ”+用户名
*/
public String sayHelloTo(String userId){
System.out.println("Hello " + userId);
User user = userService.getUser(Integer.parseInt(userId));
if(user == null)
return "no user";
return "Hello "+user.getUserName();
}
public IUserServiceImpl getUserService() {
return userService;
}
public void setUserService(IUserServiceImpl userService) {
this.userService = userService;
}
}
=============================================
MyFirstFSH2.mxml:
import mx.rpc.events.ResultEvent;
function setTextArea(evnet:ResultEvent):void{
var ff:String = evnet.result as String;
textArea.text = ff;
}
function remotingSayHello():void{
var sname:String = nameInput.text;
hello2.sayHelloTo(sname);
}
]]>
=============================================