( 结果:{update_time=2022-07-17T12:15:23, deleted=0, create_time=2022-07-16T22:06:50, name=双双4, id=1548240344215633922, version=3, age=23, email=146578237@qq.com}
以字段为键以字段所对应的值为值 )
- /*
- * 查询单条数据
- * */
- Users selectUsers(@Param("name") String name);
-
-
-
- select * from users where name = #{name}
-
-
-
-
- @Test
- public void testSelectMapper(){
- SqlSession sqLsession = SQLsession.getSqlsession();
- SelectMapper mapper = sqLsession.getMapper(SelectMapper.class);
- Users users = mapper.selectUsers("双双");
- System.out.println(users);
- }
(注意在多条数据的时候一定不能用实体类对象来进行接收,如果用实体类对象来接收的话会抛出异常TooManyResultsException)
- /*
- * 查询多条数据
- * */
- List
selectUser(@Param("name") String name); -
-
-
- select * from users where name = #{name}
-
-
-
-
- @Test
- public void testSelectMappers(){
- SqlSession sqLsession = SQLsession.getSqlsession();
- SelectMapper mapper = sqLsession.getMapper(SelectMapper.class);
- List
users = mapper.selectUser("双双"); - System.out.println(users);
- }
(注意:因为返回值为Integer,所以在mapper.xml映射文件当中resutlType应该返回Integer
还可以写成 int , Integer, integer 因为这是mybatis默认规定:详情可查阅mybatis官网)
- /*
- * 查询总记录数
- * */
- Integer getCount();
-
- select count(*) from users
-
-
-
- @Test
- public void tsetgetCount(){
- SqlSession sqLsession = SQLsession.getSqlsession();
- SelectMapper mapper = sqLsession.getMapper(SelectMapper.class);
- int users = mapper.getCount();
- System.out.println(users);
- }
- /*
- * 根据name查询用户信息为一个map集合
- * */
- Map
getUserBynameToMap(@Param("name") String name); -
-
- select * from users where name = #{name}
-
-
-
- @Test
- public void tsetgetUserBynameToMap(){
- SqlSession sqLsession = SQLsession.getSqlsession();
- SelectMapper mapper = sqLsession.getMapper(SelectMapper.class);
- Map
users = mapper.getUserBynameToMap("双双4"); - System.out.println(users);
- }
- /*
- * 查询所有的用户信息为map集合
- * */
- // List
- @MapKey("id")
- Map
getAllUserToMap(); -
-
-
- select * from users
-
-
-
- @Test
- public void tsetgetAllUserToMap(){
- SqlSession sqLsession = SQLsession.getSqlsession();
- SelectMapper mapper = sqLsession.getMapper(SelectMapper.class);
- // List
- Map
users = mapper.getAllUserToMap(); - System.out.println(users);
- }