-
String str = query.uniqueResult().toString();
-
return Long.valueOf(str);
-
}
-
//获取表中最小的id
-
String sql = “select min(a.employeeid) from Emplyees a”;
-
Long id = (Long) session.createQuery(sql).uniqueResult();
-
//获取数据库时间mysql
-
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 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) 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)
-
// 将结果集映射为EmpBean对象
-
.setResultTransformer(
-
Transformers.aliasToBean(EmpBean.class))
-
.list();
-
String hql = "from Attendance att where att.employeeid = ? and att.date =? ";
-
List 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);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
ava开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!**
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!