hibernate4對sql語句的一些基本操作

1.首先我們要明確,hibernate4與之前的版本的開發沒有本質區別,但是對於開發者來說,hibernate4利用的是最底層的開發方法,沒有再繼承hibernateDaoSupport。

2.hibernate4可以兼容spring,springMVC架構,而且最重要的是不需要再把每一個實體類配置一個hbl.xml配置文件。

3.沒有配置文件,那多表查詢如何進行呢,需不需要進行多表關聯查詢?我實踐的是完全沒問題,不需要任何配置,執行原生態的sql就能實現你自己想要的功能,下面看一下我對多表的一些處理。僅供參考,如有錯誤,請留言想告。


爲了方便簡潔,只來dao的實現類的操作:


@Repository("friendCircleDao")
public class FriendCircleDaoImpl implements FriendCircleDao {
	@Autowired
	@Qualifier("sessionFactory")
<span style="white-space:pre">	</span>private SessionFactory sessionFactory;
<span style="white-space:pre">	</span>首先是注入<span style="font-family: 'Microsoft YaHei', SimHei, arial;">sessionFactory,然後我們來操作多表條件查詢</span>
<span style="font-family:Microsoft YaHei, SimHei, arial;">public List<FilterModel> getAllfriend(int pn, int pageSize,String yonghuming, String shoujihao, String begintime
<span style="white-space:pre">			</span>String lasttime) {</span>
<span style="font-family:Microsoft YaHei, SimHei, arial;">//sql語句,只要能在數據庫的視圖工具裏面執行就沒問題
String hql="select user.yonghuming,user.shoujihao,userdescripe.time,userdescripe.neirong,userdescripe.coords "
<span style="white-space:pre">		</span>+"from userdescripe left join user on userdescripe.userId=user.id WHERE 1=1";
<span style="white-space:pre">		</span>if(!"".equals(yonghuming)&&yonghuming!=null){
<span style="white-space:pre">			</span>hql+=" and user.yonghuming like '%"+yonghuming+"%'";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(!"".equals(shoujihao)&&shoujihao!=null){
<span style="white-space:pre">			</span>hql+=" and user.shoujihao like '%"+shoujihao+"%'";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(!"".equals(begintime)&&begintime!=null){
<span style="white-space:pre">			</span>hql+=" and userdescripe.time >= '"+begintime+"'";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(!"".equals(lasttime)&&lasttime!=null){
<span style="white-space:pre">			</span>hql+=" and userdescripe.time <= '"+lasttime+"'";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>hql+=" ORDER BY time desc";
<span style="white-space:pre">		</span>Query query = sessionFactory.getCurrentSession().createSQLQuery(hql);
<span style="white-space:pre">		</span>if (pn > -1 && pageSize > -1) {
<span style="white-space:pre">			</span>query.setMaxResults(pageSize);
<span style="white-space:pre">			</span>int start = PageUtil.getPageStart(pn, pageSize);
<span style="white-space:pre">			</span>if (start != 0) {
<span style="white-space:pre">				</span>query.setFirstResult(start);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if (pn < 0) {
<span style="white-space:pre">			</span>query.setFirstResult(0);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>List list =  query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
<span style="white-space:pre">		</span>return list;
<span style="white-space:pre">	</span>}</span>
<span style="font-family:Microsoft YaHei, SimHei, arial;">接下來進行多表刪除,</span>
<span style="font-family:Microsoft YaHei, SimHei, arial;">@Override
<span style="white-space:pre">	</span>public void deleteFriend(String id) {
<span style="white-space:pre">		</span>String hql="DELETE userdescripe,reply  FROM userdescripe,reply WHERE userdescripe.id=reply.friendUuid   AND userdescripe.userId='"+id+"'";
<span style="white-space:pre">		</span>Query query=sessionFactory.getCurrentSession().createSQLQuery(hql);
<span style="white-space:pre">		</span>query.executeUpdate();
<span style="white-space:pre">	</span>}
</span><span style="font-family:Microsoft YaHei, SimHei, arial;">執行後就是你想要的結果,hibernate4完全可以自己優化sql語句,優化你的代碼,大家有好的別忘了分享。
}
</span>


發佈了42 篇原創文章 · 獲贊 17 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章