实体类
package org.example.entity; public class User { private Integer id; private String name; private int age; private String gender; private String email; public User(Integer id, String name, int age, String gender, String email) { this.id = id; this.name = name; this.age = age; this.gender = gender; this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", email='" + email + '\'' + '}'; } }
mapper接口
public void addUser(User user);
mapper.xml
<!-- useGeneratedKeys:表示当前添加功能使用自增的主键 keyProperty:将添加的数据的自增主键为实体类类型的参数的属性赋值 --> <insert id="addUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values (null,#{name},#{age},#{gender},#{email}); </insert>
测试代码
@Test public void addUser(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(null,"xiaoming",35,"男","[email protected]"); mapper.addUser(user); System.out.println(user); sqlSession.close(); }
标签:自增,String,gender,age,public,email,mybatis,主键,name From: https://www.cnblogs.com/ixtao/p/17166244.html