mybatis通过多个参数查询

public interface UserDao {

User finMap(Map map);
}
-------------------------------------

<select id="finMap" resultType="pojo.User">
select * from user where id=#{id} and username=#{username} and age=#{age}
</select>
---------------------------------------
--------------通过Map集合查询(可以单个参数也可以多个参数)-----------------------------
Map map=new HashMap<>();
map.put("id",1);
map.put("username","UZI");
map.put("age",19);

User user1 = userDao.finMap(map);

System.out.println(user1);
session.close();
}
}


--------------多个参数查询,通过注解@——————————————————————————————————————————————-——————
User findtwo(@Param("id") int id, @Param("username") String username);

----------------------------------------------------------------------------------
<select id="findtwo" resultType="pojo.User">
select * from user where id=#{id} and username=#{username}
</select>
------------------------------------------------------------------------------------
User user = userDao.findtwo(2, "PDD");
System.out.println(user);
session.close();
------------------------------------------------------------------------------
(为什么多参数会报错,设计时候源码只写了一个 obj 多个参数时候会封装成一个Map对象 agr或者param作为key值)


--------------插入操作----------------------------------------------------------------
void insertUser(User user); (这边不需要返回值,不然会报错)
insert id="insertUser">
insert into user values (#{id},#{username},#{age},#{address})
</insert>


userDao.insertUser(new User(3,"若风",35,"黑龙江"));

session.close();
 
posted @ 2022-01-23 21:59  谢远栋  阅读(331)  评论(0)    收藏  举报