ActiveRecord 模式
直接调用Model对象来进行增删改查。
实体类只需继承 Model 类即可进行强大的 CRUD 操作
需要项目中已注入对应实体的BaseMapper
实体类User
点击查看代码
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
@Data
public class User extends Model<User> {
private Long id;
private String name;
private Integer age;
private String email;
}
Mapper接口
点击查看代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplusdemo.sample.model.User;
import java.util.List;
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
}
测试
点击查看代码
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
System.out.println(("----- selectAll method test ------"));
//List<User> userList = userMapper.findAll();
User user = new User();
List<User> userList = user.selectAll();
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
测试结果
----- selectAll method test ------
User(id=1, name=Jone, age=18, [email protected])
User(id=2, name=Jack, age=20, [email protected])
User(id=3, name=Tom, age=28, [email protected])
User(id=4, name=Sandy, age=21, [email protected])
User(id=5, name=Billie, age=24, [email protected])
但是不能使用findAll方法
标签:mybatisplus,name,ActiveRecord,age,模式,baomidou,User,com,email From: https://www.cnblogs.com/shigongp/p/16603156.html