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>
@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>