HQL查詢對象實戰詳解

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章