package springbook.user.domain; public class User { String id; String name; String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
필드명 | 타입 | 설정 |
UserId | VARCHAR(10) | Primary Key |
Name | VARCHAR(20) | Not Null |
Password | VARCHAR(20) | Not Null |
package springbook.user.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import springbook.user.domain.User; public class UserDao { public void add(User user) throws SQLException, ClassNotFoundException{ Class.forName("com.mysql.jdbc.Driver"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost/springbook", "spring", "book"); PreparedStatement ps = c.prepareStatement("insert into users(id, name, password) values(?,?,?)"); ps.setString(1, user.getId()); ps.setString(2, user.getName()); ps.setString(3, user.getPassword()); ps.executeUpdate(); ps.close(); c.close(); } public User get(String id) throws ClassNotFoundException, SQLException{ Class.forName("com.mysql.jdbc.Driver"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost/springbook", "spring", "book"); PreparedStatement ps = c.prepareStatement("select * from users where id = ?"); ps.setString(1, id); ResultSet rs = ps.executeQuery(); rs.next(); User user = new User(); user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); rs.close(); ps.close(); c.close(); return user; } }
public static void main(String[] args) throws SQLException, ClassNotFoundException { UserDao dao = new UserDao(); User user = new User(); user.setId("zeropage"); user.setName("제로페이지"); user.setPassword("zp"); dao.add(user); System.out.println(user.getId() + "등록 성공"); User user2 = dao.get(user.getId()); System.out.println(user2.getName()); System.out.println(user2.getPassword()); System.out.println(user2.getId() + "조회 성공"); }
zeropage 등록 성공 제로페이지 zp zeropage 조회 성공
public void add(User user) throws SQLException, ClassNotFoundException { Connection c = getConnection(); … } public void get(String id) throws SQLException, ClassNotFoundException { Connection c = getConnection(); … } private Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost/springbook", "spring", "book"); }
public class DaoFactory{ // ConnectionMaker를 설정하고 생성하는 코드가 반복됨 public UserDao userDao( ){ return new UserDao(new DConnectionMaker()); } public AccountDao accountDao( ){ return new AccountDao(new DConnectionMaker()); } public MessageDao messageDao( ){ return new MessageDao(new DConnectionMaker()); } }분리 전
public class DaoFactory{ public UserDao userDao( ){ return new UserDao(connectionMaker()); } public AccountDao accountDao( ){ return new AccountDao(connectionMaker()); } public MessageDao messageDao( ){ return new MessageDao(connectionMaker()); } // 중복 코드 분리 public ConnectionMaker connectionMaker(){ return new DConnectionMaker(); } }분리 후
@Configuration public class DaoFactory{}
@Configuration public class DaoFactory{ @Bean public UserDao userDao(){ ...} }
public static void main(Strings[] args) throws ClassNotFoundException, SQLException{ ApplicatioContext context = new AnnotationConfigApplicationContext(DaoFactory.class); }
public static void main(Strings[] args) throws ClassNotFoundException, SQLException{ ApplicatioContext context = new AnnotationConfigApplicationContext(DaoFactory.class); UserDao dao = context.getBean("userDao", UserDao.class); }
@Configuration class DaoFactory { ... @Bean public ConnectionMaker connectionMaker() { return new DConnectionMaker(); } @Bean userDao setConnectionMaker(connectionMaker()) { ... }; }
<beans> <bean id="myConnectionMaker" class="springbook.user.dao.DConnectionMaker" /> <bean id="userDao" class="springbook.dao.UserDao"> <property name="connectionMaker ref="myConnectionMaker" /> </bean> </beans>
ApplicationContext context = new GenericXmlApplicationContext("springbook/user/dao/daoContext.xml")