1. 程式人生 > >Hibernate通過createSQLQuery( )方法實現增刪改查

Hibernate通過createSQLQuery( )方法實現增刪改查

 一、專案結構

二、hibernate核心配置檔案:   hibernate.cfg.xm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	<!-- 配置hibernate核心配置檔案 -->
	<hibernate-configuration>
		<!-- 配置hibernate資料來源連線 -->
		<session-factory>
			<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
			<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
			<property name="hibernate.connection.username">wzf</property>
			<property name="hibernate.connection.password">1234</property>
			
			<!-- 配置資料庫方言 -->
			<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
			<!-- 配置sql列印、格式化 -->
			<property name="hibernate.show_sql">true</property>
			<property name="hibernate.format_sql">true</property>
			
			<!-- 配置hibernate對映檔案位置 -->
			<mapping resource="com/gomai/pojo/student.hbm.xml"/>
		</session-factory>
	</hibernate-configuration>

三、hrbernate的對映檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <!-- 配置hibernate對映檔案 -->
    <hibernate-mapping>
    	<class name="com.gomai.pojo.Student" table="student">
    		<!-- 配置主鍵生成策略 -->
    		<id name="student_id" column="STUID">
    			<generator class="sequence">
    				<param name="sequence">SQ_STUDENT</param>
    			</generator>
    		</id>
    		<!-- 配置表與屬性 -->
    		<property name="student_name" column="stuname"></property>
    		<property name="student_age" column="stuage"></property>
    		<property name="student_sex" column="stusex"></property>
    		<property name="student_no" column="stuno"></property>
    	</class>
    </hibernate-mapping>

 四、測試類(該類包括增刪改查四個方法的實現,下面依次介紹)

新增:

	/**
	 * 新增:通過序列生成主鍵自增
         */
	@Test
	public void insertStu(){
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		//開啟事務
		Transaction tr = session.beginTransaction();
	
		int i = session.createSQLQuery("insert into student values(SQ_STUDENT.nextval,?,?,?,?)")
			.setParameter(0, 2)
			.setParameter(1, "露娜")
			.setParameter(2, 23)
			.setParameter(3, "女")
			.setParameter(4, 1003)
			.executeUpdate();
		
		System.out.println(i);
		try {
			tr.commit();
		} catch (HibernateException e) {
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

 刪除:

/**
	 * 刪除
	 */
	@Test
	public void deleteStu(){
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		//開啟事務
		Transaction tr = session.beginTransaction();
		
		int i = session.createSQLQuery("delete from student where stuid = ?")
				.setParameter(0, 100)
				.executeUpdate();
		System.out.println("TestSQL.deleteStu()" + i);
		
		//事務回滾、關流
		try {
			tr.commit();
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

更新:

	/**
	 * 更新
	 */
	@Test
	public void updateStu(){
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		int i = session.createSQLQuery("update student set stuname = ?,stusex = ? where stuid = ? ")
			.setParameter(0, "公孫離")
			.setParameter(1, "女")
			.setParameter(2, 9)
			.executeUpdate();
		
		System.out.println(i);
		
		//事務回滾、關流
		try {
			tr.commit();
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

查詢: 

/**
	 * 查詢
	 */
	@Test
	public void searchStu(){
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		
		List<Student> list = session.createSQLQuery("select * from student")
				.addEntity(Student.class)
				.list();
		
		for (Student student : list) {
			System.out.println(student);
		}
		session.close();
		
	}

原文出處: