hibernate的hql语言实现增删改查

时间:2021-06-07 00:24:24

oracle下建表test

create table(stuname char(32),

stuno varchar(50),

sex char(1),

cource varchar(50),

grade number(18));

新建Java工程

在src下建包stu

在stu下建class Student

package stu;
public class Student {
private String stuNo;
private String stuName;
private String sex;
private String cource;
private Long grade;
public Student(){}
public Student (String stuNo,String stuName){
this.stuNo = stuNo;
this.stuName = stuName;
}
public Student (String stuNo,String stuName,String sex,String course,Long grade){
this.stuNo = stuNo;
this.stuName = stuName;
this.cource = course;
this.grade = grade;
this.sex = sex;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCource() {
return cource;
}
public void setCource(String cource) {
this.cource = cource;
}
public Long getGrade() {
return grade;
}
public void setGrade(Long grade) {
this.grade = grade;
}

}
在stu下建class test

package stu;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.IteratorImpl;

public class test {
@SuppressWarnings("unused")
public static void main(String args[])throws Exception{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Student s = new Student();//从这里开始是插入数据,建了一个student对象s,给s的stuno,stuname等属性赋值
s.setStuNo("1");
s.setStuName("zy");
s.setSex("m");
s.setCource("computer");
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.save(s);//保存了对象s
tx.commit();
System.out.println("新增记录一条");
Student s2 = new Student();//建新的对象,再给新对象的属性赋值
s2.setStuNo("2");
s2.setStuName("xiaoluo");
s2.setCource("music");
session.save(s2);//一定要保存
Transaction tx2 = session.beginTransaction();
tx2.commit();//而且要提交
System.out.println("新增记录2条");
Query query= session.createQuery("select stuName,stuNo,cource from Student");//这里开始是查询,要注意大小写,Student类里面private的那些是怎么写的这里就要怎么写,hql不像sql什么都不区分大小写,关键字可以SELECT 或者select或者SeLeCt都随你,但是类名,属性都要区分大小写
List list = query.list();//用来保存查询到的数据
Iterator it = list.iterator();
int i=0;//就是计数的,看查到了几个数据,不要也行
while (it.hasNext()){
i++;
Object[] student =(Object[]) it.next();//object[]是个数组,可以是任何类型,什么String啊int的,不用强转,但是这里为什么强转了呢,就/ //像是一个是int,一个是int的一个数组,这里的it是object,student是object[],所以要强转。
System.out.println("name: "+student[0]);//数组第一个值就是select到的stuname,以此类推
System.out.println("stuno: "+student[1]);
System.out.println("cource: "+student[2]);
}
System.out.println(i+"****");//输出查到了几个数据
Transaction tx3 = null;//这里开始是update
try{
tx3 = session.beginTransaction();
String hql = "update Student s set s.stuNo=100 where s.stuName='xiaoluo'";//这里要注意Student是我们建的Java类的名字,而不是数据库的表的名字test,
Query query2 = session.createQuery(hql);
query2.executeUpdate();
tx3.commit();
tx3 = null;
System.out.println("成功更改");
}
catch(Exception e){
e.printStackTrace();
if(tx3!=null){
tx3.rollback();
}
}
Transaction tx4 = null;//这里开始是删除
try{
tx4 = session.beginTransaction();
String hql2 = "delete Student s where s.stuName='zy'";
Query query2 = session.createQuery(hql2);
query2.executeUpdate();
tx4.commit();
tx4 = null;
System.out.println("成功删除");
}
catch(Exception e){
e.printStackTrace();
if(tx4!=null){
tx4.rollback();
}
}
/*Transaction tx5 = null;//你以为这是insert吗?哈哈,错了,hql没有insert,我也是试了才知道
try{
tx5 = session.beginTransaction();
String hql3 = "insert into Student s(s.stuName,s.stuNo) values('zhangsan','007')";
Query query3 = session.createQuery(hql3);
query3.executeUpdate();
tx5.commit();
tx5 = null;
System.out.println("成功插入一条数据");
}
catch(Exception e){
e.printStackTrace();
if(tx5!=null){
tx5.rollback();
}
}*/
finally{
session.close();
sf.close();
}
}

}
在stu下建student.cfg.xml

<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="stu">
<class name = "Student" table="test" >
<id name = "stuNo" type="java.lang.String"></id>
<property name="stuName" type ="java.lang.String"></property>
<property name="sex" type ="java.lang.String"></property>
<property name="cource" type = "java.lang.String"></property>
<property name = "grade" type ="java.lang.Long" ></property>
</class>
</hibernate-mapping>

在src下建hibernate.cfg.xml

<?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>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<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.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<mapping resource="stu/student.hbm.xml"/>

</session-factory>
</hibernate-configuration>
运行test,搞定~

注意:如果你每次运行调试的过程中都要手动在oracle里面delete数据的话,一定要记得commit,不然你会发现你没运行一次,那个计数的i都会加2,而且输出的查询出来的数据也奇多,你确实delete了,再select也确实没有数据了,但是你没有commit就是没有彻底删掉,如果不懂可以参见我另一篇叫数据库简单笔记整理的文章里面第7条有写。

如果你直接把代码粘过去肯定报错,因为没有build path,要建一个文件夹叫lib,然后把所有需要的jar包复制进去,然后右键build path,再configure build path,再add jar,选你的项目名,找到lib,然后选中所有jar包,然后ok,就可以运行test了


今天也是收获满满的一天~

ps:好好的520,521,人家都和男朋友出门happy,我却在这里学习,唉,,,我爱学习,学习使我快乐!!!