注解的利用一般与xml配置互相补充,注解一般用于比较简单的查询工作,在涉及到多表连接查询时还是尽量使用xml配置中的相关方法,能有效避免由于对象内部死循环造成的StackOverFlowErro 栈溢出问题
一般简单的查询注解:
@Select("Select * from t_insect")@Results(id="insectMap",value={@Result(id="ture",column="pk_id",porperty="pkId"),@Result(column="insect",porperty="insect"),@Result(colmun="host",property="host"),@Result(colmun="danger",property="danger")})
List<TInsect>getList(TInsect t)
进行方法调用:
@Test
SqlSession sqlSession= null;publicvoidgetList(){
InsectDao mapper= sqlSession.getMapper(InsectDao.class);
Tinsect t= null;
List<TInsect>list= mapper.getList(t);
System.out.println("list = "+ list);}
如果是有相应的条件进行限定查询,select注解中加入条件即可:
@Select("Select * from t_insect where pk_id = #{pkId}")@ResultMap("insectMap")
TInsectgetOneInsect(@Param("pkId")int InsectId);
测试:
@TestpublicvoidgetOneInsect(){
InsectDao mapper= sqlSession.getMapper(InsectDao.class);
TInsect t= mapper.getOneInsect(2);
System.out.println("t = "+ t);}