实体类关联数据库字段,操作实体类,HQL语句对数据结构CRUD)
引入jar包
配置文件
hibernate.cfg.xml
User.hbm.xml
SEQ_ID
增
package com.book.test;import java.util.Date;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.book.entity.User;public class AddTest { public static void main(String[] args) { Configuration conf = null; SessionFactory sessionFactory =null; Session session = null; Transaction tx = null; try { //解析配置文件 conf = new Configuration().configure(); //创建解析工厂 sessionFactory= conf.buildSessionFactory(); //打开session session = sessionFactory.openSession(); //开启事务 tx = session.beginTransaction(); //创建对象 User user = new User("Tom","adsf123",0,0,"sdsad@qq.com",new Date()); //增 添加数据 session.save(user); //提交 tx.commit(); } catch (HibernateException e) { //回滚 tx.rollback(); e.printStackTrace(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }}
删
package com.book.test;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.book.entity.User;public class DelTest { public static void main(String[] args) { Configuration conf = null; SessionFactory sessionFactory =null; Session session = null; Transaction tx = null; try { //解析配置文件 conf = new Configuration().configure(); //创建解析工厂 sessionFactory= conf.buildSessionFactory(); //打开session session = sessionFactory.openSession(); //开启事务 tx = session.beginTransaction(); //先查 后删除 load和get一样 User user =(User) session.load(User.class, 45); session.delete(user); //提交 tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }}
改
package com.book.test;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.book.entity.User;public class UpdateTest { public static void main(String[] args) { Configuration conf = null; SessionFactory sessionFactory =null; Session session = null; Transaction tx = null; try { //解析配置文件 conf = new Configuration().configure(); //创建解析工厂 sessionFactory= conf.buildSessionFactory(); //打开session session = sessionFactory.openSession(); //开启事务 tx = session.beginTransaction(); //先查 后改 User user =(User) session.load(User.class, 45); user.setEmail("504177380@qq.com"); session.update(user); //提交 tx.commit(); } catch (HibernateException e) { tx.rollback(); e.printStackTrace(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }}
查
package com.book.test;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.book.entity.User;public class QueryTest { public static void main(String[] args) { Configuration conf = null; SessionFactory sessionFactory =null; Session session = null; try { //解析配置文件 conf = new Configuration().configure(); //创建解析工厂 sessionFactory= conf.buildSessionFactory(); //打开session session = sessionFactory.openSession(); //查 查不涉及 事务 User user =(User) session.load(User.class, 45); System.out.println(user.getUserpassword()+" "+user.getUsername()); } catch (HibernateException e) { e.printStackTrace(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }}
测试HQL语句
package com.book.test;import java.util.Date;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.book.entity.User;public class TestHql { public static void main(String[] args) { Configuration conf = null; SessionFactory sessionFactory =null; Session session = null; try { //解析配置文件 conf = new Configuration().configure(); //创建解析工厂 sessionFactory= conf.buildSessionFactory(); //打开session session = sessionFactory.openSession(); Query query = session.createQuery("from User where userid =?"); query.setInteger(0, 45); //分页查询 int pageIndex=2; int pageSize=10; query.setFirstResult((pageIndex-1)*pageSize); query.setMaxResults(pageSize); Listlist = query.list(); //单个对象 User user1 = (User)query.uniqueResult(); //多个对象的结果集 List list1 =query.list(); for(int i=0;i