时间:2021-07-01 10:21:17 帮助过:34人阅读

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<ElecSystemDDL>集合放入request对象中,属性名为list。当dictionary.jsp页面获得request.list之后需要将ElecSystemDDL对象的keyword属性遍历出来,放入下拉选项框中,有两种方案:
<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<Object>,例如
String hql = "SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql);
(2)如果投影查询是多个字段,此时返回List<Object[]>,例如
String hql = "SELECT DISTINCT o.keyword,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
(3)如果投影查询是多个字段,此时返回List<Object[]>,例如
String hql = "SELECT o,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
数组的第一个值,是一个ElecSystemDDL的对象,数组的第二个值表示字段ddlName的值。
(4)如果投影查询是一个对象,此时返回List<ElecSystemDDL>,例如
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)方案一:使用<s:iterator>遍历<option>
<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>
<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>
以上就是数据字典设计之投影查询的方法的详细内容,更多请关注Gxl网其它相关文章!