封装数据的链接
package com.cloud.day2; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; public class BaseJdbcSupport { private HibernateTemplate hibernateTemplate; private JdbcTemplate jdbcTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } } |
封装常用的操作
package com.cloud.day2; import java.util.Collection; import java.util.List; public interface CommonInte { /** * 保存一个任意的对象 * @param obj * @throws Exception */ public void saveObject(Object obj) throws Exception; /** * 保存或者更新任意对象 * @param obj * @throws Exception */ public void saveOrUpdate(Object obj) throws Exception; /** * 编辑任意对象 * @param obj * @throws Exception */ public void updateObject(Object obj) throws Exception; /** * 保存或编辑任意对象 * @param objs * @throws Exception */ @SuppressWarnings("rawtypes") public void saveOrupdateColloection(Collection objs) throws Exception; /** * 删除任意对象 * @param obj * @throws Exception */ public void deleteObj(Object obj) throws Exception; /** * 通过ID获取对象 * @param obj * @param id * @return * @throws Exception */ @SuppressWarnings("rawtypes") public Object getObjectById(Class obj,String id) throws Exception; /** * 根据SQL语句查询结果集合 * @param sql * @return * @throws Exception */ @SuppressWarnings("rawtypes") public List getList(String sql) throws Exception; /** * 执行增删改的SQL语句 * @param sql * @throws Exception */ public void executeSQL(String sql) throws Exception; /** * 执行批量的增删改查SQL语句 * @param sql * @throws Exception */ public void executeBatchSQL(String[] sql) throws Exception; } |
实现封装接口
package com.cloud.day2; import java.util.Collection; import java.util.List; public class CommonService extends BaseJdbcSupport implements CommonInte{
@Override public void saveObject(Object obj) throws Exception { try { this.getHibernateTemplate().save(obj); } catch (Exception e) { e.printStackTrace(); throw new Exception("保存失败!"); } }
@Override public void saveOrUpdate(Object obj) throws Exception { try { this.getHibernateTemplate().saveOrUpdate(obj); } catch (Exception e) { e.printStackTrace(); throw new Exception(e.getMessage()); } }
@Override public void updateObject(Object obj) throws Exception { try { this.getHibernateTemplate().update(obj); } catch (Exception e) { e.printStackTrace(); throw new Exception(e.getMessage()); } }
@SuppressWarnings("rawtypes") @Override public void saveOrupdateColloection(Collection objs) throws Exception { this.getHibernateTemplate().saveOrUpdateAll(objs); }
@Override public void deleteObj(Object obj) throws Exception { this.getHibernateTemplate().delete(obj); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object getObjectById(Class obj, String id) throws Exception { Object pojo = (Object)this.getHibernateTemplate().get(obj, id); return pojo; }
@SuppressWarnings("rawtypes") @Override public List getList(String sql) throws Exception { return this.getJdbcTemplate().queryForList(sql); }
@Override public void executeSQL(String sql) throws Exception { this.getJdbcTemplate().execute(sql); }
@Override public void executeBatchSQL(String[] sql) throws Exception { this.getJdbcTemplate().batchUpdate(sql); } } |