标签:xml文件 blog hash 返回 sele parameter pre key use
User selectOne(User user);
<select id="selectOne" parameterType="cn.lyn4ever.entity.User" resultType="cn.lyn4ever.entity.User">
select id,username,telphone from user where telphone=#{telphone} and password = #{password}
</select>
List<User> selectList(User user);
<select id="selectOne" parameterType="cn.lyn4ever.entity.User" resultType="cn.lyn4ever.entity.User">
select id,username,telphone from user
</select>
resultType
改为java.util.HashMap
,这会有生成下边这样Map selectList(User user);
<select id="selectOne" parameterType="cn.lyn4ever.entity.User" parameterType="cn.lyn4ever.entity.User" resultType="java.util.HashMap">
select id,username,telphone from user where telphone=#{telphone} and password = #{password}
</select>
上这结果就是说,以User类的属性名为key,属性值为value。
List<Map> selectList(User user);
<select id="selectOne" parameterType="cn.lyn4ever.entity.User" resultType="java.util.HashMap">
select id,username,telphone from user
</select>
{
"01":{
username:"zhangsan",
telphone:"13000000000"
}
}
也就是我们要自定义一个Map<String,User>,这样的话,有以下两种解决方法:
@MapKey("id")
Map<String,User> getUserInMap();
<!--xml和之前写法一样-->
<select id="getUserInMap" parameterType="cn.lyn4ever.entity.User" resultType="User">
select id,username,telphone from user where telphone=#{telphone} and password = #{password}
</select>
这个@MapKey的value写的就是User对象的一个属性
HashMap<String,Object> getUserInMap();
<select id="getUserInMap" parameterType="cn.lyn4ever.entity.User" resultType="java.util.HashMap">
SELECT id as ‘key‘, * as ‘value‘, id,username,telphone from user where telphone=#{telphone} and password = #{password}
</select>
当然以上这两种方法,如果查出来是多条的话也会是List
reference:https://www.cnblogs.com/Lyn4ever/archive/2019/09/30/11614416.html
标签:xml文件 blog hash 返回 sele parameter pre key use
原文地址:https://www.cnblogs.com/marxtsui/p/14040481.html