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

posted @ 2018-11-21 11:15  ryelqy  阅读(4066)  评论(0编辑  收藏  举报