package com.surfbird.common.util; import java.sql.SQLException; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JdbcInstance { private static JdbcInstance instance; private static SqlSessionTemplate sql; private static ApplicationContext ctx; /** *单例模式,私有化本对象构造方法。 **/ private JdbcInstance(){}; /** * 提供访问该类对象访方法. * @return * @throws SQLException **/ public static JdbcInstance instance() throws SQLException { if(instance == null) { instance = new JdbcInstance(); ctx = new ClassPathXmlApplicationContext("classpath:config.xml"); sql = (SqlSessionTemplate)ctx.getBean("sqlSessionTemplate"); } return instance; } /** * 添加数据. * @param name * @return * @throws Exception **/ public static int save(String name)throws Exception{ JdbcInstance.instance(); return sql.insert(name); } /** * 添加数据. * @param name * @param object * @return * @throws Exception **/ public static int save(String name, Object object)throws Exception{ JdbcInstance.instance(); return sql.insert(name, object); } /** * 删除数据. * @param name * @return * @throws Exception **/ public static int clear(String name)throws Exception{ JdbcInstance.instance(); return sql.delete(name); } /** * 删除数据. * @param name * @param object * @return * @throws Exception **/ public static int clear(String name, Object object)throws Exception{ JdbcInstance.instance(); return sql.delete(name, object); } /** * 修改数据. * @param name * @return * @throws Exception **/ public static int edit(String name)throws Exception { JdbcInstance.instance(); return sql.update(name); } /** * 修改数据. * @param name * @param object * @return * @throws Exception **/ public static int edit(String name, Object object)throws Exception { JdbcInstance.instance(); return sql.update(name, object); } /** * 查询数据. * @param name * @return * @throws SQLException **/ public static Object show(String name) throws SQLException { JdbcInstance.instance(); return sql.selectOne(name); } /** * 查询数据. * @param name * @param object * @return * @throws SQLException **/ public static Object show(String name, Object object) throws SQLException { JdbcInstance.instance(); return sql.selectOne(name, object); } /** * 查询数据. * @param name * @return * @throws SQLException **/ public static List select(String name) throws SQLException { JdbcInstance.instance(); return sql.selectList(name); } /** * 查询数据. * @param name * @param object * @return * @throws SQLException **/ public static List select(String name, Object object) throws SQLException { JdbcInstance.instance(); return sql.selectList(name, object); } }