Java tutorial
/******************************************************************************* * Copyright (c) 2013 BowenCai. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * BowenCai - initial API and implementation ******************************************************************************/ package net.freechoice.dao.impl; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.util.List; import net.freechoice.dao.IDao_Transaction; import net.freechoice.dao.annotation.DBSpec; import net.freechoice.dao.annotation.Dialect; import net.freechoice.model.FC_Transaction; import net.freechoice.model.orm.Map_Transaction; import org.springframework.jdbc.core.PreparedStatementCreator; /** * * @author BowenCai * */ public class Dao_Transaction extends DaoTemplate<FC_Transaction> implements IDao_Transaction { public static final String TIME_DESCEND = " order by time_committed desc "; public static final String TIME_ASCEND = " order by time_committed asc "; public Dao_Transaction() { super(FC_Transaction.class, new Map_Transaction()); } private static final String SELECT_FROM_FC_TRANSACTION = "SELECT id," + "id_account_," + "time_committed," + "amount," + "comment " + "from fc_transaction "; @Override public FC_Transaction getById(int id) { return getJdbcTemplate().queryForObject(SELECT_FROM_FC_TRANSACTION + "where is_valid = true", mapper); } @Override public List<FC_Transaction> getTransactionsOfUser(int userId) { return getJdbcTemplate() .query("select * from fc_transaction as Tr" + " join fc_account as Ac on Tr.id_account_ = Ac.id" + " join fc_user as U on U.id = Ac.id_user_" + "where U.is_valid = true and U.id = " + userId + Dao_Transaction.TIME_DESCEND, mapper); } @Override public List<FC_Transaction> getTransactionsOfAccount(int accountId) { return getJdbcTemplate().query(SELECT_FROM_FC_TRANSACTION + "where is_valid = true and id_account_ = " + accountId + Dao_Transaction.TIME_DESCEND, mapper); } @Override public List<FC_Transaction> getTransactionsOnTime(final Timestamp timestamp) { return getJdbcTemplate().query(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement(SELECT_FROM_FC_TRANSACTION + "where is_valid = true and time_committed = ?" + Dao_Transaction.TIME_DESCEND); ps.setTimestamp(1, timestamp); return ps; } }, mapper); } @DBSpec(dialect = Dialect.PostgreSQL) @Override public List<FC_Transaction> getTransactionsOnDate(final Date date) { return getJdbcTemplate().query(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement(SELECT_FROM_FC_TRANSACTION + "where is_valid = true and time_committed::date = ?" + Dao_Transaction.TIME_DESCEND); ps.setDate(1, date); return ps; } }, mapper); } }