DAO, 모호한 쿼리 구현 방법
public class DAO {
private QueryRunner queryRunner = new QueryRunner();
private Class clazz;
public DAO(){
Type superClass = getClass().getGenericSuperclass();
if(superClass instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) superClass;
Type[] typeArgs = parameterizedType.getActualTypeArguments();
if(typeArgs != null && typeArgs.length>0){
if(typeArgs[0] instanceof Class){
clazz = (Class) typeArgs[0];
}
}
}
}
/**
* T List
* T List
* @param sql
* @param args
* @return
*/
public List getForList(String sql,Object...args){
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanListHandler(clazz), args);
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtils.releaseConnection(connection);
}
return null;
}
}
커넥터CustomerDAO 정의
public interface CustomerDAO {
public List getForListWithCCustomer(CCustomer cc);
/**
* list
* @return
*/
public List getAll();
}
클래스 정의
public class CCustomer {
private String name;
private String address;
private String phone;
public CCustomer(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
//
if(name == null){
name ="%%";
}else{
name ="%"+ name +"%";
}
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
//
if(address == null){
address ="%%";
}else{
address ="%"+ address +"%";
}
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
//
if(phone == null){
phone ="%%";
}else{
phone ="%"+ phone +"%";
}
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
定义一个继承了DAO 且实现了CustomerDAO 的类
public class CustomerDAOJdbcImpl extends DAO
implements CustomerDAO{ @Override public List getForListWithCCustomer(CCustomer cc) { String sql = "SELECT id,name,address,phone FROM customerss " + "WHERE name LIKE ? AND address LIKE ? AND phone LIKE ?"; return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone()); } @Override public List getAll() { String sql = "SELECT id,name,address,phone FROM customerss"; return getForList(sql); } }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Confluence에서 PlantUML을 사용하여 ER 다이어그램을 텍스트 기반으로 작성외래 키 제약이 완벽하게 작성된 환경이라면, 리버스 엔지니어링으로 자동으로 ER 다이어그램을 작성해 주는 툴은 상당히 있을까 생각합니다. 그러나 외래 키 제약 조건이 유지되지 않는 환경에서는 수동으로 관계를 결정해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.