Projection 쿼리는 그림과 같이 드롭다운 상자를 클릭하여 데이터베이스의 데이터 사전 테이블에 있는 기존 데이터 유형 이름을 표시하고 중복 없이 드롭다운 메뉴로 이동하는 것을 의미합니다
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; }
<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 문에 대한 프로젝션 쿼리:
String hql = "SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql);과 같이 이때 List