spring+mybatis轻量级企业框架的学习之路之错误笔记day04

来源:互联网 发布:java互联网面试题 编辑:程序博客网 时间:2024/06/05 06:28

在之前的一天由于没有合理安排自己就的时间玩了一天,想想一点都不好玩还是默默的开始搬砖了。


mybatis框架已经很多人在用了,我也不细细讲诉基本搭建方法。讲下在过程中遇到一些细节问题


1:在mapper。xml配置文件中,查询多表关联查询,之前的思路是写3个接口方法来查找,但是太

笨重这个思路,于是看到了resultmap,其中有个属性是collection 干说没意思,上代码:

<resultMap type="com.abc.entity.Role" id="roleMap">
<id column="role_id" property="role_id"></id>
<collection property="modules" javaType="java.util.ArrayList"
ofType="com.tarena.entity.Module" column="role_id"
select="selectModules">
</collection>
</resultMap>

<select id="selectModules" 
parameterType="int" 
resultType="com.tarena.entity.Module">
select * from module_info where module_id in (
select module_id from role_module
where role_id=#{role_id}
)
</select>

<select id="findByRole" 
parameterType="com.abc.entity.page.Page"
resultMap="roleMap">
<![CDATA[select * from (select ro.*,ROWNUM r from role_info ro order by role_id) r
where r.r<#{end} and r.r>#{begin}]]>

对应的javaBean里有这么几个属性:

public class Role{

private Integer role_id;
private String name;
private List<Module> modules;
// private List<Integer> moduleIds;

}

更具findByRole这个sql大家可以看出这只是个普通的查询role的查询语句,但是后台得到的Role里modules

这个属性居然不为空,居然关联查询除了对应的modules,当时还以为是和Hibernate的机制类似的机制呢,

其实不然,关键在resultmap的collection,下面一一解释colletion的属性作用:

property:所要填充的javaBean字段,通俗讲就是Role里的modules

javaType:collection所需要的java接受类型 ,一般就是java.util.ArrayList

ofType:集合所对应的java类型,List<module> 路径要写全eg:com.tarena.entity.Module

colum:对应查询的字段,次字段为关联查询的字段,一般是id

select:关联查询的sql语句id,更具次ID找出对应的sql并执行将collection的module查出

并以集合的方式赋值给Role

优点:SQL语句写的少,后台只要查询Role就可以获得Module,无需多余后台代码。


2:在写jsp页面的时候真的改不了自己粗心大意的毛病,报的这么个错误:

Property 'name' not found on type java.lang.String

/WEB-INF/role/role_list.jsp at line 51


48:               ${module.name }
49:               </c:otherwise> 
50:               </c:choose> --%>
51:                  ${module.name }  
52:               </c:forEach>
53:               </td>
54:               <td>

我看了许久,也查看了module是否有值,很遗憾有值,根本不是值得问题,于是

翻了下博客发现原来是标签的值多了一个空格:

<c:forEach items="${role.modules} " var="module" varStatus="s">
              <%-- <c:choose>
              <c:when test="${s.last }">
              ${module.name }
              </c:when>
              <c:otherwise>
              ${module.name }
              </c:otherwise> 

注意到了吗items="${role.modules} " 多了个空格,由于还不太会这编辑器还是手撕算了,

正确写法items="${role.modules}" 

好了今天到此为止,合理安排自己的时间才能更加有效率,晚安!

1 0