欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ibatis3 简单示例 博客分类: java JavaiBATISSQLDAOJSP 

程序员文章站 2024-02-18 10:32:34
...
ibatis3 已经发布了。跟ibatis2还是有一定的区别的。
总的来说,配置更简单了。当然,你还可以到网上找些自动生成的工具。
那样更方便了 , 国内国外都有。推荐一下:http://tomcatmonitor.googlecode.com/files/abator.swf 

iBATIS 3 是一个 ORM 持久框架,用来将 Java 对象内的属性映射到数据库内的表列。iBATIS 以映射为中心,重点在于映射一个优秀的对象模型与一个优秀的关系型数据库设计。

iBATIS 3 内的一个新特性是使用 Java 注释进行映射,这就使得映射更为整洁也更为直观,而且可以以 Java源代码的形式为很多项目所用。iBATIS 3 还提供了使用 XML配置文件进行对象映射的功能。这种双重配置方法的存在让您可以为自己的项目选用最为简单的配置 iBATIS 的方式。


iBATIS 3 带来了一些新的变化,允许您利用 Java 5 注释。通过使用注释,您可以创建 mapper 接口来供您在 Java 代码内进行从对象到数据库的全部映射。清单 8 所示的代码展示了这个用于配置(并非 XML 配置)的 AutomobileMapper 接口。

在 Automobile mapper 的 XML 配置中,resultMap 元素用来映射 model_year 数据库列与 Automobile 对象上的 year 字段。这个映射相当简单,可以在注释内进行,只要使用为该列赋别名的 SQL 特性就可以了,这个特性可由在 @Select 注释内定义的 SQL 完成。

AutomobileMapper 接口内的 Select 注释将 selectAutomobile 方法映射到用来根据给定值从 automobiles 表选择一个记录的 SQL。这个值被指定为实参的 id 参数并被作为 #{id}包括在这个 SQL 语句内,正如其在 XML 配置中那样。使用 Java 接口映射这些 SQL 方法的一个极大的好处是 feedbac会以编译时错误的形式出现在 Java 编辑器中。这样一来,我们就可以确认这些方法均能返回正确的类型,而 XML配置通常需要先执行代码才能找到错误。

此外,iBATIS 3 现在还支持接口继承,允许对 Java 接口进行优化以减少代码重复。

iBATIS 文档中有这样的一个提示,即对于较小且较为简单的项目,注释可以更为简单和易读。不过,较 XML 配置而言,注释的功能相对有限。若项目中包含复杂的对象或复杂的数据库结构,请考虑使用 XML 配置,而不是 Java 注释。


先说SQL映射文件 *.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">


<mapper namespace="User">

	<resultMap id="UserResult" type="com.mylink.mipbs.model.User">
              <result property="id" column="ID_" />				
              <result property="name" column="NAME_" />				
              <result property="password" column="PASSWORD_" />				
              <result property="realname" column="REALNAME_" />				
              <result property="sex" column="SEX_" />				
              <result property="company" column="COMPANY_" />				
              <result property="worktel" column="WORKTEL_" />				
              <result property="mobiletel1" column="MOBILETEL1_" />				
              <result property="mobiletel2" column="MOBILETEL2_" />				
              <result property="fax" column="FAX_" />				
              <result property="birthday" column="BIRTHDAY_" />				
              <result property="email" column="EMAIL_" />				
              <result property="address" column="ADDRESS_" />				
              <result property="postalcode" column="POSTALCODE_" />				
              <result property="idcard" column="IDCARD_" />				
              <result property="blocked" column="BLOCKED_" />				
              <result property="regdate" column="REGDATE_" />				
              <result property="lastloginDate" column="LASTLOGIN_DATE_" />				
              <result property="logincount" column="LOGINCOUNT_" />				
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="userColumns">
	    <![CDATA[
        	ID_ ,
        	NAME_ ,
        	PASSWORD_ ,
        	REALNAME_ ,
        	SEX_ ,
        	COMPANY_ ,
        	WORKTEL_ ,
        	MOBILETEL1_ ,
        	MOBILETEL2_ ,
        	FAX_ ,
        	BIRTHDAY_ ,
        	EMAIL_ ,
        	ADDRESS_ ,
        	POSTALCODE_ ,
        	IDCARD_ ,
        	BLOCKED_ ,
        	REGDATE_ ,
        	LASTLOGIN_DATE_ ,
        	LOGINCOUNT_ 
	    ]]>
	</sql>

	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" parameterType="com.mylink.mipbs.model.User" >
	

	<selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE">
              select SEQG_USER_INFO_ID.nextval as id from dual
    </selectKey>
	
    
    <![CDATA[
        INSERT INTO
        TG_USER_INFO (
        	ID_ ,
        	NAME_ ,
        	PASSWORD_ ,
        	REALNAME_ ,
        	SEX_ ,
        	COMPANY_ ,
        	WORKTEL_ ,
        	MOBILETEL1_ ,
        	MOBILETEL2_ ,
        	FAX_ ,
        	BIRTHDAY_ ,
        	EMAIL_ ,
        	ADDRESS_ ,
        	POSTALCODE_ ,
        	IDCARD_ ,
        	BLOCKED_ ,
        	REGDATE_ ,
        	LASTLOGIN_DATE_ ,
        	LOGINCOUNT_ 
        ) VALUES (
        	#{id,jdbcType=DECIMAL} ,
        	#{name,jdbcType=VARCHAR} ,
        	#{password,jdbcType=VARCHAR} ,
        	#{realname,jdbcType=VARCHAR} ,
        	#{sex,jdbcType=VARCHAR} ,
        	#{company,jdbcType=VARCHAR} ,
        	#{worktel,jdbcType=VARCHAR} ,
        	#{mobiletel1,jdbcType=VARCHAR} ,
        	#{mobiletel2,jdbcType=VARCHAR} ,
        	#{fax,jdbcType=VARCHAR} ,
        	#{birthday,jdbcType=DATE} ,
        	#{email,jdbcType=VARCHAR} ,
        	#{address,jdbcType=VARCHAR} ,
        	#{postalcode,jdbcType=VARCHAR} ,
        	#{idcard,jdbcType=VARCHAR} ,
        	#{blocked,jdbcType=DECIMAL} ,
        	#{regdate,jdbcType=DATE} ,
        	#{lastloginDate,jdbcType=DATE} ,
        	#{logincount,jdbcType=DECIMAL} 
        )
    ]]>
		<!--	
			oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL 
			DB2: order="BEFORE"" values nextval for sequenceName
		<selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">
			SELECT sequenceName.nextval AS ID FROM DUAL 
        </selectKey>
		-->
	</insert>
    
	<update id="update" parameterType="com.mylink.mipbs.model.User">
    <![CDATA[
        UPDATE TG_USER_INFO SET
	        NAME_ = #{name,jdbcType=VARCHAR} ,
	        PASSWORD_ = #{password,jdbcType=VARCHAR} ,
	        REALNAME_ = #{realname,jdbcType=VARCHAR} ,
	        SEX_ = #{sex,jdbcType=VARCHAR} ,
	        COMPANY_ = #{company,jdbcType=VARCHAR} ,
	        WORKTEL_ = #{worktel,jdbcType=VARCHAR} ,
	        MOBILETEL1_ = #{mobiletel1,jdbcType=VARCHAR} ,
	        MOBILETEL2_ = #{mobiletel2,jdbcType=VARCHAR} ,
	        FAX_ = #{fax,jdbcType=VARCHAR} ,
	        BIRTHDAY_ = #{birthday,jdbcType=DATE} ,
	        EMAIL_ = #{email,jdbcType=VARCHAR} ,
	        ADDRESS_ = #{address,jdbcType=VARCHAR} ,
	        POSTALCODE_ = #{postalcode,jdbcType=VARCHAR} ,
	        IDCARD_ = #{idcard,jdbcType=VARCHAR} ,
	        BLOCKED_ = #{blocked,jdbcType=DECIMAL} ,
	        REGDATE_ = #{regdate,jdbcType=DATE} ,
	        LASTLOGIN_DATE_ = #{lastloginDate,jdbcType=DATE} ,
	        LOGINCOUNT_ = #{logincount,jdbcType=DECIMAL} 
        WHERE 
	        ID_ = #{id} 
    ]]>
	</update>

    <delete id="delete" parameterType="java.lang.Long">
    <![CDATA[
        delete from TG_USER_INFO where
        ID_ = #{id} 
    ]]>
    </delete>
    
    <select id="getById" parameterType="java.lang.Long" resultMap="UserResult">
		select <include refid="userColumns" />
	    <![CDATA[
		    from TG_USER_INFO 
	        where 
		        ID_ = #{id} 
	    ]]>
	</select>
	
	<sql id="userDynamicWhere">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用,具体请查看Ognl类 -->
		<where>
	       <if test="@Ognl@isNotEmpty(id)">
				and ID_ = #{id}
			</if>
	       <if test="@Ognl@isNotEmpty(name)">
				and NAME_ = #{name}
			</if>
	       <if test="@Ognl@isNotEmpty(password)">
				and PASSWORD_ = #{password}
			</if>
	       <if test="@Ognl@isNotEmpty(realname)">
				and REALNAME_ = #{realname}
			</if>
	       <if test="@Ognl@isNotEmpty(sex)">
				and SEX_ = #{sex}
			</if>
	       <if test="@Ognl@isNotEmpty(company)">
				and COMPANY_ = #{company}
			</if>
	       <if test="@Ognl@isNotEmpty(worktel)">
				and WORKTEL_ = #{worktel}
			</if>
	       <if test="@Ognl@isNotEmpty(mobiletel1)">
				and MOBILETEL1_ = #{mobiletel1}
			</if>
	       <if test="@Ognl@isNotEmpty(mobiletel2)">
				and MOBILETEL2_ = #{mobiletel2}
			</if>
	       <if test="@Ognl@isNotEmpty(fax)">
				and FAX_ = #{fax}
			</if>
	       <if test="@Ognl@isNotEmpty(birthday)">
				and BIRTHDAY_ = #{birthday}
			</if>
	       <if test="@Ognl@isNotEmpty(email)">
				and EMAIL_ = #{email}
			</if>
	       <if test="@Ognl@isNotEmpty(address)">
				and ADDRESS_ = #{address}
			</if>
	       <if test="@Ognl@isNotEmpty(postalcode)">
				and POSTALCODE_ = #{postalcode}
			</if>
	       <if test="@Ognl@isNotEmpty(idcard)">
				and IDCARD_ = #{idcard}
			</if>
	       <if test="@Ognl@isNotEmpty(blocked)">
				and BLOCKED_ = #{blocked}
			</if>
	       <if test="@Ognl@isNotEmpty(regdate)">
				and REGDATE_ = #{regdate}
			</if>
	       <if test="@Ognl@isNotEmpty(lastloginDate)">
				and LASTLOGIN_DATE_ = #{lastloginDate}
			</if>
	       <if test="@Ognl@isNotEmpty(logincount)">
				and LOGINCOUNT_ = #{logincount}
			</if>
		</where>
	</sql>
		
    <select id="count" resultType="long">
        select count(*) from TG_USER_INFO 
		<include refid="userDynamicWhere"/>    
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="pageSelect" resultMap="UserResult">
    	select <include refid="userColumns" />
	    from TG_USER_INFO 
		<include refid="userDynamicWhere"/>
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>

	
</mapper>



数据类当然,基本跟以前差不多:
public class User extends BaseEntity {
	
	//alias
	public static final String TABLE_ALIAS = "User";
	public static final String ALIAS_ID_ = "用户ID";
	public static final String ALIAS_NAME_ = "账号";
	public static final String ALIAS_PASSWORD_ = "密码";
	public static final String ALIAS_REALNAME_ = "真实姓名";
	public static final String ALIAS_SEX_ = "性别";
	public static final String ALIAS_COMPANY_ = "所属公司";
	public static final String ALIAS_WORKTEL_ = "工作电话";
	public static final String ALIAS_MOBILETEL1_ = "手机号1";
	public static final String ALIAS_MOBILETEL2_ = "手机号2";
	public static final String ALIAS_FAX_ = "传真";
	public static final String ALIAS_BIRTHDAY_ = "生日";
	public static final String ALIAS_EMAIL_ = "电子邮件";
	public static final String ALIAS_ADDRESS_ = "家庭住址";
	public static final String ALIAS_POSTALCODE_ = "邮编";
	public static final String ALIAS_IDCARD_ = "身份证";
	public static final String ALIAS_BLOCKED_ = "禁用";
	public static final String ALIAS_REGDATE_ = "开通时间";
	public static final String ALIAS_LASTLOGIN_DATE_ = "最后登录时间";
	public static final String ALIAS_LOGINCOUNT_ = "登录次数";
	
	//date formats
	public static final String FORMAT_BIRTHDAY_ = DATE_TIME_FORMAT;
	public static final String FORMAT_REGDATE_ = DATE_TIME_FORMAT;
	public static final String FORMAT_LASTLOGIN_DATE_ = DATE_TIME_FORMAT;
	
	//columns START
	private java.lang.Long id;
	private java.lang.String name;
	private java.lang.String password;
	private java.lang.String realname;
	private java.lang.String sex;
	private java.lang.String company;
	private java.lang.String worktel;
	private java.lang.String mobiletel1;
	private java.lang.String mobiletel2;
	private java.lang.String fax;
	private java.sql.Date birthday;
	private java.lang.String email;
	private java.lang.String address;
	private java.lang.String postalcode;
	private java.lang.String idcard;
	private java.lang.Boolean blocked;
	private java.sql.Date regdate;
	private java.sql.Date lastloginDate;
	private java.lang.Integer logincount;
	//columns END

	public User(){
	}

	public User(
		java.lang.Long id
	){
		this.id = id;
	}

	public void setId(java.lang.Long value) {
		this.id = value;
	}
	
	public java.lang.Long getId() {
		return this.id;
	}
	public void setName(java.lang.String value) {
		this.name = value;
	}
	
	public java.lang.String getName() {
		return this.name;
	}
	public void setPassword(java.lang.String value) {
		this.password = value;
	}
	
	public java.lang.String getPassword() {
		return this.password;
	}
	public void setRealname(java.lang.String value) {
		this.realname = value;
	}
	
	public java.lang.String getRealname() {
		return this.realname;
	}
	public void setSex(java.lang.String value) {
		this.sex = value;
	}
	
	public java.lang.String getSex() {
		return this.sex;
	}
	public void setCompany(java.lang.String value) {
		this.company = value;
	}
	
	public java.lang.String getCompany() {
		return this.company;
	}
	public void setWorktel(java.lang.String value) {
		this.worktel = value;
	}
	
	public java.lang.String getWorktel() {
		return this.worktel;
	}
	public void setMobiletel1(java.lang.String value) {
		this.mobiletel1 = value;
	}
	
	public java.lang.String getMobiletel1() {
		return this.mobiletel1;
	}
	public void setMobiletel2(java.lang.String value) {
		this.mobiletel2 = value;
	}
	
	public java.lang.String getMobiletel2() {
		return this.mobiletel2;
	}
	public void setFax(java.lang.String value) {
		this.fax = value;
	}
	
	public java.lang.String getFax() {
		return this.fax;
	}
	public String getBirthdayString() {
		return date2String(getBirthday(), FORMAT_BIRTHDAY_);
	}
	public void setBirthdayString(String value) {
		setBirthday(string2Date(value, FORMAT_BIRTHDAY_,java.sql.Date.class));
	}
	
	public void setBirthday(java.sql.Date value) {
		this.birthday = value;
	}
	
	public java.sql.Date getBirthday() {
		return this.birthday;
	}
	public void setEmail(java.lang.String value) {
		this.email = value;
	}
	
	public java.lang.String getEmail() {
		return this.email;
	}
	public void setAddress(java.lang.String value) {
		this.address = value;
	}
	
	public java.lang.String getAddress() {
		return this.address;
	}
	public void setPostalcode(java.lang.String value) {
		this.postalcode = value;
	}
	
	public java.lang.String getPostalcode() {
		return this.postalcode;
	}
	public void setIdcard(java.lang.String value) {
		this.idcard = value;
	}
	
	public java.lang.String getIdcard() {
		return this.idcard;
	}
	public void setBlocked(java.lang.Boolean value) {
		this.blocked = value;
	}
	
	public java.lang.Boolean getBlocked() {
		return this.blocked;
	}
	public String getRegdateString() {
		return date2String(getRegdate(), FORMAT_REGDATE_);
	}
	public void setRegdateString(String value) {
		setRegdate(string2Date(value, FORMAT_REGDATE_,java.sql.Date.class));
	}
	
	public void setRegdate(java.sql.Date value) {
		this.regdate = value;
	}
	
	public java.sql.Date getRegdate() {
		return this.regdate;
	}
	public String getLastloginDateString() {
		return date2String(getLastloginDate(), FORMAT_LASTLOGIN_DATE_);
	}
	public void setLastloginDateString(String value) {
		setLastloginDate(string2Date(value, FORMAT_LASTLOGIN_DATE_,java.sql.Date.class));
	}
	
	public void setLastloginDate(java.sql.Date value) {
		this.lastloginDate = value;
	}
	
	public java.sql.Date getLastloginDate() {
		return this.lastloginDate;
	}
	public void setLogincount(java.lang.Integer value) {
		this.logincount = value;
	}
	
	public java.lang.Integer getLogincount() {
		return this.logincount;
	}

	public String toString() {
		return new ToStringBuilder(this)
			.append("Id",getId())
			.append("Name",getName())
			.append("Password",getPassword())
			.append("Realname",getRealname())
			.append("Sex",getSex())
			.append("Company",getCompany())
			.append("Worktel",getWorktel())
			.append("Mobiletel1",getMobiletel1())
			.append("Mobiletel2",getMobiletel2())
			.append("Fax",getFax())
			.append("Birthday",getBirthday())
			.append("Email",getEmail())
			.append("Address",getAddress())
			.append("Postalcode",getPostalcode())
			.append("Idcard",getIdcard())
			.append("Blocked",getBlocked())
			.append("Regdate",getRegdate())
			.append("LastloginDate",getLastloginDate())
			.append("Logincount",getLogincount())
			.toString();
	}
	
	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.append(getName())
			.append(getPassword())
			.append(getRealname())
			.append(getSex())
			.append(getCompany())
			.append(getWorktel())
			.append(getMobiletel1())
			.append(getMobiletel2())
			.append(getFax())
			.append(getBirthday())
			.append(getEmail())
			.append(getAddress())
			.append(getPostalcode())
			.append(getIdcard())
			.append(getBlocked())
			.append(getRegdate())
			.append(getLastloginDate())
			.append(getLogincount())
			.toHashCode();
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof User == false) return false;
		if(this == obj) return true;
		User other = (User)obj;
		return new EqualsBuilder()
			.append(getId(),other.getId())
			.append(getName(),other.getName())
			.append(getPassword(),other.getPassword())
			.append(getRealname(),other.getRealname())
			.append(getSex(),other.getSex())
			.append(getCompany(),other.getCompany())
			.append(getWorktel(),other.getWorktel())
			.append(getMobiletel1(),other.getMobiletel1())
			.append(getMobiletel2(),other.getMobiletel2())
			.append(getFax(),other.getFax())
			.append(getBirthday(),other.getBirthday())
			.append(getEmail(),other.getEmail())
			.append(getAddress(),other.getAddress())
			.append(getPostalcode(),other.getPostalcode())
			.append(getIdcard(),other.getIdcard())
			.append(getBlocked(),other.getBlocked())
			.append(getRegdate(),other.getRegdate())
			.append(getLastloginDate(),other.getLastloginDate())
			.append(getLogincount(),other.getLogincount())
			.isEquals();
	}
}



DAO类:
@Component
public class UserDao extends BaseIbatis3Dao<User,java.lang.Long>{

	public Class getEntityClass() {
		return User.class;
	}
	
	public void saveOrUpdate(User entity) {
		if(entity.getId() == null) 
			save(entity);
		else 
			update(entity);
	}
	
	public Page findByPageRequest(PageRequest pageRequest) {
		return pageQuery("User.pageSelect",pageRequest);
	}
	

}




接口定义:
public interface UserManager {
	
	User getById(java.lang.Long id);
	
	void save(User u);
	
	void update(User u);
	
	void removeById (java.lang.Long id);
	
	Page findByPageRequest(PageRequest<Map> q);

}



ibatis实现功能类:
@Component("userManager")
@Transactional
public class UserManagerImpl extends BaseManager<User,java.lang.Long> 
					implements UserManager {

	private UserDao userDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性*/
	public void setUserDao(UserDao dao) {
		this.userDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.userDao;
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return userDao.findByPageRequest(pr);
	}
	
}



外部调用类:
public class UserAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/User/query.jsp";
	protected static final String LIST_JSP= "/pages/User/list.jsp";
	protected static final String CREATE_JSP = "/pages/User/create.jsp";
	protected static final String EDIT_JSP = "/pages/User/edit.jsp";
	protected static final String SHOW_JSP = "/pages/User/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/User/list.do";
	
	
	private UserManager userManager;
	
	private User user;
	java.lang.Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			user = new User();
		} else {
			user = (User)userManager.getById(id);
		}
	}
	
	/** 通过spring自动注入 */
	public void setUserManager(UserManager manager) {
		this.userManager = manager;
	}	
	
	public Object getModel() {
		return user;
	}
	
	public void setId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void extlist() throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(getRequest(), DEFAULT_SORT_COLUMNS);
		Page page = userManager.findByPageRequest(pr);
		
		List<User> Userlist = (List) page.getResult();
		ListRange<User> resultList = new ListRange<User>();
		resultList.setList(Userlist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(resultList);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void extsave() throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			userManager.save(user);
			result.put("success", true);
			result.put("msg", "添 加 成 功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void extupdate() throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			userManager.update(user);
			result.put("success", true);
			result.put("msg", "修 改 成 功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void extdelete() throws IOException
	{
		String ids = getRequest().getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Long id = new java.lang.Long((String)idarray[i]);
				userManager.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}
	
}



测试用例类:
public class UserManagerTest extends BaseManagerTestCase{

	
	private UserManagerImpl manager;
	
	
	@Autowired
	public void setUserManager(UserManagerImpl manager) {
		this.manager = manager;
	}

	@Override
	protected String[] getDbUnitDataFiles() {
		return new String[]{"classpath:common_testdata.xml","classpath:User_testdata.xml"};
	}

	@Test
	public void crud() {
		User obj = new User();
		
	  	obj.setName(new java.lang.String("1"));
	  	obj.setPassword(new java.lang.String("1"));
	  	obj.setRealname(new java.lang.String("1"));
	  	obj.setSex(new java.lang.String("1"));
	  	obj.setCompany(new java.lang.String("1"));
	  	obj.setWorktel(new java.lang.String("1"));
	  	obj.setMobiletel1(new java.lang.String("1"));
	  	obj.setMobiletel2(new java.lang.String("1"));
	  	obj.setFax(new java.lang.String("1"));
	  	obj.setBirthday(new java.sql.Date(System.currentTimeMillis()));
	  	obj.setEmail(new java.lang.String("1"));
	  	obj.setAddress(new java.lang.String("1"));
	  	obj.setPostalcode(new java.lang.String("1"));
	  	obj.setIdcard(new java.lang.String("1"));
	  	obj.setBlocked(new java.lang.Boolean("1"));
	  	obj.setRegdate(new java.sql.Date(System.currentTimeMillis()));
	  	obj.setLastloginDate(new java.sql.Date(System.currentTimeMillis()));
	  	obj.setLogincount(new java.lang.Integer("1"));
		
		manager.save(obj);
		manager.getEntityDao().flush();
		
		manager.update(obj);
		manager.getEntityDao().flush();
		
		assertNotNull(obj.getId());
		
		manager.removeById(obj.getId());
		manager.getEntityDao().flush();
	
	}
}


表结构SQL

create table TG_USER_INFO
(
  ID_             NUMBER(10) not null,
  NAME_           VARCHAR2(100) not null,
  PASSWORD_       VARCHAR2(40) not null,
  REALNAME_       VARCHAR2(100),
  SEX_            VARCHAR2(100),
  COMPANY_        VARCHAR2(100),
  WORKTEL_        VARCHAR2(100),
  MOBILETEL1_     VARCHAR2(100),
  MOBILETEL2_     VARCHAR2(100),
  FAX_            VARCHAR2(100),
  BIRTHDAY_       DATE,
  EMAIL_          VARCHAR2(100),
  ADDRESS_        VARCHAR2(200),
  POSTALCODE_     VARCHAR2(10),
  IDCARD_         VARCHAR2(50),
  BLOCKED_        NUMBER(1),
  REGDATE_        DATE,
  LASTLOGIN_DATE_ DATE,
  LOGINCOUNT_     NUMBER(8)
)


DAO测试:

public class UserDaoTest extends BaseDaoTestCase{
	
	private UserDao dao;
	
	@Autowired
	public void setUserDao(UserDao dao) {
		this.dao = dao;
	}

	@Override
	protected String[] getDbUnitDataFiles() {
		return new String[]{"classpath:common_testdata.xml","classpath:User_testdata.xml"};
	}
	
	@Test
	public void findByPageRequest() {
		int pageNumber = 1;
		int pageSize = 10;
		
		PageRequest<Map> pageRequest = new PageRequest(new HashMap());
		pageRequest.setPageNumber(pageNumber);
		pageRequest.setPageSize(pageSize);
		pageRequest.setSortColumns(null);
		
		pageRequest.getFilters().put("name", "1");
		pageRequest.getFilters().put("password", "1");
		pageRequest.getFilters().put("realname", "1");
		pageRequest.getFilters().put("sex", "1");
		pageRequest.getFilters().put("company", "1");
		pageRequest.getFilters().put("worktel", "1");
		pageRequest.getFilters().put("mobiletel1", "1");
		pageRequest.getFilters().put("mobiletel2", "1");
		pageRequest.getFilters().put("fax", "1");
		pageRequest.getFilters().put("birthday", "1");
		pageRequest.getFilters().put("email", "1");
		pageRequest.getFilters().put("address", "1");
		pageRequest.getFilters().put("postalcode", "1");
		pageRequest.getFilters().put("idcard", "1");
		pageRequest.getFilters().put("blocked", "1");
		pageRequest.getFilters().put("regdate", "1");
		pageRequest.getFilters().put("lastloginDate", "1");
		pageRequest.getFilters().put("logincount", "1");
		
		Page page = dao.findByPageRequest(pageRequest);
		
		assertEquals(pageNumber,page.getThisPageNumber());
		assertEquals(pageSize,page.getPageSize());
		List resultList = (List)page.getResult();
		assertNotNull(resultList);
		
	}
	
}




写的比较简单,希望对大家有点用。呵呵。