Hibernate中的命名查询
介绍下面几种方式:
方法一:在配置文件中<class/>标记的下面,声明查询语句
[xml]
<hibernate-map<A href="http://www.linuxso.com/command/ping.html" target=_blank><U>ping</U></A>>
<class name="com.test.bean.Student" table="student" <A href="http://www.linuxso.com/command/cat.html" target=_blank><U>cat</U></A>alog="users">
<<A href="http://www.linuxso.com/command/id.html" target=_blank><U>id</U></A> name="id" type="integer">
<<A href="http://www.linuxso.com/command/col.html" target=_blank><U>col</U></A>umn name="id" />
<generator class="identity" />
</id>
<property name="name" type="s<A href="http://www.linuxso.com/command/tr.html" target=_blank><U>tr</U></A>ing">
<column name="name" length="11" />
</property>
<property name="age" type="integer">
<column name="age" />
</property>
<property name="s<A href="http://www.linuxso.com/command/ex.html" target=_blank><U>ex</U></A>" type="string">
<column name="易做图" length="2" />
</property>
</class>
<!-- 定义查询语句 -->
<query name="<A href="http://www.linuxso.com/command/find.html" target=_blank><U>find</U></A>StudentByName">
<![CDATA[from Student where name = :name]]>
</query>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="11" />
</property>
<property name="age" type="integer">
<column name="age" />
</property>
<property name="易做图" type="string">
<column name="易做图" length="2" />
</property>
</class>
<!-- 定义查询语句 -->
<query name="findStudentByName">
<![CDATA[from Student where name = :name]]>
</query>
</hibernate-mapping>备注:<![CDATA[ ]]>是什么意思?
标明是纯文本的,没有这个的话 < > & 字符是不能直接存入XML的,需要转义,而用这个标记则不需要转义而将这些符号存入XML文档。可以避免未预料的特殊符号导致XML解析出错。
[java]
public List<Student> query(){
Session session = HibernateSessionFactory.getSession();
Query q = session.getNamedQuery("findStudentByName");
q.<A href="http://www.linuxso.com/command/set.html" target=_blank><U>set</U></A>String("name", "zhangsan");
List<Student> list = q.list();
return list;
}
public List<Student> query(){
Session session = HibernateSessionFactory.getSession();
Query q = session.getNamedQuery("findStudentByName");
q.setString("name", "zhangsan");
List<Student> list = q.list();
return list;
}
方法二:也可以在配置文件中<class/>标记的里面,声明查询语句, 但是java代码调用时需要指定(包+类+配置名)
[xml]
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="11" />
</property>
<property name="age" type="integer">
<column name="age" />
</property>
<property name="易做图" type="string">
<column name="易做图" length="2" />
</property>
<!-- 定义查询语句 -->
<query name="findStudentByName">
<![CDATA[from Student where name = :name]]>
补充:软件开发 , Java ,