mybatis 使用selectkey 注解 应用

最近一直被mybatis 注解 selectkey 所迷惑,insert后的回的key并不是在原对象中返回 而是新new 了一个对象返回,经过多次尝试 成功了 和大家分享一下

/**
* @file_name PRODUCTLISTMapper.java
* @date 2012-5-20 下午10:59:29
*/
package com.work.company.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import com.work.company.entity.ProductEntry;

/**
* @author chedejun
* @Email [email protected]
* @date 2012-5-20 下午10:59:29
*/
@Repository
interface IProductListMapper {

/**
*
*
* 添加
*
* @param PRODUCTLIST
* @return KEY
*/
@Insert("INSERT INTO PRODUCTLIST(PRODUCTNAME,IMAGEURL,DESCRIBES,CATALOGID) "
+ "VALUES(#{PRODUCT.productName},#{PRODUCT.imageUrl},"
+ "#{PRODUCT.describes},#{PRODUCT.catalogId})")
@SelectKey(before=false,keyProperty="PRODUCT.id",resultType=Integer.class,statementType=StatementType.STATEMENT,statement="SELECT LAST_INSERT_ID() AS id")
int insertProduct(final @Param("PRODUCT")ProductEntry product);

/**
*
* 删除
*
* @param id
* @return effect rows
*/
@Delete("DELETE FROM PRODUCTLIST WHERE ID=#{ID}")
int delProduct(@Param("ID") final int id);

/**
*
*
* @param productID
* @param seriesId
* @return List
*/

@SelectProvider(type = ProductProvider.class, method = "getProductSql")
List selProductListById(
@Param("ID") final Integer id,
@Param("CATALOGID") final Integer catalogId);
/**
*
*
*@param PRODUCTLIST
*@return effect row
*/
@Update("UPDATE PRODUCTLIST SET PRODUCTNAME=#{PRODUCT.productName},IMAGEURL=#{PRODUCT.imageUrl}," +
"DESCRIBES=#{PRODUCT.describes},CATALOGID=#{PRODUCT.catalogId} " +
"WHERE ID=#{PRODUCT.id}")
int updateProduct(@Param("PRODUCT") final ProductEntry product);

}


测试

/**
*
*
* 添加
*
* @param productDetail
* @return KEY
*/
@Test
public void insertProductDetail(){
final CatalogEntry catalog=new CatalogEntry();
catalog.setCatalogImage("adfadsf");
catalog.setCatalogName("aaaaa");
final ProductEntry productDetail=new ProductEntry();
productListMapper.insertCatalog(catalog);
productDetail.setCatalogId(catalog.getId());
productDetailMapper.insertProduct(productDetail);
System.out.println(productDetail.getId());
}


另一种方式 官方帮助文档上基于xml



PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">




SELECT LAST_INSERT_ID() AS id

INSERT INTO CATALOGLIST(CATALOGNAME,CATALOGIMAGE)
VALUES
(#{catalogName},#{catalogImage})





/**
* @file_name IProductListMapper.java
* @date 2012-5-20 下午8:27:53
*/
package com.work.company.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import com.work.company.entity.CatalogEntry;

/**
* @author chedejun
* @Email [email protected]
* @date 2012-5-20 下午8:27:53
*/
@Repository
interface ICatalogListMapper {


/**
* 获取产品系列
*
* @param id
* 系列编号
*
* @return List
*/
@SelectProvider(type = ProductProvider.class,method = "getCatalogSql")
List selCatalogList(@Param("ID") final Integer id);

/**
* 添加系列
*
* @param productList
* @return key
*
*/
int insertCatalog(final CatalogEntry catalog);

//@Insert("INSERT INTO CATALOGLIST(CATALOGNAME,CATALOGIMAGE) VALUES"
// + " (#{CATALOG.catalogName},#{CATALOG.catalogImage})")
//@SelectKey(before=false,keyProperty="id",statementType=StatementType.STATEMENT, resultType = Integer.class, statement = "SELECT LAST_INSERT_ID() AS id" )





/**
* 删除系列
*
* @param id
* key
* @return 影响行数
*/
@Delete("DELETE FROM CATALOGLIST WHERE ID=#{ID}")
int delCatalog(@Param("ID") final Integer id);

/**
* 更新
*
* @param productList
* @return 影响行数
*/
@Update("UPDATE CATALOGLIST SET CATALOGNAME=#{CATALOG.catalogName},CATALOGIMAGE=#{CATALOG.catalogImage} "
+ "WHERE ID=#{CATALOG.id}")
int updateCatalog(@Param("CATALOG") final CatalogEntry catalog);
}



我错误的原因 在于 @Parm 造成的

你可能感兴趣的:(mybatis)