📄 contactdao.java
字号:
/*
* @(#)ContactDao.java
*
* Copyright 2003 Copyright.com , Inc. All rights reserved.
* Use is subject to license terms.
*/
package sample.dao.impl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.object.MappingSqlQuery;
import sample.dao.IContactDao;
import sample.Contact;
import javax.sql.DataSource;
import java.sql.Types;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* <br>Title:
* <br>Description:
* <br>Copyright: Copyright.com (c) 2003
* <br>Company:
* History:
* create 2005-11-7 17:52:15
*
* @author youlq
* @version 1.0
*/
public class ContactDao extends JdbcDaoSupport implements IContactDao{
private ContactDelete contactDelete;
private ContactInsert contactInsert;
private ContactUpdate contactUpdate;
private ContactsAllQuery contactsAllQuery;
private ContactsByIdQuery contactsByIdQuery;
//~ Methods ================================================================
public Contact getById(Integer id){
List list=contactsByIdQuery.execute(id.intValue());
if(list.size()==0){
return null;
} else{
return (Contact)list.get(0);
}
}
public void create(Contact contact){
contactInsert.insert(contact);
}
public void delete(Integer contactId){
contactDelete.delete(contactId);
}
public List findAll(){
return contactsAllQuery.execute();
}
public void update(Contact contact) {
contactUpdate.update(contact);
}
protected void initDao() throws Exception{
contactInsert=new ContactInsert(getDataSource());
contactUpdate=new ContactUpdate(getDataSource());
contactDelete=new ContactDelete(getDataSource());
contactsAllQuery=new ContactsAllQuery(getDataSource());
contactsByIdQuery=new ContactsByIdQuery(getDataSource());
}
protected class ContactDelete extends SqlUpdate{
protected ContactDelete(DataSource ds){
super(ds, "DELETE FROM contacts WHERE id = ?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
protected void delete(Integer contactId){
super.update(contactId.intValue());
}
}
protected class ContactInsert extends SqlUpdate{
protected ContactInsert(DataSource ds){
super(ds, "INSERT INTO contacts ( CONTACT_NAME, EMAIL) VALUES (?,?)");
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
protected void insert(Contact contact){
Object[] objs=new Object[]{contact.getName(),contact .getEmail()};
super.update(objs);
}
}
protected class ContactUpdate extends SqlUpdate{
protected ContactUpdate(DataSource ds){
super(ds,
"UPDATE contacts SET contact_name = ?, email = ? WHERE id = ?"
);
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
protected void update(Contact contact){
Object[] objs=new Object[]{contact.getName(),contact.getEmail(),contact
.getId()};
super.update(objs);
}
}
protected class ContactsAllQuery extends MappingSqlQuery{
protected ContactsAllQuery(DataSource ds){
super(ds, "SELECT id, contact_name, email FROM contacts ORDER BY id");
compile();
}
protected Object mapRow(ResultSet rs, int rownum)
throws SQLException{
Contact contact=new Contact();
contact.setId(new Integer(rs.getInt("id")));
contact.setName(rs.getString("contact_name"));
contact.setEmail(rs.getString("email"));
return contact;
}
}
protected class ContactsByIdQuery extends MappingSqlQuery{
protected ContactsByIdQuery(DataSource ds){
super(ds,
"SELECT id, contact_name, email FROM contacts WHERE id = ? ORDER BY id"
);
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
protected Object mapRow(ResultSet rs, int rownum)
throws SQLException{
Contact contact=new Contact();
contact.setId(new Integer(rs.getInt("id")));
contact.setName(rs.getString("contact_name"));
contact.setEmail(rs.getString("email"));
return contact;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -