整合spring4.1.7和dorado5

来源:互联网 发布:淘宝什么东西最好卖 编辑:程序博客网 时间:2024/06/05 19:37

由于公司项目采用了dorado5开发,所以我自己练习了下整合spring和dorado5构建了一个简单的项目。项目采用mysql数据库。dorado5好像只对ie兼容,所以浏览页面的时候推荐使用ie浏览器。
- 新建一个dorado项目。项目搭建请参考Dorado5深入浅出。在dorado的view目录的clazz属性里面添加上我们对应的java类Main.java。这个也是dorado链接后台的关键。
- 导入spring相关的jar包
jar包详情:核心
spring-core: spring-core-4.1.7.RELEASE.jar
commons-logging-1.2.jar

spring-beans: spring-beans-4.1.7.RELEASE.jar

spring-context: spring-context-4.1.7.RELEASE.jar
spring-aop-4.1.7.RELEASE.jar
aopalliance-1.0.jar
spring-expression-4.1.7.RELEASE.jar

Dao层(数据库)
spring-jdbc: spring-jdbc-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
(包含了spring-tx)

web层
spring-web: spring-web-4.1.7.RELEASE.jar
spring-webmvc:spring-webmvc-4.1.7.RELEASE.jar

  • 新建BaseAction 继承DefaultViewModel。这个类用于作为所有和dorado的view文件链接的action的父类。
private static WebApplicationContext context;    protected static ApplicationContext getContext(){        System.out.println("监控下context============"+context);        if(context==null){            //获取dorado的对象,用于得到request            HttpDoradoContext hc = (HttpDoradoContext)DoradoContext.getContext();            HttpServletRequest request = hc.getRequest();            context = WebApplicationContextUtils.                    getRequiredWebApplicationContext(request.getSession().getServletContext());        }        return context;    } 

这个方法是读取spring相关配置的入口。通过context.getBean()方法,我们能获取到spring ioc注入的dao

  • 编辑Main.java继承BaseAction
    我们重写从父类继承过来的doLoadData方法。当页面刷新的时候会调用这个方法。
        UserDao userDao = (UserDao) this.getContext().getBean("userDao");        if(dataset.getId().equals("dsUser")){            Map map = new HashMap(); //作为存入条件的容器,可以是对象            ParameterSet parameters = dataset.parameters();            DOUtils.variantSetToDO(parameters, map);            List<UserT> users = userDao.queryUser(map.get("userName")==null?null:map.get("userName").toString());            dataset.fromDO(users);        }
  • 查询表,可以使用spingTemplate
        // TODO Auto-generated method stub        String sql = "select * from t_user where 1=1";        if(userName!=null && !"".equals(userName)){            sql +=" and user_name='"+userName+"'";        }        final List<UserT> lists = new ArrayList<UserT>();        jdbcTemplate.query(sql, new RowCallbackHandler(){            @Override            public void processRow(ResultSet rs) throws SQLException {                // TODO Auto-generated method stub                UserT user = new UserT();                user.setUserId(rs.getLong(1));                user.setUserName(rs.getString(2));                user.setPassword(rs.getString(3));                user.setCreateDate(rs.getDate(4));                lists.add(user);            }        });        return lists;

当然事务是不可缺少的
通过声明式事务解决

 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">           <property name="dataSource" ref="dataSource" />          </bean>          <tx:advice id="txAdvice" transaction-manager="txManager">              <tx:attributes>                  <tx:method name="add*" propagation="REQUIRED" />                   <tx:method name="insert*" propagation="REQUIRED" />                  <tx:method name="save*" propagation="REQUIRED" />                  <tx:method name="del*" propagation="REQUIRED" />                  <tx:method name="update*" propagation="REQUIRED" />                  <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />              </tx:attributes>          </tx:advice>            <aop:config proxy-target-class="true">            <aop:pointcut id="servicePointcut" expression="execution(* com.zwq.dao.impl.*.*(..))" />            <aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice" order="1" />        </aop:config>

详细代码点我下载

0 0