myBatis的基础查询
单条或多条查询
根据id查询单条数据,查询所有数据的列表集合,查询所有数据的条目,查询出单条数据返回值为map,查询多条数据返回值为列表,查询多条数据返回值为map
步骤
创建SelectMapper接口和SelectMapper.xml映射文件和test类
SelectMapper
package com.wjw.mybatis.mapper;
import com.wjw.mybatis.pojo.User;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* @Author wjwdive
* @Date 2023/2/25 22:39
* @Description:
*/
public interface SelectMapper {
/**
* 根据Id查询用户信息
* @param id
* @return
*/
User getUserById(@Param("id") Integer id);
/**
* 查询所有用户信息
* @return
*/
List<User> getAllUser();
/**
* 查询用户的总数量
* @return
*/
Integer getCount();
/**
* 根据id查询用户信息为map集合
* @param id
* @return
*/
Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
/**
* 查询所有用户信息返回值为 List<Map<String, Object>
* @return
*/
List<Map<String, Object> >getAllUserToMap();
/**
* 查询所有用户信息,返回值为 map 要用@Mapkey("id") 注解
* @return
*/
@MapKey("id")
Map<String, Object> getAllUserToMap2();
}
SelectMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wjw.mybatis.mapper.SelectMapper">
<!-- User getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id};
</select>
<!-- List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
select * from t_user;
</select>
<!-- Integer getCount();-->
<select id="getCount" resultType="int">
select count(*) from t_user;
</select>
<!-- Map<String, Object> getUserByIdToMap(@Param("id") Integer id);-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id};
</select>
<!-- List<Map<String, Object> >getAllUserToMap();-->
<select id="getAllUserToMap" resultType="User">
select * from t_user;
</select>
<!-- List<Map<String, Object> >getAllUserToMap2();-->
<select id="getAllUserToMap2" resultType="User">
select * from t_user;
</select>
</mapper>
test类
package com.wjw.mybatis.test;
import com.wjw.mybatis.mapper.SelectMapper;
import com.wjw.mybatis.pojo.User;
import com.wjw.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
import java.util.Map;
/**
* @Author wjwdive
* @Date 2023/2/25 23:22
* @Description:
*/
public class SelectMapperTest {
@Test
public void testSelectUserById() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById(2);
System.out.println("user: " + user );
}
@Test
public void testGetAllUsers() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<User> list = mapper.getAllUser();
System.out.println("users: ");
list.forEach(System.out::println);
// System.out.println("users: " + list );
}
@Test
public void testGetCount() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Integer totalUsers = mapper.getCount();
System.out.println("totalUser counts: " + totalUsers );
}
@Test
public void testGetUserByIdToMap() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getUserByIdToMap(3);
System.out.println("user map: " + map);
}
@Test
public void getAllUserToMap() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object> > list = mapper.getAllUserToMap();
System.out.println("all user list of map: " + list);
}
@Test
public void getAllUserToMap2() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getAllUserToMap2();
System.out.println("all users of map: " + map);
}
}
标签:mapper,SelectMapper,map,ssm,sqlSession,23001,mybatis,import,id
From: https://www.cnblogs.com/wjw-blog/p/17155814.html