++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
注意namespace属性值为对应接口的全限定类名
<?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="org.rain.mybatis.mapper.SelectMapper">
</mapper>
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
User getUserById(@Param("id") Integer id);
<!--User getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>
@Test
public void testGetUserById(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
User user = selectMapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
List<User> getAllUser();
<!--List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
select * from t_user
</select>
@Test
public void testGetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
List<User> users = selectMapper.getAllUser();
for (User user : users) {
System.out.println(user);
}
}
Integer getCount();
注意:resultType属性的值除了可以是java.lang.Integer之外,还可以是Integer、integer、int或INT等;
这是因为mybatis对Java中常用的类型都设置了类型别名。
<!--Integer getCount();-->
<select id="getCount" resultType="java.lang.Integer">
select count(*) from t_user
</select>
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
Integer count = selectMapper.getCount();
System.out.println(count);
sqlSession.close();
}
Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
<!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id}
</select>
@Test
public void testGetUserByIdToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> userByIdToMap = selectMapper.getUserByIdToMap(1);
System.out.println(userByIdToMap);
sqlSession.close();
}
注意:当某个字段值为null时,该字段不会放进map集合中
List<Map<String,Object>> getAllUserToMap();
<!--List<Map<String,Object>> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> allUserToMap = selectMapper.getAllUserToMap();
for (Map<String, Object> map : allUserToMap) {
System.out.println(map);
}
sqlSession.close();
}
可以通过@MapKey注解设置大map集合的键为数据中的某个字段值(该字段值不能存在重复),值是每条数据所对应的小map集合
@MapKey("ID")
Map<String,Object> getAllUserToMap();
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> allUserToMap = selectMapper.getAllUserToMap();
System.out.println(allUserToMap);
sqlSession.close();
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章