当前位置: 代码迷 >> SQL >> 在hibernate框架中施用sql语句
  详细解决方案

在hibernate框架中施用sql语句

热度:171   发布时间:2016-05-05 15:19:38.0
在hibernate框架中使用sql语句
public static List<Article> getArticleByArticleIds(String articleIds){
System.out.println("开始执行sql "+new Date());
Configuration cfg = new Configuration().configure();
    SessionFactory sf = cfg.buildSessionFactory();
    Session sessions = sf.openSession();
List<Article> list= new ArrayList<Article>();
String sql="select id,title,summary,ori_publish_time as oriPublishTime,gettime,keywords," +
"_content as content,ori_address as oriAddress,site_address as siteAddress,cfg_worker as cfgWorker,site_name as siteName from t_article where id in ("+articleIds+") ";

SQLQuery sqlQuery = sessions.createSQLQuery(sql);
sqlQuery.setResultTransformer(Transformers
.aliasToBean(Article.class));
List<Article> list1 = sqlQuery.
                                   addScalar("id",Hibernate.INTEGER).
                                   addScalar("title",Hibernate.STRING).
                                   addScalar("summary",Hibernate.STRING).
                                   addScalar("oriPublishTime",Hibernate.TIMESTAMP).
                                   addScalar("gettime",Hibernate.TIMESTAMP).
                                   addScalar("keywords",Hibernate.STRING).
                                   addScalar("content",Hibernate.STRING).
                                   addScalar("oriAddress",Hibernate.STRING).
                                   addScalar("cfgWorker",Hibernate.STRING).
                                   addScalar("siteName",Hibernate.STRING).
                                   list();
return list1;

}
  相关解决方案