hibernate中的增删改查实现代码
所属分类:
网络编程 / JSP编程
阅读数:
1319
收藏 0赞 0分享
第一个我们首先看看增,增在SQL里面就是insert,也就是插入,在hibernate中,我们只需要,操纵一个对象进行sava,然后再commit事务,就能实现插入功能,下面给大家具体看看代码,持久类我就不再写了,里面也就是与数据库中的字段要一一对应的东西,要有set,get方法,我直接就写的怎么调用save方法。
//导入所需的包
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserTest {
public static void main(String args[]){
Configuration cfg = new Configuration().configure(); //获取hibernate的配置信息
SessionFactory sf = cfg.buildSessionFactory(); //根据config建立sessionFactory
Session ses = sf.openSession(); //factory用于建立session,开启Session,相当于开启JDBC的Connection
Transaction ts = ses.beginTransaction(); //创建事务的对象ts
User user = new User(); //持久化对象
user.setName("kobe");
user.setTel("111111111");
try {
ses.save(user);
ts.commit();
}catch (HibernateException he){
he.printStackTrace();
ts.rollback();
}finally{
ses.close();
sf.close();
System.out.println("插入成功");
}
}
}
第二个我们看看删,删在SQL里面是delete,也就是删除,同样在hibernate中,我们也是只需要调用一个对象,调用delete方法,就能进行删除。
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DeleteTest {
public static void main(String args[]){
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
Transaction ts = ses.beginTransaction();
User user = new User();
user.setId("8a8308891e9c3ef3011e9c3ef4aa0001");
try {
ses.delete(user);
ts.commit();
}catch (HibernateException he){
he.printStackTrace();
ts.rollback();
}finally{
ses.close();
sf.close();
System.out.println("删除成功");
}
}
}
具体中间的含义参照sava方法,这里我们要注意一点,我们调用删除的时候,他删除的条件,也就是where后面的条件一定是我们xml中配置id,通过这个来进行查找删除,这里尤其值得注意,也就是,我这里调用的user.setId(" ");这句话,他是通过""中的内容进行删除的。
第三个我们看看改,改在SQL中update,在hibernate中,我们同样只需要操作一个对象进行更改信息。
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UpdateTest {
public static void main(String args[]){
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
Transaction ts = ses.beginTransaction();
User user = new User();
user.setId("8a8308891e9c3ef3011e9c3ef4aa0001");
user.setName("kobe24");
try {
ses.update(user);
ts.commit();
}catch (HibernateException he){
he.printStackTrace();
ts.rollback();
}finally{
ses.close();
sf.close();
System.out.println("更改成功");
}
}
}
但是这里我们有需要注意的地方了,如果有的朋友用过这个update就会发现,调用这个方法的时候他更新的不只是你想更新的数据,你不想更新的数据,他也会随着改变,如果你没有给他set值,他就会出现null,或者表格中什么都没有,这里我们就需要用另一种方法了,去更新你想更新的数据,而你不想改变的数据还会保持原来的状态,这里我们就需要调用一个方法。
Session ses = sf.openSession();
Transaction ts = ses.beginTransaction();
User user = (User)ses.get(User.class,"8a8308891e9c3ef3011e9c3ef4aa0001");
user.setName("kobe24");
try {
ses.update(user);
ts.commit();
这样我们就会发现,我们只更新了我们想要更新的数据。ses不光光有这一个get方法,相同功能他还有一个load方法,两个方法功能是相同的但是有什么区别呢,区别就是用load方法时候他是从缓存中查找,而我们调用get方法的时候是从数据库中查找,不过get方法他也是先从缓存中查找,如果没有在去数据库中查找。
第三个我们看看查,查在SQL中是select,在hibernate中我们查询的时候有多种方法,这里我就写一种hibernate比较提倡的方法,就是HQL。用这个方法时候我们尤其需要注意的是他其中的from跟的不是表名,而是类名。
package hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SeleteTest {
public static void main(String args[]){
Configuration cfg=new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session ses=sf.openSession();
Transaction tx=ses.beginTransaction();
User user = new User();
Query query = ses.createQuery("from User");
List users = query.list(); //序列化
Iterator it = users.iterator(); //迭代
while (it.hasNext()){
user = (User) it.next();
System.out.println(user.getName()+" "+user.getTel()+" ");
}
ses.close();
sf.close();
}
}
Java Web实现的基本MVC实例分析
这篇文章主要介绍了Java Web实现的基本MVC,以完整实例形式较为详细的分析了JSP实现MVC架构的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0赞 0分享
jsp中调用java代码小结
大多数情况下, jsp 文档的大部分由静态文本(html)构成, 为处理该页面而创建的 servlet 只是将它们原封不动的传递给客户端
收藏 0赞 0分享
struts2中一个表单中提交多个请求的例子(多个提交按钮)
在很多Web应用中,为了完成不同的工作,一个HTML form标签中可能有两个或多个submit按钮,Struts2中提供了另外一种方法,使得无需要配置可以在同一个action类中执行不同的方法(默认执行的是execute方法)
收藏 0赞 0分享
servlet中session简介和使用例子
在servlet中,session是封装在javax.servlet.http.HttpSession这个接口中的,这个接口是构建在cookie或者URL重写的基础上,要得到一个HttpSession的实例,就可以通过HttpServletRequest的getSession()
收藏 0赞 0分享
查看更多