1.项目结构及目标数据库:
2.hql配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- jdbc -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- SQL dialect方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 在控制台输出sql -->
<property name="show_sql">true</property>
<!-- 重启程序时自动更新ddl和配置 -->
<property name="hbm2ddl.auto">update</property>
<property name="format_sql">true</property>
<!-- 映射文件 -->
<mapping resource="hbm/Emp.hbm.xml" />
<mapping resource="hbm/Dept.hbm.xml" />
</session-factory>
</hibernate-configuration>
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hxzy.hql.entity.Emp" table="emp"
lazy="true" select-before-update="true">
<!--主键,推荐使用interger,因为int初省值为0,可能出现没有赋值却在库中有值的情况-->
<id name="empno" column="empno" type="java.lang.Integer">
<generator class="increment" />
</id>
<!--各列映射-->
<property name="ename" column="ename" type="java.lang.String"/>
<property name="deptno" column="deptno" type="java.lang.Integer" />
<property name="job" column="job" type="java.lang.String" />
<property name="mgr" column="mgr" type="java.lang.String" />
<property name="hiredate" column="hiredate" type="java.lang.Integer" />
<property name="sal" column="sal" type="java.lang.Integer" />
<property name="comm" column="comm" type="java.lang.Integer" />
</class>
</hibernate-mapping>
3.对象
public class Emp {
private int empno;
private String ename;
private int deptno;
private String job;
private int mgr;
private String hiredate;
private int sal;
private String comm;
/**
* emp构造方法(empno,ename,deptno)
* */
public Emp(int empno, String ename, int deptno) {
super();
this.deptno = deptno;
this.empno = empno;
this.ename = ename;
}
。。。
}
4.查询代码:
dao层:
public class EmpDaoImpl {
private static SessionFactory sessionFactory;
private static Session session;
private static Transaction transaction;
/**
* 获取session
* */
public Session getSession() {
Configuration cfg = new Configuration().configure();//启动配置
sessionFactory = cfg.buildSessionFactory();//创建会话工厂
session = sessionFactory.openSession();//开启会话
return session;
}
/**
* 关闭资源
* */
public void destory(){
if(session!=null){//关闭会话
session.close();
}
if(sessionFactory!=null){//销毁会话工厂
sessionFactory.close();
}
}
/**
* select all
* */
public List<Emp> getEmpList(){
Session session = getSession();//获取会话
transaction = session.beginTransaction();//开启事务
List<Emp> empList = new ArrayList<Emp>();
String hql = "select new com.hxzy.hql.entity.Emp(emp.empno,emp.ename,emp.deptno) from Emp emp ";
try {
Query query = session.createQuery(hql);//创建hql query
empList = query.list();//查出结果集并接收
transaction.commit();//提交事务
}catch(Exception e) {
e.printStackTrace();//输出异常信息
transaction.rollback();//有异常则回滚
}finally {
destory();//关闭资源
}
return empList;
}
}
测试:
@Test
public void tet() {
EmpDaoImpl empDaoImpl = new EmpDaoImpl();
List<Emp> empList = empDaoImpl.getEmpList();
System.out.println(" deptno\tempno\tename");
for(Emp e: empList) {
System.out.println(e.getDeptno()+"\t"+e.getEmpno()+"\t"+e.getEname());
}
}
5.结果:
参考文档:https://download.csdn.net/download/olengyuehun/11082672
参考资料:https://blog.csdn.net/oLengYueHun/article/details/88974387