首页 > 代码库 > Mybatis返回list<object>方法

Mybatis返回list<object>方法

哎,查了很久百度,竟然没有直接的例子... 自己参考其他例子,写了一个...

其他的忽略,直接上关键代码:

转自http://www.xuebuyuan.com/782623.html

pojo 类:

package com.mymaven.mybatisdemo.po;public class Department {	private String dp_id;	private String dp_name;	private String cost_center;	public String getDp_id() {		return dp_id;	}	public void setDp_id(String dp_id) {		this.dp_id = dp_id;	}	public String getDp_name() {		return dp_name;	}	public void setDp_name(String dp_name) {		this.dp_name = dp_name;	}	public String getCost_center() {		return cost_center;	}	public void setCost_center(String cost_center) {		this.cost_center = cost_center;	}	public Department() {		super();	}}

mapper接口:

public interface DepartmentMapper {	//查询返回一个list	public List<Department> queryAllDepartment();}

mapper对应的配置文件:

<!-- 此处namespace需要指定dao接口 --><mapper namespace="com.mymaven.mybatisdemo.dao.DepartmentMapper">    <!--配置一个resultMap 指定返回的类型 -->    <resultMap id="departMent" type="Department">        <id column="dp_id" property="dp_id" />        <result column="dp_name" property="dp_name" />        <result column="cost_center" property="cost_center" />    </resultMap>    <!-- 返回一个list的写法 -->    <select id="queryAllDepartment"  resultMap="departMent" >        select * from t_department    </select>   </mapper>

注意
<select id="queryAllDepartment"  resultMap="departMent" 这里返回类型是resultMap,不是ResultType.这个我搞错了。。。

Mybatis返回list<object>方法