Home > Web Front-end > JS Tutorial > body text

Projection query method of data dictionary design

零下一度
Release: 2017-07-02 09:29:16
Original
1481 people have browsed it

 Projection query refers to: click the drop-down box to display the existing data type names in the data dictionary table in the database, and traverse to the drop-down menu without duplication, as shown in the figure

1. Add in the home of ElecSystemDDLAction:

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";
    }
}
Copy after login

2. Declare the projection query method in IElecSystemDDLService:

public interface IElecSystemDDLService {public static final String SERVICE_NAME="cn.elec.service.impl.ElecSystemDDLServiceImpl";

    List<ElecSystemDDL> findSystemDDLListByDistinct();
    
}
Copy after login

3. Rewrite in ElecSystemDDLServiceImpl:

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;
    }
}
Copy after login

4. Declare the projection query method in IElecSystemDDLDao:

public interface IElecSystemDDLDao extends ICommonDao<ElecSystemDDL> {public static final String SERVICE_NAME="cn.elec.dao.imp.ElecSystemDDLImpl";

    List<ElecSystemDDL> findSystemDDLListByDistinct();
}
Copy after login

5. Rewrite this method in ElecSystemDDLImpl:

@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;
    }
}
Copy after login

The second query plan requires creating a constructor with parameters in ElecSystemDDL:

public ElecSystemDDL() {//无参构造方法}    
public ElecSystemDDL(String keyword) {//带参构造方法this.keyword=keyword;
}  
Copy after login

6. Traverse in dictionaryIndex.jsp:

Because the List collection is put into the request object in the Actionl class, and the attribute name is list. When the dictionary.jsp page obtains the request.list, it needs to traverse the keyword attribute of the ElecSystemDDL object and put it into the drop-down option box. There are two options:

<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>
Copy after login

Test, On the data dictionary homepage function page, the drop-down menu can correctly display the type name.

1. Projection query of hql and sql statements:

(1) If the projection query is a field, List is returned at this time, for example

String hql = "SELECT DISTINCT o.keyword FROM ElecSystemDDL o";
List<Object> list = this.getHibernateTemplate().find(hql);
Copy after login

(2) If the projection query is for multiple fields, List will be returned at this time, for example

String hql = "SELECT DISTINCT o.keyword,o.ddlName FROM ElecSystemDDL o";
List<Object[]> list = this.getHibernateTemplate().find(hql);
Copy after login

(3) If the projection query is for multiple fields, List will be returned at this time, for example

String hql = "SELECT o,o.ddlName FROM ElecSystemDDL o";
List<Object[]> list = this.getHibernateTemplate().find(hql);
Copy after login

The first value of the array is an ElecSystemDDL object, and the second value of the array represents the value of the field ddlName.

(4) If the projection query is an object, List is returned at this time, for example

String hql = "SELECT o FROM ElecSystemDDL o";
List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
Copy after login

(5) If it is an hql statement, use the hql statement to directly place the fields of the projection query into the object, such as

String hql = "SELECT DISTINCT new cn.itcast.elec.domain.ElecSystemDDL(o.keyword) FROM ElecSystemDDL o";
List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
Copy after login

2. The page uses select to traverse List

(1) Solution 1: Use to traverse

<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>
Copy after login

(2) Solution Two: Use

<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>
Copy after login

The above is the detailed content of Projection query method of data dictionary design. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template