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();
		
	}

 

原文出處:

小白農, Hibernate通過createSQLQuery( )方法實現增刪改查, https://blog.csdn.net/w18706334163/article/details/79923466


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM