当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

ibatis There is no statement named XXXX 常见的一个解决方案

发表于: 2011-04-21   作者:爪哇岛岛主   来源:转载   浏览:
摘要: 有两个类: public class Person { private Integer id; private String firstName; private String lastName; private String email; private Department dept; // getter and
有两个类:

public class Person {

	private Integer id;
	private String firstName;
	private String lastName;
	private String email;
	private Department dept;
          
          // getter and setter methods 
}

public class Department {

	private Integer id;
	private String name;
	private Set<Person> persons;

         //getter and setter methods
}

现在通过Person提供的一个ID号,来查询一条person记录,记录包括:
id
firstName
lastName
email
dept.id
dept.name



   SQL MAP  xml文件
   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org//dtd//sql-map-2.dtd">

<sqlMap namespace="Person">

	<typeAlias alias="Person" type="com.ymq.ibatis.Person"/>
	<typeAlias alias="Dept" type="com.ymq.ibatis.Department"/>
	
	<resultMap id="get-person-result" class="Person">
		
		<result property="id" column="id"/>
		<result property="firstName" column="firstName"/>
		<result property="lastName" column="lastName"/>
		<result property="email" column="email"/>
		<result property="dept" column="dept_id" select="Person.getDept"/>
		
	</resultMap>
	
	<resultMap id="get-dept-result" class="Dept">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
	</resultMap>
	
	<select id="getDept" parameterClass="int" resultMap="get-dept-result">
		select * from department where id=#value#
	</select>
	
	<select id="getPerson" resultMap="get-person-result">
		select * from person where id=#value#
	</select>

	
</sqlMap>



测试代码
  @SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException ,SQLException{
		Reader reader = Resources.getResourceAsReader("com/ymq/ibatis/SqlMapConfig.xml");
		SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader );
		System.out.println("查询所有的用户信息");
		Person person = (Person)sqlMap.queryForObject("Person.getPerson", 1);
		
		System.out.println("id:"+person.getId());
		System.out.println("name:"+person.getFirstName()+person.getLastName());
		System.out.println("deptId:"+person.getDept().getId());
		System.out.println("deptName:"+person.getDept().getName());
	}



报错:ibatis There is no statement named “getDept”。。。。

仔细观察知道


<resultMap id="get-person-result" class="Person">
		
		<result property="id" column="id"/>
		<result property="firstName" column="firstName"/>
		<result property="lastName" column="lastName"/>
		<result property="email" column="email"/>
		<result property="dept" column="dept_id" select="getDept"/>



这句:<result property="dept" column="dept_id" select="getDept"/>

红色字体要用select="Person.getDept"代替,因为设置了命名空间。

ibatis There is no statement named XXXX 常见的一个解决方案

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号