hibernate 执行sql ,select 返回string long

来源:互联网 发布:谈谈你对数据库的认识 编辑:程序博客网 时间:2024/05/01 02:59

1.执行sql,返回一个结果集,适用查询一个字段并返回一条记录

[java] view plain copy
 print?
  1. public Long findSeqTbTest() {    
  2.     String sql = "select SEQ_TB_TEST.nextval from dual";    
  3.     SQLQuery query = this.getSession().createSQLQuery(sql);    
  4.     String str = query.uniqueResult().toString();    
  5.     return Long.valueOf(str);    
  6. }    
[java] view plain copy
 print?
  1. //获取表中最小的id    
  2. String sql = "select min(a.employeeid) from Emplyees a";    
  3. Long id = (Long) session.createQuery(sql).uniqueResult();    
  4.     
  5. //获取数据库时间mysql    
  6. String sql = "select now() from dual";    
  7. String time =  session.createSQLQuery(sql).uniqueResult().toString();    

2.删除、更新等操作,这里参数是从0开始的

[java] view plain copy
 print?
  1. public void deleteTbTest(Long id) {    
  2.     String hql = "DELETE FROM TbTest WHERE ID = ?";    
  3.     this.getSession().createQuery(hql).setLong(0, id).executeUpdate();    
  4. }    

[java] view plain copy
 print?
  1. public void updateTbTest(Date date, boolean flag) {    
  2.     String sql = "update tb_test set t_name=? where t_status!=1 and t_date" + (flag ? "<?" : "=?");    
  3.     SQLQuery query = this.getSession().createSQLQuery(sql);    
  4.     query.setString(0,flag ? "hello""hi").setDate(1, date).executeUpdate();    
  5. }    


3.执行sql,查询单表中多条数据

[java] view plain copy
 print?
  1. //尽量避免适用"*"    
  2. String sql = "select * from employee e where e.valid=1 and not exists (select employeeid from attendance a where a.employeeid=e.employeeid and a.date = ?)";   
  3.  SQLQuery query = getSession().createSQLQuery(sql);    
  4. query.addEntity(Employee.class).setDate(0, day);    
  5. List<Employee> retList = query.list();    

4.查询多表

[java] view plain copy
 print?
  1. String hql = "select new map(dept.deptID as deptID,dept.depNo as deptNo,dept.deptName as deptName,emp.empName as empName,emp.empID as empID,emp.empAge as age,emp.empNo as empNo) from Department dept ,Employee emp where dept.depID = emp.depID and emp.empName = ? and emp.sex = ?";    
  2. return getHibernateTemplate().find(hql, new Object[] { name, sex });   

每个字段都保存在map中(key是字段名,value是此字段的值如:[{empID=1,empName=leona,...},...])

5.查询多表

 

[java] view plain copy
 print?
  1. String sql = "select dept.deptID as deptID,dept.depNo as deptNo,dept.deptName as deptName,emp.empName as empName,emp.empID as empID,emp.empAge as age,emp.empNo as empNo,emp.birthday as birthday from Employee emp LEFT JOIN Department dept on dept.depID = emp.depID where empName = ?";    
  2.         return (List<EmpBean>) this.getSession()    
  3.             .createSQLQuery(sql)    
  4.             .addScalar("deptID", Hibernate.STRING)    
  5.             .addScalar("deptNo", Hibernate.STRING)    
  6.             .addScalar("deptName", Hibernate.STRING)    
  7.             .addScalar("empName", Hibernate.STRING)    
  8.             .addScalar("empID", Hibernate.STRING)    
  9.             .addScalar("age", Hibernate.LONG)    
  10.             .addScalar("birthday", Hibernate.DATE)    
  11.             .addScalar("empNo", Hibernate.STRING)    
  12.             .setString(0, empName)    
  13.             // 将结果集映射为EmpBean对象    
  14.             .setResultTransformer(Transformers.aliasToBean(EmpBean.class)).list();    
[java] view plain copy
 print?
  1. String hql = "from Attendance att where att.employeeid = ? and att.date =? ";   
  2. List<Attendance> list = this.getHibernateTemplate().find(hql,   
  3. new Object[] { employeeid, workDay });   
  4. if (null != list && !list.isEmpty()) {   
  5. return list.get(0);   
  6. }   
[java] view plain copy
 print?
  1. String queryString = "FROM Attendance a WHERE a.employeeid=? AND DATE_FORMAT(a.date,'%Y-%m')=DATE_FORMAT(?,'%Y-%m') ORDER BY a.teamname";   
  2. Query queryObject = getSession(). createQuery(queryString);   
  3. queryObject.setParameter(0, id);   
  4. queryObject.setParameter(1, date);   
  5. return queryObject.list();   
[java] view plain copy
 print?
  1. Session session = getSession();   
  2. session.clear();   
  3. getSession().saveOrUpdate(transientInstance);   


startBatch()的用法

 

[java] view plain copy
 print?
  1. public class LocalDaoImpl extends SqlMapClientDaoSupport implements LocalDao {   
  2.   
  3.     public void insertBuNaTaxBatLst(final PaginatedList list)   
  4.     {   
  5.          getSqlMapClientTemplate().execute(new SqlMapClientCallback() {   
  6.                 public Object doInSqlMapClient(SqlMapExecutor executor)   
  7.                         throws SQLException {   
  8.                     executor.startBatch();   
  9.                     // do some iBatis operations here   
  10.                     for(int i=0,count=list.size();i<count;i++)   
  11.                     {      
  12.                         executor.insert("insertBuNaTaxBatLst", list.get(i));   
  13.                         if (i % 50 == 0) {   
  14.                             System.out.println("----" + i);//没有意义只为测试   
  15.                         }   
  16.                     }   
  17.                     executor.executeBatch();   
  18.                     return null;   
  19.                 }   
  20.             });   
  21.     }   

0 0
原创粉丝点击