Mybatis-Plus 是一款 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。Mybatis-Plus 适用于 Mybatis 的大部分场景,不需要对 Mybatis 有深入的了解即可使用。
@TableName("user")
public class User {
// ...
}
@TableName("user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
// ...
}
@TableName("user")
public class User {
// ...
@TableField("name")
private String name;
// ...
}
@TableName("user")
public class User {
// ...
@TableLogic
private Integer deleted;
// ...
}
@TableName("user")
public class User {
// ...
@Version
private Integer version;
// ...
}
public enum Gender {
MALE(1, "男"),
FEMALE(2, "女");
@EnumValue
private Integer value;
// ...
}
@TableName("user")
public class User {
@KeySequence("SEQ_USER")
private Long id;
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Insert("INSERT INTO user (name, age) VALUES (#{name}, #{age})")
int insertUser(User user);
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}")
int updateUser(User user);
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Delete("DELETE FROM user WHERE id = #{id}")
int deleteUser(Long id);
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM user WHERE id = #{id}")
User selectUserById(Long id);
// ...
}
@TableName("user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
@TableLogic
private Integer deleted;
@Version
private Integer version;
// ...
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
// ...
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public void addUser(User user) {
userMapper.insert(user);
}
public void deleteUser(Long id) {
userMapper.deleteById(id);
}
public void updateUser(User user) {
userMapper.updateById(user);
}
public User getUser(Long id) {
return userMapper.selectById(id);
}
// ...
}
通过本教程,我们了解了 Mybatis-Plus 的常用注解及其使用方法。Mybatis-Plus 通过这些注解,提供了丰富的功能和便捷的操作,能够帮助开发者简化代码,提高开发效率。在实际开发中,我们可以充分利用 Mybatis-Plus 提供的功能,让数据库操作变得更加简单和高效。