Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArticleDao {
- private final JdbcTemplate jdbcTemplate;
- public ArticleDao(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
- public void draft(String md5) {
- jdbcTemplate.update("update article set state = ? where md5 = ?", "draft", md5);
- }
- public List<Article> findByDate(String date) {
- return jdbcTemplate.query("select * from article where date = ? order by date desc", new Object[] { dateBuilder.date(date) }, new ArticleMapper());
- }
- }
- public class ArticleMapper implements RowMapper<Article> {
- @Override
- public Article mapRow(ResultSet resultSet, int i) throws SQLException {
- Map<String, String> map = new HashMap<String, String>();
- map.put("channel", resultSet.getString("category"));
- map.put("title", resultSet.getString("title"));
- map.put("override title", resultSet.getString("title_overriden"));
- map.put("testo", resultSet.getString("content"));
- map.put("datanotizia", resultSet.getString("date"));
- map.put("state", resultSet.getString("state"));
- return new Article(map);
- }
- }
- public class MysqlBuilder {
- @Override
- protected DataSource build() {
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl(format("jdbc:mysql://{0}/{1}", getHost(), getName()));
- dataSource.setUsername("root");
- dataSource.setPassword("");
- return dataSource;
- }
- }
- public class ArticleDaoTest {
- private static ArticleDao dao;
- @BeforeClass
- public static void setUp() {
- dao = new DaoFactory("testdb").createDao(Article.class);
- }
- private List<Article> foundArticles;
- @Test
- public void shouldFindByMd5() {
- Article article = new Article(map("channel", "channel", "title", "title", "testo", "testo", "datanotizia", "2010-02-16"));
- dao.updateOrInsert(article);
- foundArticles = dao.findByMd5(article.getMd5());
- assertEquals(article, foundArticles.get(0));
- }
- @Test
- public void shouldFoundAddedArticleByCategory() {
- Article article = new Article(map("channel", "channel", "title", "title", "testo", "testo", "datanotizia", "2010-02-16"));
- dao.updateOrInsert(article);
- foundArticles = dao.findByCategory("channel");
- assertEquals(article, foundArticles.get(0));
- }
- }
Add Comment
Please, Sign In to add comment