mybatis_select_标签

mybatis select的输入和输出

例如 select id,name,age from user where name like '%王%' and age>20

这里既有输入也有输出

select的属性
id,parameterClass,resultClass,parameterMap,resultMap,cacheModel
resultSetType,fetchSize,xmlResultName,remapResults,timeout
输入 parameterClass,ParameterMap
resultSetTYpe为结果集类型,主要是说游标,具有方向性,可以取
forward_only,scoll_insensitive,scoll_sensitive,具体情况具体配置

fetchSize 预取回数据量的大小

xmlResultName,返回result为xml的名称

如果返回的结果可以修改则吧remapResults设为true,否则设为false

输入,用参数带进来
可以是#,$做为占位符

不同占位符使用


Xml代码
<SPAN style="FONT-SIZE: small"><select id="getAllUsers1" resultClass="com.mypackage.User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>
<select id="getAllUsers1" resultClass="com.mypackage.User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT like '$department$'
</select>
</SPAN>
Xml代码
<SPAN style="FONT-SIZE: small"><SPAN style="FONT-SIZE: x-small"><select id="getAllUsers1" resultClass="com.mypackage.User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>
<select id="getAllUsers1" resultClass="com.mypackage.User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT like '$department$'
</select>
</SPAN></SPAN>

#不能放在‘’之内

resultMap



Xml代码
<SPAN style="FONT-SIZE: small"><!-- 无映射 -->
<select id="getAllUsers1" resultClass="User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 内联映射->实体类 -->
<select id="getAllUsers2" resultClass="User"
parameterClass="string">
SELECT USERID as userid, USERNAME as username, PASSWORD as
password, DEPARTMENT as department FROM USER WHERE
DEPARTMENT=#department#
</select>

<!-- 内联映射->MAP类 -->
<select id="getAllUsers3" resultClass="hashmap"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 显示映射->实体类 -->
<resultMap id="accoutResult" class="User">
<result property="userid" column="USERID" />
<result property="username" column="USERNAME" />
<result property="password" column="PASSWORD" />
<result property="department" column="DEPARTMENT" />
</resultMap>
<select id="getAllUsers4" resultMap="accoutResult"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 显示映射->MAP类 -->
<resultMap id="mapResult" class="hashmap">
<result property="userid" column="USERID" />
<result property="username" column="USERNAME" />
<result property="password" column="PASSWORD" />
<result property="department" column="DEPARTMENT" />
</resultMap>
<select id="getAllUsers5" resultMap="mapResult"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

</SPAN>
Xml代码
<SPAN style="FONT-SIZE: small"><SPAN style="FONT-SIZE: x-small"><!-- 无映射 -->
<select id="getAllUsers1" resultClass="User"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 内联映射->实体类 -->
<select id="getAllUsers2" resultClass="User"
parameterClass="string">
SELECT USERID as userid, USERNAME as username, PASSWORD as
password, DEPARTMENT as department FROM USER WHERE
DEPARTMENT=#department#
</select>

<!-- 内联映射->MAP类 -->
<select id="getAllUsers3" resultClass="hashmap"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 显示映射->实体类 -->
<resultMap id="accoutResult" class="User">
<result property="userid" column="USERID" />
<result property="username" column="USERNAME" />
<result property="password" column="PASSWORD" />
<result property="department" column="DEPARTMENT" />
</resultMap>
<select id="getAllUsers4" resultMap="accoutResult"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

<!-- 显示映射->MAP类 -->
<resultMap id="mapResult" class="hashmap">
<result property="userid" column="USERID" />
<result property="username" column="USERNAME" />
<result property="password" column="PASSWORD" />
<result property="department" column="DEPARTMENT" />
</resultMap>
<select id="getAllUsers5" resultMap="mapResult"
parameterClass="string">
SELECT * FROM USER WHERE DEPARTMENT=#department#
</select>

</SPAN></SPAN>



map映射 ,返回的list里的每一项都是一个map然后有键值对

xml映射


Xml代码
<SPAN style="FONT-SIZE: small"><!-- XML -->
<select id="selectXML" parameterClass="string" resultClass="xml"
xmlResultName="log">
SELECT * FROM USER_ACCOUNT WHERE GROUPNAME=#groupName#
</select>
</SPAN>
Xml代码
<SPAN style="FONT-SIZE: small"><SPAN style="FONT-SIZE: x-small"><!-- XML -->
<select id="selectXML" parameterClass="string" resultClass="xml"
xmlResultName="log">
SELECT * FROM USER_ACCOUNT WHERE GROUPNAME=#groupName#
</select>
</SPAN></SPAN>

resultClass="xml"这个xml也是预定义好的类
在测试类里调用后返回的是xml格式的字符串,指定的xmlResultName相当于xml的
根元素



返回结果有三类,1 返回直接指定的类 2 返回map类型 3 返回xml

你可能感兴趣的:(mybatis)