mybatis中的参数问题
样例:根据用户名查询用户信息
mybatis中获取参数值的方式有两种#{}和${}
1. 若mapper接口方法的参数为单个的字面量类型
User getUserByUsername(String username);
<!--User getUserByUsername(String username);--> <select id="getUserByUsername" resultType="User"> <!-- select * from t_user where username = #{username} --> select * from t_user where username = '${username}' </select>
@Test public void testGetUserByUsername(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserByUsername("admin"); System.out.println(user); }
此时用#{}和${}任意内容获取参数值,注意${}的单引号问题
#{}的本质是占位符 ${}的本质是sql语句拼接
2.若mapper接口方法的参数为多个的字面量类型
/** * 验证登录 * @param username * @param password * @return */ User checkLogin(String username, String password);
<!-- User checkLogin(String username, String password); --> <select id="checkLogin" resultType="User"> <!--select * from t_user where username =#{arg0} and password =#{arg1}--> select * from t_user where username ='${arg0}' and password ='${arg1}' </select>
@Test public void testCheckLogin(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.checkLogin("admin","123456"); System.out.println(user); }
此时mabatis会将参数放在map集合中,一两种方式进行存储
a> arg0,arg1.... b> param1,param2...
需要通过#{}和${}获取mapper中的值
3.若mapper接口的参数为map集合时
/** * 验证登录 以map集合作为参数方式 * @param map * @return */ User checkLoginByMap(Map<String, Object> map);
<!--User checkLoginByMap(Map<String, Object> map);--> <select id="checkLoginByMap" resultType="com.gu.mybatis.pojo.User"> select * from t_user where username =#{username} and password =#{password} </select>
@Test public void testCheckLoginByMap(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map< String, Object> map = new HashMap<>(); map.put("username", "admin"); map.put("password","123456"); User user = mapper.checkLoginByMap(map); System.out.println(user); }
需要通过#{}和${}获取mapper中的值
键值要与map集合中的键值一致
4. 若mapper接口方法的参数为实体类类型的参数
/** * 添加用户 * @param user */ void insertUser(User user);
<!-- void insertUser(User user); --> <insert id="insertUser"> insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email}) </insert>
@Test public void testInsertUser(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(null,"root","123456",33,"女","hahahaha"); mapper.insertUser(user); }
只需要通过#{}和${}访问实体类中的属性名,可以获取相应的属性值
5. 在mapper接口的参数上设置@Param注解
/** * 验证登录,使用param注解 * @param username * @param password * @return */ User checkLoginByParam(@Param("username") String username,@Param("password") String password);
<!--User checkLoginByParam(@Param("username") String username,@Param("password") String password);--> <select id="checkLoginByParam" resultType="com.gu.mybatis.pojo.User"> select * from t_user where username=#{username} and password = #{password} </select>
@Test public void testCheckLoginByParam(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.checkLoginByParam("admin","123456"); System.out.println(user); }
mybatis会将这些参数放在map中,以两种方式存储标签:username,mapper,day111,map,参数,user,mybatis,password,User From: https://www.cnblogs.com/GUGUZIZI/p/17496859.html
* a>以注解的value属性为键,以参数为值
* b>以param1,param2为键,以参数值只需要通过#{}和${}访问map的属性名,可以获取相应的属性值