案例学习BlazeDS+Spring之三InSync01查找联系人

InSync01:查找联系人


一、运行DEMO:
1、运行程序:http://localhost:8400/spring-flex-testdrive/insync01/index.html;
2、单击Search按钮,从数据库获取所有的联系人。
3、在Search文本框输入几个字符,单击“Search”按钮,按名字来查找联系人。


二、理解代码:
1、insync01.mxml文件:
定义远程对象,

<s:RemoteObject id="ro" destination="contactService" endpoint="http://localhost:8400/spring-flex-testdrive/messagebroker/amf"/>
Search的单击事件处理代码是:ro.findByName(searchStr.text)

2、配置文件

flex-servlet.xml :
<flex:remoting-destination ref="contactService" />
app-config.xml :
<bean id="contactService" class="org.springframework.flex.samples.contact.ContactDAO">
    <constructor-arg ref="dataSource" />
</bean>

3、org/springframework/flex/samples/contact/ContactDAO.java
构造函数实例化template(用于查找、更新、删除操作)和insertContact(用于插入操作)。
public ContactDAO(DataSource dataSource) {
        this.template = new SimpleJdbcTemplate(dataSource);
        this.insertContact = new SimpleJdbcInsert(dataSource).withTableName("CONTACT").usingGeneratedKeyColumns("ID");
    }

按名称查找联系人的方法。

public List<Contact> findByName(String name) {
        return this.template.query("SELECT * FROM contact WHERE UPPER(CONCAT(first_name, ' ', last_name)) LIKE ? ORDER BY first_name, last_name",
            this.rowMapper, "%" + name.toUpperCase() + "%");
    }

4、SimpleJdbcTemplate:

Spring的SimpleJdbcTemplate封装了常用的Jdbc操作,其中一个很方便的用法是在query查询数据库之后可以在ResultSet结果集中进行迭代,将结果集中的行转换为java对象。

SimpleJdbcTemplate的query方法格式如:

query(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args) 。

为了使用泛型简化操作,定义了Contact类,用于在数据库中返回值时进行数据行与JAVA对类的转换。

private final RowMapper<Contact> rowMapper = new ParameterizedRowMapper<Contact>() {

        public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
            Contact contact = new Contact();
            contact.setId(rs.getInt("id"));
            contact.setFirstName(rs.getString("first_name"));
            contact.setLastName(rs.getString("last_name"));
            contact.setAddress(rs.getString("address"));
            contact.setCity(rs.getString("city"));
            contact.setState(rs.getString("state"));
            contact.setZip(rs.getString("zip"));
            contact.setPhone(rs.getString("phone"));
            contact.setEmail(rs.getString("email"));
            return contact;
        }
    };

 

 三、小结:

这个例子显示了一个查询操作,从Flex到在DAO中使用Spring提供的JDBC API来操作数据库。这个例子展示的架构中的主要过程简化为:RemoteObject->flex:remoting-destination->Spring bean->Spring JdbcTemplate->H2 DB

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值