Hibernate 利用配置文件配置HQL/SQL

bigcactus 2012-05-21

Query.hbm.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<queryname="QueryUser">

<![CDATA[

FROMUseru

WHEREu.userName=:userName

]]>

<sql-queryname="SqlQueryUser">

<![CDATA[

SELECT

{u.*}

FROM

useru

WHERE

u.user_name=:userName

]]>

<returnalias="u"class="com.test.entity.User"/>

</sql-query>

</hibernate-mapping>

hibernate.cfg.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

...

<mappingresource="User.hbm.xml"/>

<mappingresource="query/Query.hbm.xml"/>

...

</session-factory>

</hibernate-configuration>

JavaCode:

Stringusername="...";

Sessionsession=...;

Queryquery=session.getNamedQuery("QueryUser");

//Queryquery=session.getNamedQuery("SqlQueryUser");

query.setString("name",username);

ListuserList=query.list();

相关推荐

LetonLIU / 0评论 2020-05-29
东方咖啡屋 / 0评论 2020-01-06