hibernate 执行sql ,select 返回string long
来源:互联网 发布:谈谈你对数据库的认识 编辑:程序博客网 时间:2024/05/01 02:59
1.执行sql,返回一个结果集,适用查询一个字段并返回一条记录
- public Long findSeqTbTest() {
- String sql = "select SEQ_TB_TEST.nextval from dual";
- SQLQuery query = this.getSession().createSQLQuery(sql);
- String str = query.uniqueResult().toString();
- return Long.valueOf(str);
- }
-
- String sql = "select min(a.employeeid) from Emplyees a";
- Long id = (Long) session.createQuery(sql).uniqueResult();
-
-
- String sql = "select now() from dual";
- String time = session.createSQLQuery(sql).uniqueResult().toString();
2.删除、更新等操作,这里参数是从0开始的
- public void deleteTbTest(Long id) {
- String hql = "DELETE FROM TbTest WHERE ID = ?";
- this.getSession().createQuery(hql).setLong(0, id).executeUpdate();
- }
- public void updateTbTest(Date date, boolean flag) {
- String sql = "update tb_test set t_name=? where t_status!=1 and t_date" + (flag ? "<?" : "=?");
- SQLQuery query = this.getSession().createSQLQuery(sql);
- query.setString(0,flag ? "hello": "hi").setDate(1, date).executeUpdate();
- }
3.执行sql,查询单表中多条数据
-
- 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 = ?)";
- SQLQuery query = getSession().createSQLQuery(sql);
- query.addEntity(Employee.class).setDate(0, day);
- List<Employee> retList = query.list();
4.查询多表
- 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 = ?";
- return getHibernateTemplate().find(hql, new Object[] { name, sex });
每个字段都保存在map中(key是字段名,value是此字段的值如:[{empID=1,empName=leona,...},...])
5.查询多表
- 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 = ?";
- return (List<EmpBean>) this.getSession()
- .createSQLQuery(sql)
- .addScalar("deptID", Hibernate.STRING)
- .addScalar("deptNo", Hibernate.STRING)
- .addScalar("deptName", Hibernate.STRING)
- .addScalar("empName", Hibernate.STRING)
- .addScalar("empID", Hibernate.STRING)
- .addScalar("age", Hibernate.LONG)
- .addScalar("birthday", Hibernate.DATE)
- .addScalar("empNo", Hibernate.STRING)
- .setString(0, empName)
-
- .setResultTransformer(Transformers.aliasToBean(EmpBean.class)).list();
- String hql = "from Attendance att where att.employeeid = ? and att.date =? ";
- List<Attendance> list = this.getHibernateTemplate().find(hql,
- new Object[] { employeeid, workDay });
- if (null != list && !list.isEmpty()) {
- return list.get(0);
- }
- String queryString = "FROM Attendance a WHERE a.employeeid=? AND DATE_FORMAT(a.date,'%Y-%m')=DATE_FORMAT(?,'%Y-%m') ORDER BY a.teamname";
- Query queryObject = getSession(). createQuery(queryString);
- queryObject.setParameter(0, id);
- queryObject.setParameter(1, date);
- return queryObject.list();
- Session session = getSession();
- session.clear();
- getSession().saveOrUpdate(transientInstance);
startBatch()的用法
- public class LocalDaoImpl extends SqlMapClientDaoSupport implements LocalDao {
-
- public void insertBuNaTaxBatLst(final PaginatedList list)
- {
- getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
- public Object doInSqlMapClient(SqlMapExecutor executor)
- throws SQLException {
- executor.startBatch();
-
- for(int i=0,count=list.size();i<count;i++)
- {
- executor.insert("insertBuNaTaxBatLst", list.get(i));
- if (i % 50 == 0) {
- System.out.println("----" + i);
- }
- }
- executor.executeBatch();
- return null;
- }
- });
- }
0 0
- hibernate 执行sql ,select 返回string long
- hibernate 执行sql ,select 返回string long
- hibernate 执行sql ,select 返回String ,Long
- hibernate 执行sql ,select 返回string long
- hibernate 执行sql ,select 返回string long
- hibernate 执行原生sql,select返回string,long
- 关于Hibernate select count(*)返回值为Long还是Integer?
- Hibernate select count(*)返回值类型Integer vs Long
- Hibernate 3.2 SQL函数返回类型从Integer变为Long
- Hibernate.String/LONG 代替
- Hibernate.String/LONG 代替
- hibernate执行SQL返回自定义Bean对象
- 关于Hibernate读取select count(*)的返回值到底是Long还是Integer
- Hibernate执行sql语句 返回可遍历list
- Hibernate 执行SQL语句的count函数返回BigDecimal类型
- hibernate 原生sql返回结果执行AddEntity方法
- hibernate 原生sql返回结果执行AddEntity方法
- Hibernate执行自定义sql 返回一个list展示在页面
- 桥基站设备通信协议-AIS
- 写一个Perl包来负责流程调用软件
- 彻底解决maven下载慢的问题
- elasticsearch三个重要的优化
- openMP之二parallel Construct
- hibernate 执行sql ,select 返回string long
- Android中使用MediaCodec硬件解码,高效率得到YUV格式帧,快速保存JPEG图片(不使用OpenGL)(附Demo)
- 钽电容和铝电解电容的区别
- C语言实现:查找一个整数中1的个数
- Java 8系列之Stream的强大工具Collector
- Struts2(二)action的三种方式
- Jenkins持续集成+蒲公英上传内测遇到的问题及解决方法
- Android PopupWindow 隐藏软键盘的方法
- shared_ptr智能指针源码剖析