public interface SqlSession extends Closeable {
/**
* Retrieve a single row mapped from the statement key
* @param <T> the returned object type
* @param statement
* @return Mapped object
*/
<T> T selectOne(String statement);
/**
* Retrieve a single row mapped from the statement key and parameter.
* @param <T> the returned object type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return Mapped object
*/
<T> T selectOne(String statement, Object parameter);
}
<select id="findOne" resultType="pojo.User">
select * from user where id=1;
</select>
//根据id查询记录
@Test
public void findOne(){
// 2.创建sqlsession对象,执行sql
SqlSession session = ssf.openSession();
User user = session.selectOne("userns.findOne");
System.out.println(user);
// 4.释放资源
session.close();
}
等价查询,这种方式更灵活
<!--根据id查询记录-->
<select id="findOne" parameterType="int" resultType="pojo.User">
select * from user where id=#{id};
</select>
//根据id查询记录
@Test
public void findOne(){
// 2.创建sqlsession对象,执行sql
SqlSession session = ssf.openSession();
User user = session.selectOne("userns.findOne",1);
System.out.println(user);
// 4.释放资源
session.close();
}