2//生成方法 通过任意键查询对象。
public List<IdCard> findIdCards(BigDecimal guestId) {
IdCardExample example = new IdCardExample();
example.createCriteria().andGuestIdEqualTo(guestId);
return idCardMapper.selectByExample(example);
}
2:通过spring连接现有的数据 连接包为sqljdbc4.jar
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
BasicDataSource bds = bf.getBean("dataSource",BasicDataSource.class);
bds.getConnection();
3:通过spring连接现有的数据 连接包为c3p0-0.9.1.2.jar
//数据库连接
public static Connection getConn(){
// BeanFactory bf = new FileSystemXmlApplicationContext(path+"/applicationContext.xml"); // 失败
// BeanFactory bf = new ClassPathXmlApplicationContext("classpath:/WEB-INF/applicationContext.xml");// 失败
// ServletActionContext.getServletContext();
WebApplicationContext wac = (WebApplicationContext)ServletActionContext.getServletContext().getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
DataSource bds = (DataSource)wac.getBean("dataSource");//配置文件中的数据库连接
try {
return bds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}