Hibernate SQLQuery简单实用,做链接查询

工单里面可能有0个告警,一个或多个告警,当工单中没有告警的时候也需要将工单显示出来,所以就需要使用工单和告警的做链接查询,下面是具体实例

表:

CREATE TABLE `alarm` (

  `id` bigint(20) NOT NULL DEFAULT '0',

  `title` varchar(20) DEFAULT NULL,

  `level` int(11) DEFAULT NULL,

  `customer_id` bigint(20) DEFAULT NULL,

  `work_sheet_id` bigint(20) DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `work_sheet_id` (`work_sheet_id`),

  CONSTRAINT `alarm_ibfk_1` FOREIGN KEY (`work_sheet_id`) REFERENCES `alarm` (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `worksheet` (

  `id` bigint(20) NOT NULL DEFAULT '0',

  `serialNo` varchar(20) DEFAULT NULL,

  `seatId` bigint(20) DEFAULT NULL,

  `device_id` bigint(20) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into alarm values(1,'title','1','1','1');

insert into alarm values(2,'title','1','1','1');

insert into alarm values(3,'title','1','1','1');

insert into alarm values(4,'title','1','1','1');

insert into alarm values(5,'title','1','1','1');

insert into alarm values(6,'title','1','1','1');







insert into worksheet values(1,'serialno',1,1);

insert into worksheet values(2,'serialno',1,1);

insert into worksheet values(3,'serialno',1,1);

insert into worksheet values(4,'serialno',1,1);

insert into worksheet values(5,'serialno',1,1);

insert into worksheet values(6,'serialno',1,1);

存放SQL文件的接口

package com.h3c.zgc;



public interface SQL {



    public static final String SELECT_WORKSHEET_LEFT_JOIN_ALARM = "select w.serialNo workSheetSerialNo,a.title title,a.level level,a.id alarmId from worksheet w left join alarm a on w.id=a.work_sheet_id where w.id=?";

    

}

dao层:将连接查询的数据转换成map对象放入list中

package com.h3c.zgc;



import java.util.List;

import javax.annotation.Resource;



import org.hibernate.SQLQuery;

import org.hibernate.SessionFactory;

import org.hibernate.transform.Transformers;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import org.springframework.stereotype.Repository;



import com.h3c.zgc.alarm.WorkSheet;



@Repository

public class WorkSheetDao extends HibernateDaoSupport {

    @Resource

    private void set(SessionFactory sessionFactory) {

        this.setSessionFactory(sessionFactory);

    }



    public List getWorkSheets(Long worksheetId) {

        SQLQuery sqlQuery = this

                .getSessionFactory()

                .openSession()

                .createSQLQuery(

                        SQL.SELECT_WORKSHEET_LEFT_JOIN_ALARM);

        sqlQuery.setParameter(0, worksheetId);

        sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

        return sqlQuery.list();

    }



    public WorkSheet getWorkSheet() {

        SQLQuery sqlQuery = this.getSessionFactory().openSession()

                .createSQLQuery("select* from worksheet where id=1")

                .addEntity(WorkSheet.class);

        List<WorkSheet> list = sqlQuery.list();

        return list.get(0);

    }

}

service层:

package com.h3c.zgc;



import java.util.Map;



import javax.annotation.Resource;



import org.springframework.stereotype.Service;



@Service

public class WorkSheetService {

    @Resource

    private WorkSheetDao workSheetDao;

    public Map<String, Object> getWorkSheet(Long worksheetId){

        return  (Map<String, Object>) workSheetDao.getWorkSheets(worksheetId).get(0);

    }

}

controller层

package com.h3c.zgc;



import java.util.Map;



import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;



import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;



@Controller

public class WorkSheetController {

    @Resource

    private WorkSheetService workSheetService;

    

    @RequestMapping("getArr")

    public String toGetArr(Long workSheetId,HttpServletRequest request){

        Map<String, Object> o = this.workSheetService.getWorkSheet(workSheetId);

        System.out.println(o.get("alarmId")==null);

        if(o.get("alarmId")==null){

            o.put("alarmId", 0);

        }

        request.setAttribute("obj",o);

        return "test/getWorkSheet";

        

    }

}

 

在页面中获取数据

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

${obj['workSheetSerialNo'] } ,${obj['alarmId'] },${obj['title'] },${obj['level'] }

<c:if test="${obj['alarmId']==0 }">告警不存在</c:if>

</body>

</html>

 

你可能感兴趣的:(Hibernate)