`
onaod32n
  • 浏览: 6646 次
社区版块
存档分类
最新评论

Flex+BlazeDS+Spring+Hibernate架构整合示例

阅读更多

  如下图所示建立工程:
  
  所需lib包一览:
  
  代码如下:
  StsHibernateTemplate.java package com.stswg.dao.base; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autow ired; import org.springframework.orm.hibernate3.HibernateTempla te; public class StsHibernateTemplate extends HibernateTemplate { @Autowired @Override public void setSessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } } CustomerDaoImpl.java package com.stswg.dao.impl; import java.util.List; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import com.stswg.dao.CustomerDao; import com.stswg.dao.base.StsHibernateTemplate; import com.stswg.entity.Customer; public class CustomerDaoImpl extends StsHibernateTemplate implements CustomerDao { private String[] data = { "电子", "仁宝电脑", "富士通软件", "太极实业", "万科地产", "中铁2局", "海南航空", "德赛电池", "东湖高新", "哈药集团", "太平洋保险", "平安保险", "太极集团", "中粮地产", "世贸股份", "中国中铁", "金山软件", "中国银行", "建设银行", "华阳软件" }; @Override public int insertCustomer() { int index = new Double(Math.floor(Math.random() * data.length)).intValue(); Customer customer = new Customer(); customer.setName(data[index]); customer.setAddress(data[index] + "公司所在地址"); customer.setTelephone("021-67376464"); save(customer); return 1; } @Override public int selectCustomerCount() { Criteria criteria = getSession().createCriteria(Customer.class); return criteria.list().size(); } @SuppressWarnings("unchecked") @Override public List selectCustomer(String name) { Criteria criteria = getSession().createCriteria(Customer.class); if(StringUtils.isNotEmpty(name)) { criteria.add(Restrictions.like("name", "%" + name + "%")); } return criteria.list(); } @Override public int deleteCustomer() { deleteAll(selectCustomer(null)); return 1; } } CustomerDao.java package com.stswg.dao; import java.util.List; import com.stswg.entity.Customer; public interface CustomerDao { public int deleteCustomer(); public int insertCustomer(); public int selectCustomerCount(); public List selectCustomer(String name); } BaseEntity.java package com.stswg.entity.base; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; @MappedSuperclass public class BaseEntity { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } public boolean isNew() { return (this.id == null); } } Customer.java package com.stswg.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import com.stswg.entity.base.BaseEntity; @Entity @Table(name="t_customer") public class Customer extends BaseEntity { @Column(name="name") private String name; @Column(name="address") private String address; @Column(name="telephone") private String telephone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } } NoCacheFilter.java package com.stswg.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; public class NoCacheFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { HttpServletResponse res = (HttpServletResponse) response; res.addHeader("Cache-Control", "no-store"); res.addHeader("Pragma", "no-store"); chain.doFilter(request, response); } catch(Exception e) { } } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } } CustomerServiceImpl.java package com.stswg.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autow ired; import org.springframework.flex.remoting.RemotingDestinat ion; import org.springframework.transaction.annotation.Propaga tion; import org.springframework.transaction.annotation.Transac tional; import com.stswg.dao.CustomerDao; import com.stswg.entity.Customer; import com.stswg.service.CustomerService; @RemotingDestination public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDao customerDao; @Transactional(propagation=Propagation.REQUIRED) public int insertCustomer() { return customerDao.insertCustomer(); } @Transactional(readOnly=true) public int selectCustomerCount() { return customerDao.selectCustomerCount(); } @Transactional(readOnly=true) public List selectCustomer(String name) { return customerDao.selectCustomer(name); } @Transactional(propagation=Propagation.REQUIRED) public int deleteCustomer() { return customerDao.deleteCustomer(); } } CustomerService.java package com.stswg.service; import java.util.List; import com.stswg.entity.Customer; public interface CustomerService { public int deleteCustomer(); public int insertCustomer(); public int selectCustomerCount(); public List selectCustomer(String name); } jdbc.properties #DataSource(C3P0) jdbc.driverClassName=org.hsqldb.jdbcDriver jdbc.url=jdbc:hsqldb:mem:. jdbc.username=sa jdbc.password= jdbc.initialPoolSize=10 jdbc.minPoolSize=5 jdbc.maxPoolSize=30 jdbc.acquireIncrement=5 jdbc.maxIdleTime=10 jdbc.maxStatements=0 #Hibernate hibernate.dialect=org.hibernate.dialect.HSQLDialec t hibernate.show_sql=false hibernate.jdbc.batch_size=20 hibernate.generate_statistics=true hibernate.cache.provider_class=org.hibernate.cache .EhCacheProvider hibernate.cache.use_query_cache=true hibernate.cache.use_second_level_cache=true hibernate.cache.use_structured_entries=true hibernate.hbm2ddl.auto=create-drop log4j.properties #Log4j #Configuring loggers log4j.rootLogger=error, console, file #Configuring Appenders log4j.appender.console=org.apache.log4j.ConsoleApp ender log4j.appender.file=org.apache.log4j.RollingFileAp pender log4j.appender.file.File=C://logs.log #Configuring layouts log4j.appender.console.layout=org.apache.log4j.Sim pleLayout log4j.appender.file.layout=org.apache.log4j.Patter nLayout log4j.appender.file.layout.ConversionPattern=%d{yy yy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n remoting-config.xml           services-config.xml             false       [BlazeDS]  false false false false   Endpoint.* Service.* Configuration      false    applicationContext.xml  flex="http://www.springframework.org/schema/ flex" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op-2.5.xsd 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/spri ng-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx -2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring- flex-1.0.xsd">       com.stswg.entity.Customer      ${hibernate.dialect}   ${hibernate.show_sql}   ${hibernate.jdbc.batch_size}   ${hibernate.generate_statistics}   ${hibernate.cache.provider_class}   ${hibernate.cache.use_query_cache}   ${hibernate.cache.use_second_level_cache}   ${hibernate.cache.use_structured_entries}   ${hibernate.hbm2ddl.auto}                flex:message-broker services-config-path="/WEB-INF/flex/services-confi g.xml" /> flex:remoting-destination ref="customerService" /> -->  web.xml   Spring-Flex-BlazeDS DEMO  noCacheFilter  com.stswg.filter.NoCacheFilter    noCacheFilter /*   flex.messaging.HttpFlexSession   Spring MVC Dispatcher Servlet org.springframework.web.servlet.Dispa tcherServlet  contextConfigLocation  /WEB-INF/applicationContext.xml   1   Spring MVC Dispatcher Servlet /spring/*   FlexDemo.html   接下来是创建Flex端工程,如下图所示:
  
  代码如下:
  style.css /* CSS file */ Application { fontSize: 14; } FlexDemo.mxml               height="100%" horizontalAlign="center"> Flex版演示程序" />          height="400" id="dataGrid" doubleClickEnabled="true" horizontalScrollPolicy="auto" headerWordWrap="true" wordWrap="true" sortExpertMode="true">            设置Flex工程参数如下: 1.Compile参数设置
  
  2.Deploy位置:
  
  启动Demo工程的Tomcat即可访问。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics