Data entity operations
The above describes a series of database operations based on the ISession session object. The operation process introduced next is simpler and more direct, completely based on the data entity object;
Note: This section describes The data entity object referred to must provide the BaseEntity abstract class through the inheritance framework;
Insert
User _user = new User(); _user.setId(UUIDUtils.UUID()); _user.setUsername("suninformation"); _user.setPwd(DigestUtils.md5Hex("123456")); _user.setAge(20); _user.setSex("F"); // 执行数据插入 _user.save(); // 或者在插入时也可以指定/排除某些字段 _user.save(Fields.create(User.FIELDS.SEX, User.FIELDS.AGE).excluded(true)); // 或者插入前判断记录是否已存在,若已存在则执行记录更新操作 _user.saveOrUpdate(); // 或者执行记录更新操作时仅更新指定的字段 _user.saveOrUpdate(Fields.create(User.FIELDS.SEX, User.FIELDS.AGE));
Update
User _user = new User(); _user.setId("bc19f5645aa9438089c5e9954e5f1ac5"); _user.setPwd(DigestUtils.md5Hex("654321")); _user.setAge(20); _user.setSex("F"); // 执行记录更新 _user.update(); // 或者仅更新指定的字段 _user.update(Fields.create(User.FIELDS.SEX, User.FIELDS.AGE));
##Query(Find)
- Load based on record ID:
User _user = new User(); _user.setId("bc19f5645aa9438089c5e9954e5f1ac5"); // 根据记录ID加载全部字段 _user = _user.load(); // 或者根据记录ID加载指定的字段 _user = _user.load(Fields.create(User.FIELDS.USER_NAME, User.FIELDS.SEX, User.FIELDS.AGE));
- Set conditions through data entities (non-empty attributes will be connected using and conditions) , query all records that meet the conditions;
User _user = new User(); _user.setUsername("suninformation"); _user.setPwd(DigestUtils.md5Hex("123456")); // 返回所有字段 IResultSet<User> _users = _user.find(); // 或者返回指定的字段 _users = _user.find(Fields.create(User.FIELDS.ID, User.FIELDS.AGE)); // 或者分页查询 _users = _user.find(Page.create(1).pageSize(10));
##Page query: User _user = new User(); _user.setSex("F"); // 分页查询,返回全部字段 IResultSet<User> _users = _user.find(Page.create(1).pageSize(10)); // 或者分页查询,返回指定的字段 _users = _user.find(Fields.create(User.FIELDS.ID, User.FIELDS.AGE), Page.create(1).pageSize(10));
Return only the first record that meets the conditions (FindFirst): User _user = new User(); _user.setUsername("suninformation"); _user.setPwd(DigestUtils.md5Hex("123456")); // 返回与用户名称和密码匹配的第一条记录 _user = _user.findFirst(); // 或者返回与用户名称和密码匹配的第一条记录的ID和AGE字段 _user = _user.findFirst(Fields.create(User.FIELDS.ID, User.FIELDS.AGE));
Delete
User _user = new User(); _user.setId("bc19f5645aa9438089c5e9954e5f1ac5"); // 根据实体主键删除记录 _user.delete();
Note
: The two database operation methods introduced above have their own characteristics, please follow Choose a more suitable method according to the actual situation, or you can mix it;