Only used when using mybatis alone, the last ssm integration without writing code below
IS = Resources.getResourceAsStream InputStream ( "MyBatis.xml" ); // read the profiles created // use factory design pattern
SqlSessionFactory Factory's new new the SqlSessionFactoryBuilder = () Build (IS);. // production
SqlSession SqlSession session = factory. the openSession ();
List <Object> = session.selectList List ( "com.lhf.mapper.UserMapper.selectAll");
for(Object object:list){
System.out.println(((User)object).getUname());
}
session.close();
is.close();
Three kinds of query
1.selectList () returns a value of List <resultType property controls>
1.1 applies to the query results need to traverse demand
List<Object> list=session.selectList("com.lhf.mapper.UserMapper.selectAll"); for(Object object:list){ System.out.println(((User)object).getUname()); }
2.selectOne () Return Value Object,
2.1 applies to only return results or variable data row
User user = session.selectOne("com.lhf.mapper.UserMapper.selectOne");
System.out.println(user);
3.selectMap () Returns the value of the Map
is suitable for the need to take a value to a column line of data in the query results demand 3.1.
3.2Map <Key, the resultType control>
Map<Object,Object> map = session.selectMap("com.lhf.mapper.UserMapper.selectMap","uname");//后面的是(Value,Key) System.out.println(map);