|
| 1 | +package tw.codedata; |
| 2 | + |
| 3 | +import org.springframework.jdbc.core.support.JdbcDaoSupport; |
| 4 | +import org.springframework.jdbc.core.RowMapper; |
| 5 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 6 | +import javax.sql.DataSource; |
| 7 | +import java.sql.*; |
| 8 | +import java.util.*; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +@Service |
| 13 | +public class DvdDaoJdbcImpl extends JdbcDaoSupport implements DvdDao { |
| 14 | + |
| 15 | + @Autowired |
| 16 | + public DvdDaoJdbcImpl(DataSource dataSource) { |
| 17 | + setDataSource(dataSource); |
| 18 | + } |
| 19 | + |
| 20 | + public DvdDaoJdbcImpl createTables() { |
| 21 | + JdbcTemplate jdbcTemplate = getJdbcTemplate(); |
| 22 | + |
| 23 | + jdbcTemplate.execute("DROP TABLE dvds if exists"); |
| 24 | + jdbcTemplate.execute("DROP TABLE directors if exists"); |
| 25 | + |
| 26 | + jdbcTemplate.execute( |
| 27 | + "CREATE TABLE directors (" |
| 28 | + + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1)," |
| 29 | + + "name VARCHAR(255)," |
| 30 | + + "PRIMARY KEY(id))" |
| 31 | + ); |
| 32 | + jdbcTemplate.execute( |
| 33 | + "CREATE TABLE dvds (" |
| 34 | + + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1)," |
| 35 | + + "title VARCHAR(255)," |
| 36 | + + "year INTEGER NOT NULL," |
| 37 | + + "duration INTEGER NOT NULL, " |
| 38 | + + "director_id INTEGER NOT NULL, " |
| 39 | + + "FOREIGN KEY (director_id) REFERENCES directors(id))" |
| 40 | + ); |
| 41 | + |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + public void saveDvd(Dvd dvd) { |
| 46 | + Integer directorId = getDirectorId(dvd.getDirector()); |
| 47 | + getJdbcTemplate().update( |
| 48 | + "INSERT INTO dvds " |
| 49 | + + "(title, year, duration, director_id) " |
| 50 | + + "VALUES (?, ?, ?, ?)", |
| 51 | + dvd.getTitle(), dvd.getYear(), dvd.getDuration(), directorId); |
| 52 | + } |
| 53 | + |
| 54 | + public List<Dvd> allDvds() { |
| 55 | + return getJdbcTemplate().query( |
| 56 | + "SELECT dvds.title, dvds.year, dvds.duration, " |
| 57 | + + "directors.name FROM dvds, directors " |
| 58 | + + "WHERE dvds.director_id = directors.id" |
| 59 | + + " ORDER BY dvds.title", |
| 60 | + new RowMapper<Dvd>() { |
| 61 | + @Override |
| 62 | + public Dvd mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 63 | + return new Dvd( |
| 64 | + rs.getString("title"), |
| 65 | + rs.getInt("year"), |
| 66 | + rs.getInt("duration"), |
| 67 | + rs.getString("name") |
| 68 | + ); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private Integer getDirectorId(String director) { |
| 74 | + if (hasNoDirector(director)) { |
| 75 | + saveDirector(director); |
| 76 | + } |
| 77 | + List ids = getJdbcTemplate().queryForList( |
| 78 | + "SELECT id FROM directors WHERE name=?", director); |
| 79 | + Map row = (Map) ids.get(0); |
| 80 | + return (Integer) row.get("id"); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean hasNoDirector(String director) { |
| 84 | + return getJdbcTemplate().queryForObject( |
| 85 | + "SELECT COUNT(*) FROM directors WHERE name=?", new Object[] {director}, Integer.class) == 0; |
| 86 | + } |
| 87 | + |
| 88 | + private void saveDirector(String director) { |
| 89 | + getJdbcTemplate().update( |
| 90 | + "INSERT INTO directors(name) values(?)", director); |
| 91 | + } |
| 92 | +} |
0 commit comments