【尚硅谷】SSM框架全套教程-讲师:杨博超
失败,是正因你在距成功一步之遥的时候停住了脚步。
// 根据id查询用户信息
User getUserById(@Param("id") String id);
<select id="getUserById" resultType="User">
select id, username, `password`, age, gender, email
from t_user
where id = #{id};
select>
@Test
public void testGetUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById("1");
sqlSession.close();
System.out.println(user);
}
// 查询所有的用户信息
List<User> getAllUser();
<select id="getAllUser" resultType="User">
select id, username, `password`, age, gender, email
from t_user;
select>
@Test
public void testGetAllUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<User> list = mapper.getAllUser();
sqlSession.close();
list.forEach(System.out::println);
}
MyBatis中为Java中常用的类型设置了类型别名
Integer: Integer,int
int: _int,_integer
Map: map
String: string
// 查询数量
int getCount();
<select id="getCount" resultType="_int">
select count(*)
from t_user
select>
@Test
public void testGetCount() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
int reuslt = mapper.getCount();
System.out.println("数量为:" + reuslt);
}
// 根据id获取一条数据,存储map中
Map<String,Object> getUserByIdToMap(@Param("id") String id);
<select id="getUserByIdToMap" resultType="map">
select *
from t_user
where id = #{id};
select>
@Test
public void testGetUserByIdToMap() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getUserByIdToMap("1");
// {password=123, gender=男, id=1, age=23, [email protected], username=ybc}
//Map map = mapper.getUserByIdToMap("7");
//这条数据部分字段为null:{password=12, id=7, username=tt}
System.out.println(map);
}
SelectMapper.java
// 查询所有用户信息,使用map集合
List<Map<String,Object>> getAllUserToMap();
SelectMapper.xml
<select id="getAllUserToMap" resultType="map">
select id, username, `password`, age, gender, email
from t_user;
select>
测试
@Test
public void testGetAllUserToMap() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> list = mapper.getAllUserToMap();
sqlSession.close();
for (Map<String, Object> map : mapList) {
System.out.println(map);
}
/**
* {password=123, gender=男, id=1, age=23, [email protected], username=ybc}
* {password=4477, gender=男, id=3, age=23, [email protected], username=zty}
*/
}
SelectMapper.java
// 查询所有用户信息为map集合
@MapKey("id")
Map<String,Object> getAllUserToMapMap();
SelectMapper.xml
<select id="getAllUserToMapMap" resultType="map">
select id, username, `password`, age, gender, email
from t_user;
select>
测试
@Test
public void testGetAllUserToMapMap() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getAllUserToMapMap();
sqlSession.close();
System.out.println(map);
/** 结果
* {
* 1={password=123, gender=男, id=1, age=23, [email protected], username=ybc},
* 3={password=4477, gender=男, id=3, age=23, [email protected], username=zty}
* }
*/
}