MP-CRUD

我们要建立一个实体类来对应数据库中的信息

@Getter
@Setter
@ToString
/**
 * @TableName("user") 将当前的实体类和数据库的表建立联系
 * 注解参数:表名
 */
@TableName("user")
public class User implements Serializable {
    /**
     * 主键属性  @TableId
     *
     * value 该属性对应的数据库表中的字段名
     * type 主键自增的类型 AUTO 代表自动递增
     */
    private long id;  //用户id
    /**
     * 非主键属性  @TableField
     *  @TableField("username")  参数为该属性对应的数据库表中的字段名
     *
     */
    private String name;//用户姓名
    private int age;    //用户年龄
    private long grade; //用户毕业年限
    private String province;  //用户所在省份
    private String city;    //用户所在城市
    private int score;    //高考分数
    private String mbtiTestResult;//MBTI测试结果
    private String holandTestResult;//霍兰德测试结果
    private String school;//高中所在学校
    private String phone;//手机号
    private String password;//密码
    public User() {
    }

    public User(long id, String name, int age, long grade, String province, String city, int score, String mbtiTestResult, String holandTestResult, String school, String phone, String password) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.grade = grade;
        this.province = province;
        this.city = city;
        this.score = score;
        this.mbtiTestResult = mbtiTestResult;
        this.holandTestResult = holandTestResult;
        this.school = school;
        this.phone = phone;
        this.password = password;
    }

然后为这个类再建立能操作数据的方法类Mapper

@Mapper
public interface IUserMapper extends BaseMapper<User> {

}

测试

···
@SpringBootTest
class ZytbDemoApplicationTests {
@Autowired
private IUserMapper userMapper;
@Autowired
private IUserService userService;
@Test
void contextLoads() {
}
@Test
public void testInsert(){
User User = new User();
User.setName("东方不败");
User.setAge(56);
User.setGrade(1998);
User.setProvince("广东");
User.setCity("dahai");
User.setScore(1);
User.setHolandTestResult("good");
User.setMbtiTestResult("不错");
User.setSchool("jialidun");
userMapper.insert(User);
//mybatisplus会自动把当前插入对象在数据库中的id写回到该实体中
System.out.println(User.getId());
}
@Test
public void test(){
User user= userService.findUserByphone("123456789");
System.out.println(user);
}
}
···

https://www.cnblogs.com/zhaoyunlong/p/10841905.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。