- 通过LAST_INSERT_ID()获取刚插入记录的自增主键值,在insert语句执行后,执行select LAST_INSERT_ID()就可以获取自增主键。
mysql:
select LAST_INSERT_ID()
INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
oracle:
实现思路:
- 先查询序列得到主键,将主键设置到user对象中,将user对象插入数据库。
select 序列.nextval() from dual
INSERT INTO USER(id,username,birthday,sex,address) VALUES( 序列.nextval(),#{username},#{birthday},#{sex},#{address})
4、在mapper中如何传递多个参数?
在mapper中如何传递多个参数?
第一种:使用占位符的思想
-
在映射文件中使用#{0},#{1}代表传递进来的第几个参数
-
**使用@param注解:来命名参数 **
-
#{0},#{1}方式
//对应的xml,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
<select id="selectUser"resultMap=“BaseResultMap”>
select * fromuser_user_t whereuser_name = #{0} anduser_area=#{1}
- @param注解方式
public interface usermapper {
user selectuser(@param(“username”) string username,
@param(“hashedpassword”) string hashedpassword);
}
select id, username, hashedpassword
from some_table
where username = #{username}
and hashedpassword = #{hashedpassword}