public interface SelectMapper {//根据ID查询用户信息User getUserById(@Param("id") Integer id);//查询所有用户信息List<User> getAllUser();//查询用户信息的总记录数Integer getCount();//查询用户信息使用mapMap<String, Object> getUserByIdToMap(@Param("id") Integer id);//查询指定年龄的人的信息String getNameByAge(@Param("age") Integer age);//查询所有用户信息 使用ListMapList<Map<String,Object>> getAllUserToListMap();//查询所有用户信息使用Map@MapKey("id")Map<String,Object> getAllUserToMap();
}
/** @author h* @desc* Mybatis中获取参数值的两种方式:${},#{}* ${}本质是字符串拼接* #{}本质是占位符* Mybatis获取参数值的各种情况:* 1、mapper接口方法的参数* 2、mapper接口方法的参数有多个* 此时Mybatis会将参数放入一个Map集合中,以两种方式进行存储* 3、若mapper接口方法的参数有多个时,可以手动将这些参数放在map中存储* 4、mapper接口方法的参数是实体类类型的参数* 通过属性来访问即可* 5、命名参数(注解)*/@Testpublic void getAllUser(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);List<User> users = mapper.getAllUser();for (Object o: users) {System.out.println(o);}}@Testpublic void getUserByUsername(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);User user = mapper.getUserByUsername("张三");System.out.println(user);}@Testpublic void checkLogin(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);User user = mapper.checkLogin("张三","123456");System.out.println(user);}@Testpublic void checkLoginByMap(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);Map<String,Object> map = new HashMap<>();map.put("username","张三");map.put("password","123456");User user = mapper.checkLoginByMap(map);System.out.println(user);}@Testpublic void insertUser(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);int result = mapper.insertUser(new User(null,"李四","abc123",23,"男","123@qq.com"));System.out.println(result);}}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.mapper.ParameterMapper"><!--List<User> getAllUser();--><select id="getAllUser" resultType="User">select * from t_user</select><!--User getUserByUsername(String name)--><select id="getUserByUsername" resultType="User"><!--#{}中的值可以为任意,只是想跟参数名保持一致--><!-- select * from t_user where username = #{username}-->select * from t_user where username = ${username}</select><!--User checkLogin(String username, String password);--><select id="checkLogin" resultType="User">select * from t_user where username = #{arg0} and password = #{arg1}</select><!--User checkLoginByMap(Map<String,Object> map);--><select id="checkLoginByMap" resultType="User">select * from t_user where username = #{username} and password = #{password}</select><!--int insertUser(User user);--><insert id="insertUser">insert into t_user values(null, #{username},#{password},#{age},#{sex},#{email})</insert>
</mapper>
注意最后一个案例 增加user的操作中
因为mybatis中底层是用Map来表示的,当操作的对象是一个实体类时,只需要用其属性值作为键即可得到对应的值。
常用的为@Param()
#{id} @Param中的键所对应的值