Java (웹) 응용 프로그램에서 데이터베이스의 DAO 구현 작업을하고 있습니다. 오직 나는 약간의 문제에 부딪쳤다.Java/sql 실험용 DAO 구현
는내 현재 코드 :
Account.java는 :
package beans;
public class Account {
private int accountId;
private String name;
private String password;
private String email;
public Account() {
}
public Account(final String name, final String password, final String email) {
this.name = name;
this.password = password;
this.email = email;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
DAO.java :
는package dao;
import java.util.Collection;
public interface DAO<T> {
public int insert(T t);
public boolean update(T t);
public T get();
public Collection<T> search();
public boolean delete(T t);
}
AccountDAO.java는 :
package dao;
import beans.Account;
import java.util.Collection;
public class AccountDAO implements DAO<Account> {
@Override
public int insert(Account t) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean update(Account t) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Account get() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Collection<Account> search() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean delete(Account t) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
건입니다 I 나는 아무 것도하지 않는다고 확신한다. 이미 DAO
인터페이스에있는 SQL 문자열을 사용하려고하지만, get()
및 search()
을 올바르게 구현하는 방법에 대한 문제가 있습니까?
제네릭 인터페이스이므로 열 이름을 사용할 수 없으며 구현하기 위해 인터페이스 내에 get()
및 search()
메서드를 유지하고 싶습니다. 실제로 제거해야하는 경우가 아니면 인터페이스에서.
내 제안 제안 범위 나 같은하지-완전히 당신이 이름 =의 X와 계정을 얻고 싶은 것이다 그래서 만약 클래스 T.의 객체 작성, 당신은 것 코드로 검색하는 것입니다
AccountDAO accountDAO = DAOFactory.getAccountDAO();
Account result = accountDAO.get(new Account(x, null, null));
하지만 이것이 최선의 해결책이 될지 확실하지 않습니다. 제발 도와주세요.
감사합니다.
내가 SQL 쿼리를 받아들이는 추상 클래스NativeQueryBasedDAO
을 만들어 :
휠을 다시 구현해야하는 이유는 무엇입니까? 기존 ORM이 문제를 해결하지 않습니까? – Thihara
@Thihara 나는 마스터하기가 어렵다는 것을 안다. 나는 인터넷에서 이미 주변을 둘러 보았지만, 모두 그렇게 어려운 것처럼 보인다. 나는 곧 나의 신청서를 가지고 시작하고 싶다. 그리고 내가이 부분을 얻으려면 지금은 충분하다고 생각할 것이다. – skiwi
당신의 해결책이 분명해 보입니다. 그게 잘못이라고 생각하니? – inigoD