毕业设计阶段遇到的一些技术问题

现在正值毕业设计阶段,在做项目的过程中,因为框架不熟练,遇到了许多问题,现在借以记录。

一、Mybatis的映射模型

1.项目中,我的Bean采用的是多层嵌套方式

 

这样导致在动态取值时,需要运用.的方式进行取值(#{managerBean.m_id})

 2.Mybatis使用一对一映射方式有ResultType以及ResultMap。

在我的Bean结构模式下,因为多层嵌套,使之映射到属性的时候存在问题,所以我开始想办法

(1)应用ResultMap,将映射关系清楚地表明

 

 1 <resultMap id="getHisInfoListMap" type="com.gdmu.hnrs.model.ComplexBean" >
 2         <association property="patientBean" javaType="com.gdmu.hnrs.model.PatientBean">
 3            <id property="p_id" column="p_id"/>
 4            <result property="p_name" column="p_name"/>
 5         </association>
 6         <association property="foodBean" javaType="com.gdmu.hnrs.model.FoodBean">
 7            <id property="f_id" column="f_id"/>
 8            <result property="f_name" column="f_name"/>
 9         </association>
10         <association property="diseaseBean" javaType="com.gdmu.hnrs.model.DiseaseBean">
11            <id property="d_id" column="d_id"/>
12            <result property="d_name" column="d_name"/>
13         </association>
14         <association property="historyListBean" javaType="com.gdmu.hnrs.model.HistoryListBean">
15            <result property="l_time" column="l_time"/>
16         </association>
17 </resultMap>

 

 

 

但这样出现一个错误,我的SQL语句原本应该拿到6条数据,但这样写返回的却只有一条

通过上网查询,得知在ResultMap中应指定id,与表对应,这样Mybatis才知道数据的唯一性,而我这样就会被最后一条数据覆盖。(但是我的结构不允许我用这种方法操作,所以pass)

(2)应用ResultType,但返回值类型用java.util.Map,将数据全部存储到了Map中(List<Map<String,Object>>),这样虽然说确实取到了数据,但由于我的业务需要,在Service层不得不写了一段很low的代码将其重新组装成Bean

 

 1 public List<ComplexBean> getHisInfoList(ComplexBean complexBean) {
 2         List<ComplexBean> models = new ArrayList<ComplexBean>();
 3         
 4         List<Map<String, Object>> tempList = managerMapper.getHisInfoList(complexBean);
 5         for(Object ob:tempList){
 6             HashMap hp = (HashMap)ob;
 7             //拿到键值对迭代器(hp.entrySet()拿到键值对集合)
 8             Iterator iter = hp.entrySet().iterator();
 9             //创建临时对象
10             ComplexBean tempComplexBean = new ComplexBean();
11             FoodBean tempFoodBean = new FoodBean();
12             PatientBean tempPatientBean = new PatientBean();
13             DiseaseBean tempDiseaseBean = new DiseaseBean();
14             HistoryListBean tempHistoryListBean = new HistoryListBean();
15             while (iter.hasNext()) {
16                 //拿到键值、val值
17                 Entry entry = (Entry) iter.next();
18                 Object key = entry.getKey();
19                 Object val = entry.getValue();
20                 
21                 if("L_TIME".equals(key)){
22                     tempHistoryListBean.setL_time((Date)val);
23                 }
24                 if("F_ID".equals(key)){
25                     tempFoodBean.setF_id((String)val);
26                 }
27                 if("F_NAME".equals(key)){
28                     tempFoodBean.setF_name((String)val);
29                 }
30                 if("D_ID".equals(key)){
31                     tempDiseaseBean.setD_id((String)val);;
32                 }
33                 if("P_NAME".equals(key)){
34                     tempPatientBean.setP_name((String)val);
35                 }
36                 if("D_NAME".equals(key)){
37                     tempDiseaseBean.setD_name((String)val);
38                 }
39                 if("P_ID".equals(key)){
40                     tempPatientBean.setP_id((String)val);;
41                 }
42                 tempComplexBean.setPatientBean(tempPatientBean);
43                 tempComplexBean.setDiseaseBean(tempDiseaseBean);
44                 tempComplexBean.setHistoryListBean(tempHistoryListBean);
45                 tempComplexBean.setFoodBean(tempFoodBean);
46             }
47             models.add(tempComplexBean);
48         }
49         return models;
50 }

 

 

 

因为现在正值工作和毕业设计的双重压力,没有多余的时间改造,等闲下来一定要重新改造!!!

posted @ 2017-04-15 19:13  龙之天族  阅读(942)  评论(0编辑  收藏  举报