投影查詢指的是:按一下下拉框,顯示資料庫中資料字典表已有的資料類型名稱,無重複的遍歷到下拉式選單中,如圖所示
public class ElecSystemDDLAction extends BaseAction<ElecSystemDDL>{ ElecSystemDDL elecSystemDDL=this.getModel(); //注入数据字典service@Resource(name=IElecSystemDDLService.SERVICE_NAME) IElecSystemDDLService elecSystemDDLService; /** * @Name: home * @Description: 跳转到数据字典页面 * @Parameters: 无 * @Return: String:跳转到system/dictionaryIndex.jsp*/public String home(){// 1:查询数据库中已有的数据类型,返回List<ElecSystemDDL>集合 List<ElecSystemDDL> list=elecSystemDDLService.findSystemDDLListByDistinct(); // 2:将ElecSystemDDL对象放入request中 request.setAttribute("list", list);return "home"; } }
public interface IElecSystemDDLService {public static final String SERVICE_NAME="cn.elec.service.impl.ElecSystemDDLServiceImpl"; List<ElecSystemDDL> findSystemDDLListByDistinct(); }
public class ElecSystemDDLServiceImpl implements IElecSystemDDLService{//数据字典Dao@Resource(name=IElecSystemDDLDao.SERVICE_NAME)private IElecSystemDDLDao elecSystemDDLDao; /** * @Name: findSystemDDLListByDistinct * @Description: 查询数据库中已有的数据类型,去掉重复值 * @Parameters: 无 * @Return: List:存储数据类型集合*/@Override @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public List<ElecSystemDDL> findSystemDDLListByDistinct() { List<ElecSystemDDL> list=elecSystemDDLDao.findSystemDDLListByDistinct();return list; } }
public interface IElecSystemDDLDao extends ICommonDao<ElecSystemDDL> {public static final String SERVICE_NAME="cn.elec.dao.imp.ElecSystemDDLImpl"; List<ElecSystemDDL> findSystemDDLListByDistinct(); }
@Repository(IElecSystemDDLDao.SERVICE_NAME)public class ElecSystemDDLImpl extends ICommonDaoImpl<ElecSystemDDL> implements IElecSystemDDLDao{/** * @Name: findSystemDDLListByDistinct * @Description: 查询数据库中已有的数据类型,去掉重复值 * @Parameters: 无 * @Return: List:存储数据类型集合*/@Overridepublic List<ElecSystemDDL> findSystemDDLListByDistinct() {//返回List集合List<ElecSystemDDL> systemList = new ArrayList<ElecSystemDDL>();/**方法一:投影查询一个字段返回List<Object>中 String hql="SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql); //组织页面返回结果 if(list!=null&&list.size()>0){ for(Object obj:list){ ElecSystemDDL elecSystemDDL = new ElecSystemDDL(); //设置数据类型,并添加到systemList elecSystemDDL.setKeyword(obj.toString()); systemList.add(elecSystemDDL); } }*///方法二:使用hql语句直接将投影查询的字段放置到对象中String hql="SELECT DISTINCT new cn.elec.domain.ElecSystemDDL(o.keyword) FROM ElecSystemDDL o"; systemList=this.getHibernateTemplate().find(hql);return systemList; } }
其中第二種查詢方案需要在ElecSystemDDL中建立一個帶參得的建構方法:
public ElecSystemDDL() {//无参构造方法} public ElecSystemDDL(String keyword) {//带参构造方法this.keyword=keyword; }
因為在Actionl類別中將List
<tr> <td class="ta_01" align="right" width="35%" >类型列表:</td> <td class="ta_01" align="left" width="30%" > <!-- 方案一 <select name="keyword" class="bg" style="width:180px" onchange="changetype()"> <option value="jerrynew"></option> <s:iterator value="#request.list" var="sys"> <option value="<s:property value="#sys.keyword"/>"> <s:property value="#sys.keyword"/> </option> </s:iterator> </select> --> <!-- 方案二 --> <s:select list="#request.list" name="keyword" id="keyword" listKey="keyword" listValue="keyword" headerKey="jerrynew" headerValue="" cssClass="bg" cssStyle="width:180px" onchange="changetype()"> </s:select> </td> </tr>
#測試,在資料字典首頁功能頁面中,下拉式選單能正確顯示類型名稱。
1.hql和sql語句的投影查詢:
(1)如果投影查詢是一個字段,此時傳回List
String hql = "SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql);
(2)如果投影查詢是多個字段,此時傳回List
String hql = "SELECT DISTINCT o.keyword,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
(3)如果投影查詢是多個字段,此時傳回List
String hql = "SELECT o,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
數組的第一個值,是一個ElecSystemDDL的對象,數組的第二個值表示字段ddlName的值。
(4)如果投影查詢是一個對象,此時回傳List
String hql = "SELECT o FROM ElecSystemDDL o"; List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
# (5)如果是hql語句,使用hql語句直接將投影查詢的欄位放置到物件中,例如
String hql = "SELECT DISTINCT new cn.itcast.elec.domain.ElecSystemDDL(o.keyword) FROM ElecSystemDDL o"; List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
(1)方案一:使用
<select name="keyword" class="bg" style="width:180px" onchange="changetype()"> <option value="jerrynew"></option> <s:iterator value="#request.list" var="system"> <option value="<s:property value="#system.keyword"/>"> <s:property value="#system.keyword"/> </option> </s:iterator> </select>
#(2)方案二:使用
<s:select list="#request.list" name="keyword" id="keyword"listKey="keyword" listValue="keyword"headerKey="jerrynew" headerValue=""cssClass="bg" cssStyle="width:180px" onchange="changetype()"> </s:select>
以上是資料字典設計之投影查詢的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!