Java tutorial
/* * Copyright 2014. Vadim Baranov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.vader.apm.dao.impl; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport; import org.springframework.stereotype.Repository; import org.vader.apm.dao.AccountDao; import org.vader.apm.dao.util.DaoUtils; import org.vader.apm.dao.util.DbObjectState; import org.vader.apm.domain.Account; import java.sql.ResultSet; import java.sql.SQLException; /** * @author Vadim Baranov */ @Repository public class AccountDaoImpl extends NamedParameterJdbcDaoSupport implements AccountDao { @Override @CachePut(value = { "ACCOUNTS_L1", "ACCOUNTS_L2" }, key = "#result.id") public Account insert(Account account) { DaoUtils.assertObjectState(DbObjectState.TRANSIENT, account); final Long id = getJdbcTemplate().queryForObject( "INSERT INTO accounts (id, login, password) " + "VALUES (nextval('accounts_seq'), ?, ?) RETURNING id", Long.class, account.getLogin(), account.getPassword()); account.setId(id); return account; } @Override @Cacheable(value = { "ACCOUNTS_L1", "ACCOUNTS_L2" }, key = "#id") public Account getById(long id) { try { return getJdbcTemplate().queryForObject("SELECT * FROM accounts WHERE id = ?", AccountRowMapper.INSTANCE, id); } catch (EmptyResultDataAccessException e) { return null; } } @Override @CachePut(value = { "ACCOUNTS_L1", "ACCOUNTS_L2" }, key = "#result.id") public Account update(Account account) { DaoUtils.assertObjectState(DbObjectState.PERSISTENT, account); DaoUtils.assertOne(getJdbcTemplate().update("UPDATE accounts SET login = ?, password = ? WHERE id = ?", account.getLogin(), account.getPassword(), account.getId())); return account; } @Override @CacheEvict(value = { "ACCOUNTS_L1", "ACCOUNTS_L2" }, key = "#id") public void delete(long id) { DaoUtils.assertOne(getJdbcTemplate().update("DELETE FROM accounts WHERE id = ?", id)); } /** * Account row mapper. */ private enum AccountRowMapper implements RowMapper<Account> { INSTANCE { @Override public Account mapRow(ResultSet rs, int rowNum) throws SQLException { final Account result = new Account(); result.setId(rs.getLong("id")); result.setLogin(rs.getString("login")); result.setPassword(rs.getString("password")); return result; } } } }