Hibernate插入對象實戰詳解

1.目標數據庫及maven目錄
在這裏插入圖片描述
在這裏插入圖片描述
2.設計:
對象dept:

private int deptno;
private String dname;
private String loc;

/**
	 * 構造dept方法
	 * */
	public Dept(int deptno,String dname,String loc) {
	//	spuer;
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}

3.xml:
映射文件

<class name="com.hxzy.hql.entity.Dept" table="dept"
		lazy="true" select-before-update="true">
		
		<!--此處主鍵是自定義,所以用assigned-->
		<id name="deptno" column="deptno" type="java.lang.Integer">
			<generator class="assigned" />
		</id>
		
		<property name="dname" column="dname" type="java.lang.String"/>
		<property name="loc" column="loc" type="java.lang.String" />
		
	</class>	
</hibernate-mapping>

配置文件hibernate.cfg.xml:

<session-factory>

		<!-- jdbc -->
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@192.168.1.74:1521: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>

4.test
dao層:

/**
	 * add dept
	 * */
	public void addDept(Dept dept) {
		Session session = BaseDao.getSession();//獲取session
		transaction = session.beginTransaction();//開啓事務
		// <sql-insert>INSERT INTO PERSON (NAME, ID) VALUES ( UPPER(?), ? )</sql-insert>
		String hql = "insert into new com.hxzy.entity.Dept dept (deptno,dname,loc) values (dept.deptno,dept.dname,dept.loc)";//
		try {
		//	session.createQuery(hql).executeUpdate();//執行sql
			session.save(dept);
			transaction.commit();//提交事務
		}catch(Exception e) {
			e.printStackTrace();
			transaction.rollback();//事務異常則回滾
		}
	}
	
	/**
	 * select all
	 * */
	public List<Dept> getDeptList(){
		Session session = BaseDao.getSession();//獲取會話
		transaction = session.beginTransaction();//開啓事務
		List<Dept> deptList = new ArrayList<Dept>();
		String hql = "select new com.hxzy.hql.entity.Dept(dept.deptno,dept.dname,dept.loc) from Dept dept ";
		try {
			Query query = session.createQuery(hql);//創建hql query
			deptList = query.list();//查出結果集並接收
			transaction.commit();//提交事務
		}catch(Exception e) {
			e.printStackTrace();//輸出異常信息
			transaction.rollback();//有異常則回滾
		}finally {
			destory();//關閉資源
		}
		return deptList;
	}

Test:

DeptDao deptDao = new DeptDaoImpl();
		Dept dept = new Dept(50, "gz", "hxyz");
		deptDao.addDept(dept);
		List<Dept> deptList = deptDao.getDeptList();
		System.out.println(" deptno\tdname\tloc");
		for(Dept d:deptList) {
			System.out.println(d.getDeptno()+"\t"+d.getDname()+"\t"+d.getLoc());
		}

輸出結果:
在這裏插入圖片描述

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