一、hbm.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 7 <class name="com.sdkj.hibernate.domain.TYhOrder1Entity" table="t_yh_order_1" schema="678"> 8 <id name="id" column="id"/> 9 <property name="userId" column="user_id"/> 10 <property name="price" column="price"/> 11 <property name="date" column="date"/> 12 </class> 13 <query name="nameQueryTest"><![CDATA[from TYhOrder1Entity where price > :p1 and price < :p2]]></query> 14 <!-- <![CDATA[]]> 把hql语句放到这个标签里可以防止hql中的特殊字符干扰到xml文件 --> 15 </hibernate-mapping>
二、test()
1 @Test 2 public void testNameQuery(){ 3 4 SessionFactory sessionFactory = getSessionFactory(); 5 Session session = sessionFactory.openSession(); 6 Transaction transaction = session.beginTransaction(); 7 8 Query<TYhOrder1Entity> nameQueryTest = session.getNamedQuery("nameQueryTest") 9 .setParameter("p1", BigDecimal.valueOf(50)).setParameter("p2",BigDecimal.valueOf(100)); 10 11 List<TYhOrder1Entity> list = nameQueryTest.list(); 12 if (!list.isEmpty()){ 13 14 for (TYhOrder1Entity yhOrderEntity : list) { 15 16 System.out.println(yhOrderEntity); 17 } 18 } 19 20 commit(transaction,sessionFactory,session); 21 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16125159.html