Mybaits笔记框架:https://blog.csdn.net/qq_43751200/article/details/128154837
Mybatis中文官方文档: https://mybatis.org/mybatis-3/zh/index.html
SelectMapper接口
/**
* 根据用户id查询用户信息
注意:resultType="User"
* @param id
* @return
*/
User getUserById(@Param("id") int id);
SelectMapper.xml
<select id="selectById" resultType="User">
select * from t_user where id = #{id};
select>
测试方法
@Test
public 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<User> getUserList();
SelectMapper.xml
<select id="getUserList" resultType="User">
select * from t_user;
select>
测试方法
@Test
public void testGetUserList(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
List<User> 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
<select id="getCount" resultType="int">
select count(id) from t_user;
select>
测试方法
@Test
public 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<String, Object> getUserToMap(@Param("id") int id);
SelectMapper.xml
<select id="getUserToMap" resultType="map">
select * from t_user where id = #{id}
select>
测试方法
@Test
public void testGetUserToMap(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> userToMap = selectMapper.getUserToMap(18);
System.out.println(userToMap);
sqlSession.close();
}
SelectMapper接口
/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取
* 这时候的resultType="map",指定泛型为Map类型
*/
List<Map<String, Object>> getAllUserToMap();
SelectMapper.xml
<select id="getUserToMaps" resultType="map">
select * from t_user;
select>
测试方法
@Test
public void testGetUserToMaps(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> userToMaps = selectMapper.getUserToMaps();
for(Map<String, Object> map : userToMaps){
System.out.println(map);
}
sqlSession.close();
}
SelectMapper接口
/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的map集合
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();
SelectMapper.xml
<select id="getAllUserToMap" resultType="map">
select * from t_user
select>
测试方法
@Test
public void testGetUserToMaps(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> userToMaps = selectMapper.getUserToMaps();
System.out.println(userToMaps);
Set<Map.Entry<String, Object>> entries = userToMaps.entrySet();
for(Map.Entry<String, Object> entry : entries){
System.out.println(entry);
}
sqlSession.close();
}