package com.wm.daoTest;
import java.sql.CallableStatement;
import java.sql.Connection;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import com.wm.commune.entity.CommuneCategoryEntity;
import com.wm.dao.dynamic.CustomerContextHolder;
import com.wm.dao.dynamic.CustomerType;
public class DBFunctionTest {
public ApplicationContext appContext;
private HibernateTemplate hibernateTemplate;
@Before
public void before(){
CustomerContextHolder.setCustomerType(CustomerType.COMMUNE_DB);
appContext = new ClassPathXmlApplicationContext("spring/ApplicationContext-base.xml");
hibernateTemplate = appContext.getBean(HibernateTemplate.class);
}
@Test
public void testHibernate(){
CommuneCategoryEntity comm = hibernateTemplate.get(CommuneCategoryEntity.class, 100000);
System.out.println(comm);
}
@Test
public void testFunction() throws Exception {
Connection con = SessionFactoryUtils.getDataSource(hibernateTemplate.getSessionFactory()).getConnection();
String procedure = "{call delete_category(?)}";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setInt(1, 100000);
int num = cstmt.executeUpdate();
System.out.println(num);
}
}
postgresql数据库中的函数为:
CREATE OR REPLACE FUNCTION delete_category(cid integer)
RETURNS void AS
$BODY$
DECLARE rid integer;
begin
select $1-$1%10000 into rid;
delete from wm_commune_category where id>rid;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION delete_category(integer)
OWNER TO wm_psql;