系统中数据库操作封装

封装数据的链接

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);

   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值