美文网首页
关于Hibernate支持原生sql的写法

关于Hibernate支持原生sql的写法

作者: vpike | 来源:发表于2019-07-17 15:24 被阅读0次
    StringBuffer sql=new StringBuffer("SELECT * FROM edu_video");
            sql.append(" WHERE videoPath = '" + videoPath + "'");
            Query query =  getSession().createSQLQuery(sql.toString());
            query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
    
    

    StringBuffer sql=new StringBuffer("SELECT * FROM edu_video");
            sql.append(" WHERE videoPath = '" + videoPath + "'");
            Query query =  getSession().createSQLQuery(sql.toString());
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    
    

    其下为遍历

    if(query.list() != null && query.list().size() > 0) {
                Map<String, Object> map = (Map<String, Object>) query.list().get(0);
                return map;
                
            }
    
    

    Hibernate执行sql 修改

    String sql="update EduVideo video set video.online="+online+" where video.videoPath='"+videoPath+"'";
            Query query = getSession().createQuery(sql);
            query.executeUpdate();
    
    
    package com.aowin.dao;  
      
    import java.util.List;  
      
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
      
    import com.aowin.model.User;  
      
    public class UserDao extends HibernateDaoSupport implements IUserDao{  
      
        public User findByUsernameAndPassword(String username, String password){  
            String hql = "from com.aowin.model.User u where u.username=? and u.password=?";  
    //      Query query = this.getSession().createQuery(hql);  
    //      query.setString(0, username);  
    //      query.setString(1, password);  
    //      List list = query.list();  
            List list = this.getHibernateTemplate().find(hql, new Object[]{username, password});  
            if(list.isEmpty()){  
                return null;  
            }else{  
                return (User) list.get(0);  
            }  
        }  
          
        public void deleteById(Integer id) {  
    //      User user = (User) this.getSession().get(User.class, id);  
    //      this.getSession().delete(user);  
            User user = (User) this.getHibernateTemplate().get(User.class, id);  
            this.getHibernateTemplate().delete(user);  
        }  
      
        public List<User> findAll() {  
            String hql = "from com.aowin.model.User";  
    //      return this.getSession().createQuery(hql).list();  
            return this.getHibernateTemplate().find(hql);  
        }  
      
        public User findById(Integer id) {  
            return (User) this.getHibernateTemplate().get(User.class, id);  
        }  
      
        public void save(User user) {  
            this.getHibernateTemplate().save(user);  
        }  
      
        public void update(User user) {  
            this.getSession().update(user);  
        }  
      
    }  
    

    相关文章

      网友评论

          本文标题:关于Hibernate支持原生sql的写法

          本文链接:https://www.haomeiwen.com/subject/qcoqlctx.html