1.增,增在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("插入成功");
}
}
}
2. 删,删在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("删除成功");
}
}
}
3. 改,改在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("更改成功");
}
}
}
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();
4. 查,查在SQL中是select,在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();
}
}
//导入所需的包
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("插入成功");
}
}
}
2. 删,删在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("删除成功");
}
}
}
3. 改,改在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("更改成功");
}
}
}
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();
4. 查,查在SQL中是select,在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();
}
}