Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part3 ----add spring & hibernate
1. 在WEB-INF\lib 下添加 spring 、 hibernate 常用jar,包括mysql数据库的连接驱动
2.在mysql数据库中创建数据库并添加测试数据
语句如下:
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(11) NOT NULL default '0',
`password` varchar(11) NOT NULL default '0',
`age` int(11) default '0',
`email` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT;
INSERT INTO `user` VALUES (1,'lww','1234',22,'[email protected]');
INSERT INTO `user` VALUES (2,'xyp','1234',22,'[email protected]');
3.在src 创建以下文件
其中HibernateDao.java package com.core.hibernate; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.criterion.Criterion; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.core.utils.ReflectUtils; /** * 扩展HibernateDaoSupport的泛型基类 * @param <T> 实体类型 */ @Repository public class HibernateDao<T> extends HibernateDaoSupport{ protected Class<T> entityClass; /** * 在构造函数中利用反射机制获得参数T的具体类 */ public HibernateDao() { entityClass = ReflectUtils.getClassGenricType(getClass()); } /** * 根据实体类与ID获得对象 * @param clazz 实体类 * @param id 主键ID */ public <X> X get(final Class<X> clazz,final Serializable id) { return (X) getSession().get(clazz, id); } /** * 根据id获得对象 * @param id 主键ID */ public T get(Serializable id){ return get(entityClass, id); } /** * 删除对象 * @param entity 实体类 */ public void delete(final Object entity) { getSession().delete(entity); } /** * 根据ID删除对象 * @param id 主键ID */ public void delete(final Serializable id) { delete(get(id)); } /** * 根据实体类与ID删除对象 * @param clazz 实体类 * @param id 主键ID */ public void delete(final Class clazz, final Serializable id){ delete(get(clazz,id)); } /** * 保存对象 * @param entity 保存的实体对象 */ public void save(final Object entity) { getSession().saveOrUpdate(entity); } /** * 获取所有数据 * @param entityClass 参数T的反射类型 */ public <X> List<X> getAll(final Class<X> entityClass) { return createCriteria(entityClass).list(); } /** * 获取所有数据 */ public List<T> getAll() { return query(); } /** * 根据条件获取数据 * @param criterions 数量可变的Criterion */ public List<T> query(final Criterion... criterions) { return createCriteria(criterions).list(); } /** * HQL方式查询 * @param hql 符合HQL语法的查询语句 * @param values 数量可变的条件值,按顺序绑定 */ public Query createQuery(final String hql,final Object... values){ Query query = getSession().createQuery(hql); int j = values.length; for(int i = 0; i < j; i++) query.setParameter(i, values[i]); return query; } /** * SQL方式查询 * @param sql 符合SQL语法的查询语句 * @param values 数量可变的条件值,按顺序绑定 */ public SQLQuery createSQLQuery(final String sql,final Object... values){ SQLQuery query = getSession().createSQLQuery(sql); if (values != null) { for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } return query; } /** * 根据类型创建查询对象 * @param clazz 类型 */ public Criteria createCriteria(finalClass clazz) { return getSession().createCriteria(clazz); } /** * 对象化查询 * @param entityClass 参数T的反射类型 * @param criterions 数量可变的Criterion */ public Criteria createCriteria(finalClass clazz,final Criterion... criterions){ Criteria criteria = getSession().createCriteria(clazz); for (Criterion c : criterions) { criteria.add(c); } return criteria; } /** * 对象化查询 * @param criterions 数量可变的Criterion */ public Criteria createCriteria(final Criterion... criterions){ return createCriteria(entityClass, criterions); } } ######################## ReflectUtils.java package com.core.utils; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * 反射工具类 */ public class ReflectUtils { /** * 获得超类的参数类型,取第一个参数类型 * @param <T> 类型参数 * @param clazz 超类类型 */ public static <T> Class<T> getClassGenricType(finalClass clazz) { return getClassGenricType(clazz, 0); } /** * 根据索引获得超类的参数类型 * @param clazz 超类类型 * @param index 索引 */ public static Class getClassGenricType(finalClass clazz,final int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { return Object.class; } Type[] params = ((ParameterizedType)genType).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } if (!(params[index] instanceofClass)) { return Object.class; } return (Class) params[index]; } } ######################### Administrator.java package com.pkms.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.Table; /** * Administrator entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "administrator", catalog = "pkms") public class Administrator implements java.io.Serializable { // Fields private Integer id; private String loginName; private String password; // Constructors /** default constructor */ public Administrator() { } /** full constructor */ public Administrator(String loginName, String password) { this.loginName = loginName; this.password = password; } // Property accessors @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable =false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "loginName", length = 11) public String getLoginName() { return this.loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @Column(name = "password", length = 11) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } } ########### AdministratorDAO.java package com.pkms.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.flex.remoting.RemotingDestination; import org.springframework.stereotype.Repository; import com.core.hibernate.HibernateDao; import com.pkms.entity.Administrator; @SuppressWarnings("unchecked") @Repository("administratorDAO") public class AdministratorDAO extends HibernateDao<Administrator>{ @Autowired private SessionFactory sessionFactory; public AdministratorDAO(){ super(); // System.out.println("AdministratorDAO init"+sessionFactory==null); } public List getByName(String loginName){ try { Session session=super.getSession(); String sql="select u.username,u.password,u.age,u.email from user u where u.username ='"+loginName+"'"; Query query=session.createSQLQuery(sql); List list=query.list(); if(list!=null&&list.size()>0){ System.out.println(list.get(0)); } return list; } catch (Exception e) { e.printStackTrace(); return null; } } } ############################## AdministratorService.java package com.pkms.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.flex.remoting.RemotingDestination; import org.springframework.flex.remoting.RemotingInclude; import org.springframework.stereotype.Service; import com.pkms.dao.AdministratorDAO; @RemotingDestination(channels = { "my-amf", "my-secure-amf" }) @Service("administratorService") public class AdministratorService { @Autowired private AdministratorDAO administratorDAO; @RemotingInclude public List firstApp(String info) { List list = administratorDAO.getByName(info); System.out.println(info+"坑爹的有木有" + list); return list;// 返回给FLEX } }
下载地址:
http://download.csdn.net/detail/kkkloveyou/4079410