二、具体事例如下:
2.0 编写如下sql语句
1 create table teacher(tid number not null primary key,tname varchar2(20)); 2 create sequence seq_teacher; 3 insert into teacher values(seq_teacher.nextval,'holly'); 4 insert into teacher values(seq_teacher.nextval,'石头'); 5 commit; 6 insert into teacher values(seq_teacher.nextval,'holly1'); 7 insert into teacher values(seq_teacher.nextval,'holly2'); 8 commit;
2.1 创建如下的javaweb项目并添加jar文件
2.2 在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">scott</property> 8 <property name="connection.password">tiger</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 <mapping resource="com/entity/Teacher.hbm.xml"/> 13 </session-factory> 14 </hibernate-configuration>
2.3 在项目的com.entity包下创建Teacher.java类
1 package com.entity; 2 3 4 public class Teacher { 5 private int tid; 6 private String tname; 7 8 public Teacher() { 9 } 10 11 public Teacher(String tname) { 12 this.tname = tname; 13 } 14 15 public Teacher(int tid, String tname) { 16 this.tid = tid; 17 this.tname = tname; 18 } 19 20 public int getTid() { 21 return tid; 22 } 23 24 public void setTid(int tid) { 25 this.tid = tid; 26 } 27 28 public String getTname() { 29 return tname; 30 } 31 32 public void setTname(String tname) { 33 this.tname = tname; 34 } 35 36 @Override 37 public String toString() { 38 return "Teacher [tid=" + tid + ", tname=" + tname + "]"; 39 } 40 41 42 43 }
2.4 在项目的com.entity包下创建Teacher.hbm.xm文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Teacher" table="TEACHER"> 5 <id name="tid" type="java.lang.Integer" column="TID"> 6 <generator class="sequence"> 7 <param name="sequence">seq_teacher</param> 8 </generator> 9 </id> 10 <property name="tname" type="java.lang.String" column="TNAME"/> 11 </class> 12 </hibernate-mapping>
2.5 在项目的com.util包下创建HibernateUtil.java类
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 private static ThreadLocal<Session> thread=new ThreadLocal<Session>(); 10 private static Configuration config=null; 11 private static SessionFactory factory=null; 12 13 static{ 14 try { 15 config=new Configuration().configure("/hibernate.cfg.xml"); 16 factory=config.buildSessionFactory(); 17 } catch (HibernateException e) { 18 System.out.println("读取配置文件或创建工厂失败"); 19 e.printStackTrace(); 20 } 21 } 22 23 public static Session getSession(){ 24 Session session =thread.get(); 25 if(session==null){ 26 session =factory.openSession(); 27 thread.set(session); 28 } 29 return session; 30 } 31 public static void closeSession(){ 32 Session session =thread.get(); 33 thread.set(null); 34 session.close(); 35 36 } 37 38 }
2.6 在项目的com.dao包下创建TeacherDao.java类
1 package com.dao; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 9 import com.entity.Teacher; 10 import com.util.HibernateUtil; 11 12 public class TeacherDao { 13 /** 14 * 1.iterate() 延时加载查询一堆 15 * 得到1+N条sql 16 * 1是查询所有的sql 17 * n根据id查询的n条sql 18 * 延迟加载(用时在查询),关闭session的时机是读取完数据之后再关闭 19 */ 20 public static Iterator<Teacher> hqlItertor() { 21 Session session=HibernateUtil.getSession(); 22 //定义hql语句 23 String hql="from Teacher"; 24 25 //创建Query查询接口对象 26 Query query=session.createQuery(hql); 27 28 //使用list方法查询所有返回list集合 29 Iterator<Teacher> it=query.iterate(); 30 31 32 return it; 33 } 34 /** 35 * 2. list()立即加载 36 * 一条sql 37 * 立即加载(一次性查询出所有内容(关联)放入内存) 38 * 39 */ 40 public static List<Teacher> hqlList() { 41 Session session=HibernateUtil.getSession(); 42 //定义hql语句 43 String hql="from Teacher"; 44 45 //创建Query查询接口对象 46 Query query=session.createQuery(hql); 47 48 //使用list方法查询所有返回list集合 49 List<Teacher> list=query.list(); 50 51 52 HibernateUtil.closeSession(); 53 return list; 54 55 } 56 /** 57 * 3.根据条件查询返回集合 58 * @param tname 59 * @return 60 */ 61 public static List<Teacher> hqlIf(String tname) { 62 Session session=HibernateUtil.getSession(); 63 //定义hql语句 64 String hql="from Teacher where tname='"+tname+"'"; 65 66 //创建Query查询接口对象 67 Query query=session.createQuery(hql); 68 69 //使用list方法查询所有返回list集合 70 List<Teacher> list=query.list(); 71 72 73 HibernateUtil.closeSession(); 74 return list; 75 } 76 /** 77 * 4. 占位符的使用(模糊查询) 78 * @param string 79 * @return 80 */ 81 public static List<Teacher> hqlLike(String name) { 82 Session session=HibernateUtil.getSession(); 83 //定义hql语句 84 String hql="from Teacher where tname like ?"; 85 86 //创建Query查询接口对象 87 Query query=session.createQuery(hql); 88 String str="%"+name+"%"; 89 query.setString(0, str); 90 91 //使用list方法查询所有返回list集合 92 List<Teacher> list=query.list(); 93 94 95 HibernateUtil.closeSession(); 96 return list; 97 } 98 /** 99 * 5. 按照参数名称使用(模糊查询) 100 * @param string 101 * @return 102 */ 103 public static List<Teacher> hqlLikeName(String name) { 104 Session session=HibernateUtil.getSession(); 105 //定义hql语句 106 String hql="from Teacher where tname like :pname"; 107 108 //创建Query查询接口对象 109 Query query=session.createQuery(hql); 110 String str="%"+name+"%"; 111 query.setString("pname", str); 112 113 //使用list方法查询所有返回list集合 114 List<Teacher> list=query.list(); 115 116 117 HibernateUtil.closeSession(); 118 return list; 119 } 120 /** 121 * 6. 条件查询绑定任意类型的参数:命名参数 122 * @param string 123 * @return 124 */ 125 public static List<Teacher> hqlSetParam(String name) { 126 Session session=HibernateUtil.getSession(); 127 //定义hql语句 128 String hql="from Teacher where tname like :pname"; 129 130 //创建Query查询接口对象 131 Query query=session.createQuery(hql); 132 String str="%"+name+"%"; 133 134 query.setParameter("pname", str); 135 136 //使用list方法查询所有返回list集合 137 List<Teacher> list=query.list(); 138 139 140 HibernateUtil.closeSession(); 141 return list; 142 } 143 /** 144 * 7. 条件查询绑定任意类型的参数:占位符 145 * @param string 146 * @return 147 */ 148 public static List<Teacher> hqlSetParamXx(String name) { 149 Session session=HibernateUtil.getSession(); 150 //定义hql语句 151 String hql="from Teacher where tname like ?"; 152 153 //创建Query查询接口对象 154 Query query=session.createQuery(hql); 155 String str="%"+name+"%"; 156 157 query.setParameter(0, str); 158 159 //使用list方法查询所有返回list集合 160 List<Teacher> list=query.list(); 161 162 163 HibernateUtil.closeSession(); 164 return list; 165 } 166 167 168 /** 169 * 8.条件动态:通过对象属性去给命名参数赋值 170 * @param name 171 * @return 172 */ 173 public static List<Teacher> hqlSetProte(Teacher teacher) { 174 175 Session session=HibernateUtil.getSession(); 176 //定义hql语句 177 StringBuilder hql=new StringBuilder("from Teacher as t where 1=1"); 178 179 if(teacher.getTname()!=null){ 180 hql.append(" and t.tname=:tname"); 181 } 182 183 //创建Query查询接口对象 184 Query query=session.createQuery(hql.toString()); 185 query.setProperties(teacher); 186 187 //使用list方法查询所有返回list集合 188 List<Teacher> list=query.list(); 189 190 HibernateUtil.closeSession(); 191 return list; 192 } 193 /** 194 * 9.查询单条数据 195 * @return 196 */ 197 public static Teacher hhqlGetTeacher(String name) { 198 Session session=HibernateUtil.getSession(); 199 //定义hql语句 200 String hql="from Teacher where tname=:pname"; 201 202 203 //创建Query查询接口对象 204 Query query=session.createQuery(hql.toString()); 205 query.setParameter("pname", name); 206 207 //使用 208 Teacher teacher=(Teacher) query.uniqueResult(); 209 210 HibernateUtil.closeSession(); 211 return teacher; 212 } 213 /** 214 * 10. 分页查询 215 * @param i 216 * @param j 217 * @return 218 */ 219 public static List<Teacher> hqlPage(int pageSize, int pageNo) { 220 Session session=HibernateUtil.getSession(); 221 222 //1.获取中条数 223 //定义中条数的hql语句 224 String hqlcount="select count(*) from Teacher"; 225 Query countQuery=session.createQuery(hqlcount); 226 227 //uniqueResult()得到总条数 228 Integer totalCount=Integer.parseInt(countQuery.uniqueResult().toString()); 229 System.out.println("总条数:"+totalCount); 230 231 //2.计算总页数 232 int totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1; 233 System.out.println("总页数:"+totalPage); 234 235 //3.查询的最大记录数(每页查询几条) 236 String hql="from Teacher"; 237 Query query=session.createQuery(hql); 238 239 query.setMaxResults(pageSize); 240 241 //4.确定查询查询的起点 242 query.setFirstResult((pageNo-1)*pageSize); 243 244 //5.查询分页数据 245 List<Teacher> list=query.list(); 246 HibernateUtil.closeSession(); 247 return list; 248 } 249 }
2.7 在项目的com.test包下创建Test.java类
1 package com.test; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import com.dao.TeacherDao; 7 import com.entity.Teacher; 8 import com.util.HibernateUtil; 9 10 public class Test { 11 12 public static void main(String[] args) { 13 // System.out.println("--------------1.使用list查询所有-----------"); 14 // List<Teacher> list=TeacherDao.hqlList(); 15 // for (Teacher teacher : list) { 16 // System.out.println(teacher); 17 // } 18 // System.out.println("--------------2.使用Itertor查询所有-----------"); 19 // Iterator<Teacher> it=TeacherDao.hqlItertor(); 20 // Teacher te=null; 21 // while (it.hasNext()) { 22 // te=it.next(); 23 // System.out.println(te); 24 // } 25 // HibernateUtil.closeSession(); 26 // 27 // System.out.println("----------3.条件拼接查询-----------"); 28 // List<Teacher> iflist=TeacherDao.hqlIf("holly"); 29 // for (Teacher teacher : iflist) { 30 // System.out.println(teacher); 31 // } 32 // 33 // System.out.println("----------4.条件查询模糊查询:按照参数位置-----------"); 34 // List<Teacher> likelist=TeacherDao.hqlLike("holly"); 35 // for (Teacher teacher : likelist) { 36 // System.out.println(teacher); 37 // } 38 // System.out.println("----------5.条件查询模糊查询:按照名称-----------"); 39 // List<Teacher> likeNamelist=TeacherDao.hqlLikeName("holly"); 40 // for (Teacher teacher : likeNamelist) { 41 // System.out.println(teacher); 42 // } 43 44 // System.out.println("----------6.条件查询绑定任意类型的参数-----------"); 45 // List<Teacher> SetParamlist=TeacherDao.hqlSetParam("holly"); 46 // for (Teacher teacher : SetParamlist) { 47 // System.out.println(teacher); 48 // } 49 // System.out.println("----------7.条件查询绑定任意类型的参数:占位符-----------"); 50 // List<Teacher> SetParamXx=TeacherDao.hqlSetParamXx("holly"); 51 // for (Teacher teacher : SetParamXx) { 52 // System.out.println(teacher); 53 // } 54 System.out.println("----------8.条件动态:命名参数-----------"); 55 // Teacher te=new Teacher("holly"); 56 // List<Teacher> SetProte=TeacherDao.hqlSetProte(te); 57 // for (Teacher teacher : SetProte) { 58 // System.out.println(teacher); 59 // } 60 61 System.out.println("------------9.查询单条数据-----------"); 62 // Teacher teacher=TeacherDao.hhqlGetTeacher("holly"); 63 // System.out.println(teacher); 64 65 System.out.println("---------10.分页查询------------"); 66 List<Teacher> list=TeacherDao.hqlPage(2,2); 67 System.out.println("页面大小:"+2+",当前页:"+2); 68 for (Teacher teacher : list) { 69 System.out.println(teacher); 70 } 71 72 73 74 } 75 76 }
2.8 效果自己试试就知道