[mybatis]mybatis01自动生成主键并获取主键

一、实体类

public class TUser implements Serializable{
	
    private Integer id;

    private String userName;

    private String realName;

    private Byte sex;

    private String mobile;

    private String email;

    private String note;

    private TPosition position;
    
    private List jobs ;
    
    private List healthReports;

    
    private List roles;
/*    
	public TUser(Integer id, String userName) {
		super();
		this.id = id;
		this.userName = userName;
	}
	*/
    

    
  
	@Override
	public String toString() {
		String positionId=  (position == null ? "" : String.valueOf(position.getId()));
		return "TUser [id=" + id + ", userName=" + userName + ", realName="
				+ realName + ", sex=" + sex + ", mobile=" + mobile + ", email="
				+ email + ", note=" + note + ", positionId=" + positionId + "]";
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public Byte getSex() {
		return sex;
	}

	public void setSex(Byte sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public TPosition getPosition() {
		return position;
	}

	public void setPosition(TPosition position) {
		this.position = position;
	}

	public List getJobs() {
		return jobs;
	}

	public void setJobs(List jobs) {
		this.jobs = jobs;
	}

	public List getHealthReports() {
		return healthReports;
	}

	public void setHealthReports(List healthReports) {
		this.healthReports = healthReports;
	}

	public List getRoles() {
		return roles;
	}

	public void setRoles(List roles) {
		this.roles = roles;
	}

	public static void main(String[] args) {
		System.out.println(1<<2);
	}

	
	
    
}

二、mapper

public interface TUserMapper {
	
	    int insert1(TUser record);
	    int insert2(TUser record);
    
}

三、mapper.xml

方式一(推荐):useGeneratedKeys="true",keyProperty="id",keyProperty这里是主键ID,有可能id需要替换。





	
		insert into t_user (id, userName, realName,
		sex, mobile,
		email,
		note, position_id)
		values (#{id,jdbcType=INTEGER},
		#{userName,jdbcType=VARCHAR},
		#{realName,jdbcType=VARCHAR},
		#{sex,jdbcType=TINYINT}, #{mobile,jdbcType=VARCHAR},
		#{email,jdbcType=VARCHAR},
		#{note,jdbcType=VARCHAR},
		#{position.id,jdbcType=INTEGER})
	

方式二(不推荐):selectKey,mysql的order必须设置为AFTER



		
			select
			LAST_INSERT_ID()
		
		insert into t_user (id, userName, realName,
		sex, mobile,
		email,
		note,
		position_id)
		values (#{id,jdbcType=INTEGER},
		#{userName,jdbcType=VARCHAR},
		#{realName,jdbcType=VARCHAR},
		#{sex,jdbcType=TINYINT}, #{mobile,jdbcType=VARCHAR},
		#{email,jdbcType=VARCHAR},
		#{note,jdbcType=VARCHAR},
		#{position.id,jdbcType=INTEGER})
	

你可能感兴趣的:(Mysql,mybatis,java,mysql)