Is there a simple way to get a List<SomeType>

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a simple way to get a List<SomeType>

Boban
I want to retrieve a list of objects from DB. all examples I've seen
involve resultMap and maing the list as property of another object. I
need equivalent of this:

List<Account> accounts = (List<Account>)
session.selectList("mypackage.AccountMapper.selectAll");

but done in a typesafe way through mapper object

With something this in account.xml:

  <select id="selectAll" resultType="Account">
    SELECT * FROM accounts
  </select>

Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Larry Meadors
Yes.

http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf

Larry


On Thu, Oct 21, 2010 at 11:01 AM, Boban <[hidden email]> wrote:

> I want to retrieve a list of objects from DB. all examples I've seen
> involve resultMap and maing the list as property of another object. I
> need equivalent of this:
>
> List<Account> accounts = (List<Account>)
> session.selectList("mypackage.AccountMapper.selectAll");
>
> but done in a typesafe way through mapper object
>
> With something this in account.xml:
>
>  <select id="selectAll" resultType="Account">
>    SELECT * FROM accounts
>  </select>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Boban
Well that was very classy.

Can you spell it out for me? I did read the manual but didn't find a
simple way. if I try using a simple select with query that returns
multiple rows I keep getting errors that selectOne() expects only one
row or NULL.

ie this works:

select * from accounts WHERE id=#{1}

but if I remove WHERE part then I get the error about too many rows.



On Oct 21, 7:05 pm, Larry Meadors <[hidden email]> wrote:
> Yes.
>
> http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf
>
> Larry
Reply | Threaded
Open this post in threaded view
|

RE: Is there a simple way to get a List<SomeType>

Rick.Wellman
Without you supplying any source code at all, I will speculate this from page 54:
Object selectOne(String statement, Object parameter)
List selectList(String statement, Object parameter)
int insert(String statement, Object parameter)
int update(String statement, Object parameter)
int delete(String statement, Object parameter)

I would guess that you are not changing selectOne() to selectList()?

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Boban
Sent: Thursday, October 21, 2010 12:17 PM
To: mybatis-user
Subject: Re: Is there a simple way to get a List<SomeType>

Well that was very classy.

Can you spell it out for me? I did read the manual but didn't find a
simple way. if I try using a simple select with query that returns
multiple rows I keep getting errors that selectOne() expects only one
row or NULL.

ie this works:

select * from accounts WHERE id=#{1}

but if I remove WHERE part then I get the error about too many rows.



On Oct 21, 7:05 pm, Larry Meadors <[hidden email]> wrote:
> Yes.
>
> http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf
>
> Larry
Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Boban
I did use a selectList(), look at the original post. I want something
like this:

AccountMapper accMapper = session.getMapper(AccountMapper.class);
List<Account> = accMapper.getAllAccounts();

But I can't get it to work. I can get this to work:

Account account = accMapper.getOneAccount(accountId);

and I can get this:

List<Account> accounts = (List<Account>)
session.selectList("mypackage.AccountMapper.selectAll");

But my question was is there a way to get List<Account> via a typesafe
mapper. All examples I've seen involve creating resultMap and
retrieving one big object with an ArrayList<SomeOtherObject>, nothing
that would show me a simple way to get a list of objects.

Sorry if OP was unclear

Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Nathan Maves
We need more of your code to help.

For starters please post your AccountMapper interface as well as any
Xml mappings for that mapper.

Nathan

On Thu, Oct 21, 2010 at 11:28 AM, Boban <[hidden email]> wrote:

> I did use a selectList(), look at the original post. I want something
> like this:
>
> AccountMapper accMapper = session.getMapper(AccountMapper.class);
> List<Account> = accMapper.getAllAccounts();
>
> But I can't get it to work. I can get this to work:
>
> Account account = accMapper.getOneAccount(accountId);
>
> and I can get this:
>
> List<Account> accounts = (List<Account>)
> session.selectList("mypackage.AccountMapper.selectAll");
>
> But my question was is there a way to get List<Account> via a typesafe
> mapper. All examples I've seen involve creating resultMap and
> retrieving one big object with an ArrayList<SomeOtherObject>, nothing
> that would show me a simple way to get a list of objects.
>
> Sorry if OP was unclear
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Boban
OK...

AccoutsMapper:

public interface AccountMapper {
    public List<Account> selectAll();
    public Account selectAccount(int accountId);
    public void insertAccount(Account account);
    public void updateAccount(Account account);
    public void deleteAccount(int accountId);
}

accounts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://
mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mypackage.AccountMapper">

  <select id="selectAll" resultType="Account">
    SELECT * FROM accounts
  </select>

  <select id="selectAccount" parameterType="int"
resultType="Account">
    SELECT * FROM accounts WHERE "accountId" = #{accountId}
  </select>

  <insert id="insertAccount" parameterType="Account">
    INSERT INTO accounts ("name") VALUES (#{name})
  </insert>

  <update id="updateAccount" parameterType="Account">
    UPDATE accounts SET "name" = #{name} WHERE "accountId" =
#{accountId}
  </update>

  <delete id="deleteAccount" parameterType="int">
    DELETE FROM accounts WHERE "accountId" = #{accountId}
  </delete>
</mapper>

With this I can retrieve one account object, update, delete and insert
one row at a time, ie all functions except selectAll() work fine. But
selectAll() fails and says that I have too may rows in DB, specificaly
that it is using selectOne() and that selectOne() expects zero or one
rows in result set.

I want AccountMapper.selectAll() to return same result as this:

List<Account> accounts = (List<Account>)
session.selectList("mypackage.AccountMapper.selectAll");

accounts table is simple, just  int accountId and String name.

I can't make this much clearer. Anyone knows how to make this work, or
do I just use ugly typecasts?
Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Boban
OK, figured out the problem. My original code had

hashMap selectAll();

in AccountsMapper interface and batis was generating call to
selectOne() instead of selectList().

Thanx for your cooperation :D
Reply | Threaded
Open this post in threaded view
|

Re: Is there a simple way to get a List<SomeType>

Guy Rouillier
In reply to this post by Boban
On 10/21/2010 2:42 PM, Boban wrote:

> With this I can retrieve one account object, update, delete and insert
> one row at a time, ie all functions except selectAll() work fine. But
> selectAll() fails and says that I have too may rows in DB, specificaly
> that it is using selectOne() and that selectOne() expects zero or one
> rows in result set.
>
> I want AccountMapper.selectAll() to return same result as this:
>
> List<Account>  accounts = (List<Account>)
> session.selectList("mypackage.AccountMapper.selectAll");
>
> accounts table is simple, just  int accountId and String name.
>
> I can't make this much clearer. Anyone knows how to make this work, or
> do I just use ugly typecasts?

Works fine for me.  You don't mention what database you are using, so I
used PostgreSQL:

table:
create table accounts("accountId" int, name varchar(50));
select * from accounts;
1;"one"
2;"two"

mapper.xml:
     <select id="selectAllAccounts" resultType="org.sample.beans.Account">
      SELECT * FROM accounts
     </select>

mapper.java:
        public List<Account> selectAllAccounts();

Main.java:
          List<Account> accounts = mapper.selectAllAccounts();

          for (Account a: accounts)
             {
             log.info("Account name: " + a.getName());
             }

INFO 2010-10-22 00:15:01,921 org.sample.mybatis.Main: Account name: one
INFO 2010-10-22 00:15:01,921 org.sample.mybatis.Main: Account name: two


--
Guy Rouillier