论坛首页 入门技术论坛

ibatis queryForList方法取值问题

浏览 4897 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-27  
小弟最近想学学ibatis 网上找了找教程,做个简单的测试程序先试试,现在 增 删 改 查(查一个)都没问题了,就是现在遍历所有数据有个问题,他返回的是一个HashMap,我到是可以通过对HashMap进行遍历来取出所有数据,可是这样我就没法让他变成对像了

我在查一条数据的时候有一个resultMap="usersResult" 再通过写 <resultMap>标签,可以让我查出来的数据按列名自己设到我的model类里,我想问下,遍历所有数据有没有像这个一样的方法也能出来就帮我自动放到对像里了

SqlMapConfig.xml
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:sqlserver://localhost:1433;databasename=test"/>
<property name="JDBC.Username" value="sa"/>
<property name="JDBC.Password" value=""/>
<property name="Pool.MaximumActiveConnections"
value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime"
value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from
ACCOUNT"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan"
value="1"/>
<property name="Pool.PingConnectionsNotUsedFor"
value="1"/>
</dataSource>
</transactionManager>

<sqlMap resource="com/tx/test/Users.xml"/>


Users.xml
<sqlMap namespace="Users">

    <typeAlias alias="users" type="com.tx.test.Users"/>

    <cacheModel id="usersCache" type="MEMORY" readOnly="false" serialize="true">
        <flushInterval hours="24"/>
        <flushOnExecute statement="Users.update"/>
        <flushOnExecute statement="Users.insert"/>
        <flushOnExecute statement="Users.delete"/>
    </cacheModel>

    <resultMap id="usersResult" class="users">
    <result property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="age" column="age"/>
    </resultMap>

    <!-- hsql db used for this example capitalizes col names
        so Map will have them as all caps -->
    <select id="getAll" resultClass="java.util.HashMap" cacheModel="usersCache">
        SELECT
            id,
            username,       
            age     
        FROM users
    </select>

    <select id="getById" resultMap="usersResult" parameterClass="java.lang.Integer">
        SELECT id,username, age
        FROM users
        WHERE id = #value#           
    </select>

    <update id="update" parameterClass="users">
        UPDATE users
        SET
            username = #username#,         
            age = #age#         
        WHERE id = #id#
    </update>

    <insert id="insert" parameterClass="users" >
        INSERT INTO users (username, age)
        VALUES (#username#, #age#)
    </insert>

    <delete id="delete" parameterClass="java.lang.Integer">
        DELETE FROM users WHERE id = #value#
    </delete>
</sqlMap>


Users.java
        private int id;
private String username;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
        ......
        ......


测试类
    List list = sqlMapper.queryForList("Users.getAll",null);
   
    for(int i=0;i <list.size();i++) {
    HashMap hm = (HashMap)list.get(i);
    Set set = hm.keySet();
    Iterator it = set.iterator();
    while(it.hasNext()) {
    System.out.print(hm.get(it.next()) + "\t");
    }
    System.out.println();
    }
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics