import java.sql.Types;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.SqlQuery;
class Main {
public static void main(String args[]) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
// DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");
customerDao.getAll();
customerDao.getById(1L);
}
}
class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao {
private SelectCustomerById selectCustomerById;
private static class SelectCustomerById extends SqlQuery {
SelectCustomerById(DataSource ds) {
super(ds, "select * from t_customer where id=?");
declareParameter(new SqlParameter(Types.INTEGER));
}
protected RowMapper newRowMapper(Object[] parameters, Map context) {
return ParameterizedBeanPropertyRowMapper.newInstance(Customer.class);
}
}
@Override
protected void initDao() throws Exception {
this.selectCustomerById = new SelectCustomerById(getDataSource());
}
@SuppressWarnings({"unchecked"})
public List<Customer> getAll() {
return getJdbcTemplate().query("select * from t_customer", ParameterizedBeanPropertyRowMapper.newInstance(Customer.class));
}
public Customer getById(long id) {
return (Customer)this.selectCustomerById.findObject(id);
}
}
interface CustomerDao {
List<Customer> getAll();
Customer getById(long id);
}
class Customer {
private Long id;
private String firstName;
private String lastName;
private Date lastLogin;
private String comments;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getLastLogin() {
return lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Customer");
sb.append("{id=").append(id);
sb.append(", firstName='").append(firstName).append('\'');
sb.append(", lastName='").append(lastName).append('\'');
sb.append(", lastLogin=").append(lastLogin);
sb.append(", comments='").append(comments).append('\'');
sb.append('}');
return sb.toString();
}
}
|