Hibernate 執行原始SQL語句

weixin_34067049發表於2016-11-27

hibernate中有時不需要用到表的對映,需要直接執行SQL語句。

其中sessionFactory在配置檔案中配置,SSH詳細配置見http://blog.csdn.NET/xumengxing/article/details/8586012

在dao層中定義兩個常見函式用於查詢和執行操作。

dao程式碼如下:

 
package com.hsinghsu.testSSH.dao.impl;    
    
import java.util.List;    
    
import javax.annotation.Resource;    
    
import org.hibernate.SQLQuery;    
import org.hibernate.Session;    
import org.hibernate.SessionFactory;    
import org.springframework.stereotype.Repository;    
    
import com.hsinghsu.testSSH.dao.BaseDao;    
    
@Repository(value = "baseDao")    
public class BaseDaoImpl implements BaseDao {    
    
    @Resource(name = "sessionFactory")    
    private SessionFactory sessionFactory;    
    
    public Session getSession() {    
        return sessionFactory.getCurrentSession();    
    }    
    
    public List queryBySql(String sql) {    
        List<Object[]> list = getSession().createSQLQuery(sql).list();    
        return list;    
    }    
        
    public int excuteBySql(String sql)    
    {    
        int result ;    
        SQLQuery query = this.getSession().createSQLQuery(sql);    
        result = query.executeUpdate();    
        return result;    
    }    
    
}    

service程式碼如下:

package com.hsinghsu.testSSH.service.impl;    
    
import java.util.List;    
    
import org.springframework.beans.factory.annotation.Autowired;    
import org.springframework.stereotype.Service;    
    
import com.hsinghsu.testSSH.dao.BaseDao;    
import com.hsinghsu.testSSH.service.BaseService;    
    
@Service(value = "baseService")    
public class BaseServiceImpl implements BaseService {    
    @Autowired    
    private BaseDao baseDao;    
    
    public List queryBySql(String sql) {    
        List<Object[]> list = baseDao.queryBySql(sql);    
        return list;    
    }    
        
    public int excuteBySql(String sql){    
        return baseDao.excuteBySql(sql);    
    }    
}    

測試程式碼如下:

package com.hsinghsu.testSSH.service;    
    
import static org.junit.Assert.assertNotNull;    
import static org.junit.Assert.assertTrue;    
    
import java.util.List;    
    
import org.junit.Test;    
import org.springframework.beans.factory.annotation.Autowired;    
    
import com.hsinghsu.testSSH.SpringTestBase;    
    
public class BaseServiceTest  extends SpringTestBase{    
    @Autowired    
    private BaseService baseService;    
        
    @Test    
    public void testConfig() {    
         assertNotNull(baseService);    
         assertTrue(baseService != null);    
    }    
        
    @Test    
    public void testQueryBySql()    
    {    
        String sql = "select * from T_TEST_USER WHERE SEX = 1";    
        List<Object[]> list = baseService.queryBySql(sql);    
        System.out.println("---"+list.size());    
        for(Object[] obj :list)    
        {    
            System.out.println(obj[0]+" -- "+ obj[1]+" -- "+obj[2]);    
        }    
    }    
        
    @Test    
    public void testExcuteBySql()    
    {    
//      String sql = "UPDATE T_TEST_USER SET NAME = 'GSS09', PWD = 'GG007', AGE = 24, SEX = 1 WHERE ID = 6";    
//      String sql = "UPDATE T_TEST_USER SET NAME = 'GSS29', PWD = 'GG007' WHERE ID = 6";    
//      String sql = "insert into T_TEST_USER(ID,NAME,PWD,AGE,SEX) values(7,'gogo08','gogopwd08',24,1)";    
        String sql = "delete from T_TEST_USER where ID = 7";    
    
        int result = baseService.excuteBySql(sql);    
        System.out.println("---009:"+result);    
    }    
           
}    

 

相關文章