注解
讲解了mybatisplus中使用的注解
- @TableName --表名注解
- @TableId --主键注解
- @TableField --字段注解(非主键)
- @Version --乐观锁注解、标记 @Verison 在字段上
- @EnumValue --通枚举类注解(注解在枚举字段上)
- TableLogic --表字段逻辑处理注解(逻辑删除)
- @SqlParser --租户注解,支持method上以及mapper接口上
- @KeySequence --序列主键策略 oracle
核心功能
代码生成器
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
maven依赖
<!-- mp代码生成器依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
# MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,非默认需要配置
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.1.8.RELEASE</version>
</dependency>
<!--
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
-->
代码
条件构造器
QueryWrapper
年龄范围, 姓名模糊查询用户
UserService.java
public interface UserService {
public List<User> selectUsers(String nameLike, Integer maxAge, Integer minAge);
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectUsers(String nameLike, Integer maxAge, Integer minAge) {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name", "xh");
queryWrapper.between("age", minAge, maxAge);
List<User> users = userMapper.selectList(queryWrapper);
return users;
}
}
运行访问 http://127.0.0.1:8080/user/query/xh-29-10
2020-07-20 15:58:28.263 DEBUG 3784 --- [nio-8080-exec-1] t.i.m.s.mapper.UserMapper.selectList : ==> Preparing: SELECT id,name,age,sex FROM User WHERE (name LIKE ? AND age BETWEEN ? AND ?)
2020-07-20 15:58:28.291 DEBUG 3784 --- [nio-8080-exec-1] t.i.m.s.mapper.UserMapper.selectList : ==> Parameters: %xh%(String), 10(Integer), 29(Integer)
2020-07-20 15:58:28.322 DEBUG 3784 --- [nio-8080-exec-1] t.i.m.s.mapper.UserMapper.selectList : <== Total: 3
UpdateWrapper
将所有sex=0的年龄改为18
UserService.java
public interface UserService {
public void updateMaleUsers();
...
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void updateMaleUsers() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("sex", "0");
updateWrapper.set("age", 18);
userMapper.update(null, updateWrapper);
}
}
UserController.java
@RequestMapping("/update")
public void update() {
userService.updateMaleUsers();
}
运行访问 http://127.0.0.1:8080/user/update
2020-07-20 16:09:28.761 DEBUG 12328 --- [nio-8080-exec-2] t.i.m.s.mapper.UserMapper.update : ==> Preparing: UPDATE User SET age=? WHERE (sex = ?)
2020-07-20 16:09:28.762 DEBUG 12328 --- [nio-8080-exec-2] t.i.m.s.mapper.UserMapper.update : ==> Parameters: 18(Integer), 0(String)
2020-07-20 16:09:28.775 DEBUG 12328 --- [nio-8080-exec-2] t.i.m.s.mapper.UserMapper.update : <== Updates: 2
自定义sql
如果我们需要自己定义sql, 并且想要使用mybatisplus的wapper的便捷, 那该怎么办?
UserWapper.java
@Select("select * from User ${ew.customSqlSegment}")
public List<User> selectByCondition(@Param(Constants.WRAPPER) Wrapper<User> wapper);
UserService.java
/**
* 更具age查询
* @param age
* @return
*/
public List<User> selectByAge(Integer age);
UserServiceImpl.java
@Override
public List<User> selectByAge(Integer age) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", age);
return userMapper.selectByCondition(queryWrapper);
}
UserController.java
@RequestMapping("/query/{age}")
public List<User> selectByAge(@PathVariable("age") Integer age){
List<User> users = userService.selectByAge(age);
return users;
}
运行访问 http://127.0.0.1:8080/user/query/18
2020-07-20 16:26:59.366 DEBUG 6264 --- [nio-8080-exec-1] t.i.m.s.m.UserMapper.selectByCondition : ==> Preparing: select * from User WHERE (age = ?)
2020-07-20 16:26:59.395 DEBUG 6264 --- [nio-8080-exec-1] t.i.m.s.m.UserMapper.selectByCondition : ==> Parameters: 18(Integer)
2020-07-20 16:26:59.445 DEBUG 6264 --- [nio-8080-exec-1] t.i.m.s.m.UserMapper.selectByCondition : <== Total: 2