e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

插入方法:

DB必须有一个主键字段,注意keyProperty处填你的实体类字段,非DB字段

selectKey为缺省可以不写,resultType返回类型必须和实体类一致

<?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="cn.xxx.mapper.PersonMapper">

    <insert id="savePerson" parameterType="cn.xxx.entity.Person" useGeneratedKeys="true" keyProperty="personId">
        <selectKey keyProperty="personId" resultType="long" order="AFTER">
            SELECT LAST_INSERT_ID() as ID
        </selectKey>
        insert into person(
            person_id,
            person_name,
            person_age
        )values(
            null,    #{personName},    #{personAge}
        )
    </insert>

</mapper>

 

获取 返回主键的方法:

//获取方式一:
PersonMapper personMapper = ctx.getBean("personMapper", PersonMapper.class);
Person person = new Person(0, "tom", "18");
personMapper.savePerson(person);
long personId = person.getPersonId();//已获取到了插入主键的值


//获取方式二: Map<String, Object> person = new HashMap<String, Object>(); person.put("personId", ...);//必须要有此键,用于接收返回值 person.put("...", ...); person.put("...", ...); personMapper.savePersonMap(person); long personId = Long.parseLong(map.get("personId").toString());//已获取到了插入主键的值

 

 

转发请注明: 转自http://www.cnblogs.com/gscq073240/articles/6904237.html

 

posted on 2017-05-25 15:48  纯黑Se丶  阅读(259)  评论(0编辑  收藏  举报