DAO pomocí RowMapperu.
1.1 --- a/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAO.java Wed Feb 10 21:13:49 2010 +0100
1.2 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAO.java Wed Feb 10 21:23:45 2010 +0100
1.3 @@ -2,13 +2,11 @@
1.4
1.5 import cz.frantovo.springDemo1.KnihaDAORemote;
1.6 import cz.frantovo.springDemo1.dto.Kniha;
1.7 -import java.util.ArrayList;
1.8 import java.util.Collection;
1.9 import javax.annotation.Resource;
1.10 import javax.ejb.Stateless;
1.11 import javax.sql.DataSource;
1.12 import org.springframework.jdbc.core.JdbcTemplate;
1.13 -import org.springframework.jdbc.support.rowset.SqlRowSet;
1.14
1.15 /**
1.16 *
1.17 @@ -19,23 +17,10 @@
1.18
1.19 @Resource(mappedName = "jdbc/sqlVyuka/piskoviste")
1.20 private DataSource dataSource;
1.21 + private KnihaRowMapper knihaRowMapper = new KnihaRowMapper();
1.22
1.23 public Collection<Kniha> getKnihy() {
1.24 - Collection<Kniha> vysledek = new ArrayList<Kniha>();
1.25 -
1.26 JdbcTemplate t = new JdbcTemplate(dataSource);
1.27 - SqlRowSet rs = t.queryForRowSet("SELECT * FROM sbirka.kniha;");
1.28 - while (rs.next()) {
1.29 - Kniha k = new Kniha();
1.30 - k.setNazev(rs.getString("nazev"));
1.31 - k.setAutor(rs.getInt("autor"));
1.32 - k.setDatumAktualizace(rs.getDate("datum"));
1.33 - k.setId(rs.getInt("id"));
1.34 - k.setPocetStran(rs.getInt("pocet_stran"));
1.35 - k.setRokVydani(rs.getInt("rok_vydani"));
1.36 - vysledek.add(k);
1.37 - }
1.38 -
1.39 - return vysledek;
1.40 + return t.query("SELECT * FROM sbirka.kniha;", knihaRowMapper);
1.41 }
1.42 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java Wed Feb 10 21:23:45 2010 +0100
2.3 @@ -0,0 +1,24 @@
2.4 +package cz.frantovo.springDemo1.dao;
2.5 +
2.6 +import cz.frantovo.springDemo1.dto.Kniha;
2.7 +import java.sql.ResultSet;
2.8 +import java.sql.SQLException;
2.9 +import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
2.10 +
2.11 +/**
2.12 + *
2.13 + * @author fiki
2.14 + */
2.15 +public class KnihaRowMapper implements ParameterizedRowMapper<Kniha> {
2.16 +
2.17 + public Kniha mapRow(ResultSet rs, int i) throws SQLException {
2.18 + Kniha k = new Kniha();
2.19 + k.setNazev(rs.getString("nazev"));
2.20 + k.setAutor(rs.getInt("autor"));
2.21 + k.setDatumAktualizace(rs.getDate("datum"));
2.22 + k.setId(rs.getInt("id"));
2.23 + k.setPocetStran(rs.getInt("pocet_stran"));
2.24 + k.setRokVydani(rs.getInt("rok_vydani"));
2.25 + return k;
2.26 + }
2.27 +}