严格来说,本文讲述的只是oracle的内容,但因为1Mpper类里写的SQL,故而就和MyBatis挨上了。

表定义:

create table mc_user(
    id number(8),
    deleted number(1) default 0,
    create_time timestamp default sysdate,
    create_uid number(8),
    update_time timestamp default sysdate,
    update_uid number(8),
    name nvarchar2(128),
    pswd nvarchar2(32),
    email nvarchar2(32),
    primary key(id)
);

序列定义:

CREATE SEQUENCE mc_user_id_sqs
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;

这个序列是专门为mc_user表的主键做的,下面我们让插入记录时让主键id自己取序列的下一个值:

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {
    @Insert("insert into mc_user(id,name,pswd,email) values(mc_user_id_sqs.nextval,#{name},#{pswd},#{email})")
    int addUser(@Param("name") String name,
                @Param("pswd") String pswd,
                @Param("email") String email);
}

以后便不会为主键赋值烦恼了。

END

相关文章:

  • 2022-12-23
  • 2021-08-10
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-23
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案