Mybaits笔记框架:https://blog.csdn.net/qq_43751200/article/details/128154837
SelectMapper接口
/*** 根据用户id查询用户信息注意:resultType="User"* @param id* @return*/
User getUserById(@Param("id") int id);
SelectMapper.xml
测试方法
@Testpublic void testSelectById(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);User user = selectMapper.selectById(18);System.out.println(user);sqlSession.close();}

SelectMapper接口
/*** 查询所有用户信息* 注意:resultType="User", 指定泛型* @return*/
List getUserList();
SelectMapper.xml
测试方法
@Testpublic void testGetUserList(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);List userList = selectMapper.getUserList();for(User user : userList){System.out.println(user);}sqlSession.close();}

SelectMapper接口
/** * 查询用户的总记录数 * @return * 在MyBatis中,对于Java中常用的类型都设置了类型别名 * 例如:java.lang.Integer-->int|integer * 例如:int-->_int|_integer * 例如:Map-->map,List-->list */
int getCount();


SelectMapper.xml
测试方法
@Testpublic void testGetCount(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);int count = selectMapper.getCount();System.out.println("t_user表中一共有" + count + "条数据");sqlSession.close();}

SelectMapper接口
/*** 根据用户id查询用户信息为map集合* @param id* @return*/Map getUserToMap(@Param("id") int id);
SelectMapper.xml
测试方法
@Testpublic void testGetUserToMap(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);Map userToMap = selectMapper.getUserToMap(18);System.out.println(userToMap);sqlSession.close();}

SelectMapper接口
/** * 查询所有用户信息为map集合 * @return * 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取 * 这时候的resultType="map",指定泛型为Map类型*/
List
SelectMapper.xml
测试方法
@Testpublic void testGetUserToMaps(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);List

SelectMapper接口
/*** 查询所有用户信息为map集合* @return* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的map集合*/
@MapKey("id")
Map getAllUserToMap();
SelectMapper.xml
测试方法
@Testpublic void testGetUserToMaps(){SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);Map userToMaps = selectMapper.getUserToMaps();System.out.println(userToMaps);Set> entries = userToMaps.entrySet();for(Map.Entry entry : entries){System.out.println(entry);}sqlSession.close();}

