Hibernate中通过annotaion配置SQLServer的存储过程
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
環(huán)境:hibernate 3.3.1 +spring 2.5.6+ sql server 2005
?
首先,上spring的hibernate配置
?
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"p:dataSource-ref="dataSource"><property name="hibernateProperties"><props><!-- SQLServer DIALECT --><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="annotatedClasses"><list><value>com.bean.Contact</value></list></property></bean>?
這里沒有使用自動(dòng)搜索·通過下面代碼配置實(shí)例化類·
<property name="annotatedClasses"><list><value>com.bean.Contact</value></list> </property>?下一步 Contact類代碼:
import javax.persistence.Entity; import javax.persistence.EntityResult; import javax.persistence.NamedNativeQuery; import javax.persistence.SqlResultSetMapping;@Entity @SqlResultSetMapping(name = "ContactMapping", entities = @EntityResult(entityClass = Contact.class)) @NamedNativeQuery(name = "ContactQuery", query = "{call GetContact(?,?)}", resultSetMapping = "ContactMapping") public class Contact{...........}?其中:
?
???? 1 ContactQuery是可執(zhí)行的查詢名字·以后要在getNamedQuery()方法中用到·
???? 2 ContactMapping是執(zhí)行結(jié)果集和類的對(duì)應(yīng)表,這里結(jié)果集的列名和類的屬性名一致·所以就隱含設(shè)置了·
???? 3 {call GetContact(?,?)} ·GetContact是存儲(chǔ)過程的名字·(?,?)是指有兩個(gè)參數(shù)
???? 4 注意上面的引用包·用hibernate的包會(huì)出錯(cuò)·
?
? 再來看看DAO類的代碼:
?
Query query = getSession().getNamedQuery("ContactQuery"); query.setString(0, ID); query.setString(1, date); List<Contact> ContactList= query.list();?其中:
?
1 getSession().getNamedQuery("ContactQuery")調(diào)用的就是在annotation里定義的名子
2 通過query.setString()設(shè)置存儲(chǔ)過程的參數(shù)
?
PS:在網(wǎng)上查資料時(shí)看到有說這存儲(chǔ)過程是必須要有返回結(jié)果集的·
轉(zhuǎn)載于:https://my.oschina.net/piorcn/blog/310543
總結(jié)
以上是生活随笔為你收集整理的Hibernate中通过annotaion配置SQLServer的存储过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android获取string.xml的
- 下一篇: MySQL编程(0) - Mysql中文