实体类关联数据库字段,操作实体类,HQL语句对数据结构CRUD)
引入jar包
配置文件
hibernate.cfg.xml
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.book.entity.User" table="USER_NEW"> <id name="userid" type="java.lang.Integer"> <column name="USERID" /> <generator class="sequence"> <param name="sequence">SEQ_ID</param> </generator> </id> <property name="username" type="java.lang.String"> <column name="USERNAME"/> </property> <property name="userpassword" type="java.lang.String"> <column name="USERPASSWORD" length="50" not-null="true" /> </property> <property name="role" type="java.lang.Integer"> <column name="ROLE"/> </property> <property name="state" type="java.lang.Integer"> <column name="STATE"/> </property> <property name="email" type="java.lang.String"> <column name="EMAIL"/> </property> <property name="createdate" type="java.util.Date"> <column name="CREATEDATE"/> </property> </class> </hibernate-mapping>
增
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); List<User>list = query.list(); //单个对象 User user1 = (User)query.uniqueResult(); //多个对象的结果集 List<User> list1 =query.list(); for(int i=0;i<list.size();i++){ int userid = list.get(i).getUserid(); System.out.println(userid); } //创建对象 User user = new User("Tom","adsf123",0,0,"sdsad@qq.com",new Date()); //增 添加数据 session.save(user); //提交 } catch (HibernateException e) { e.printStackTrace(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } } }