Maven依赖
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
Properties配置
server.port=8090
spring.datasource.url=jdbc:mysql:///database?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
启动类
// 启动类需要加相关注解
@SpringBootApplication
@MapperScan(basePackages = {"com.cy.pj.activity.dao"}) //扫描DAO
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
实体类
/**
* @Description 信息实体类
*/
@Data
@TableName("blacklist")//@TableName中的值对应着表名
@Accessors(chain = true)
public class UserInfoEntity {
/**
* 主键
* @TableId中可以决定主键的类型,不写会采取默认值,默认值可以在yml中配置
* AUTO: 数据库ID自增
* INPUT: 用户输入ID
* ID_WORKER: 全局唯一ID,Long类型的主键
* ID_WORKER_STR: 字符串全局唯一ID
* UUID: 全局唯一ID,UUID类型的主键
* NONE: 该类型为未设置主键类型
*/
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private String position;
private String frequency;
private String degree;
private Date time;
}
持久层
// 关键是 extends BaseMapper<UserInfoEntity>
/**
* @Description 信息DAO
*/
public interface UserInfoDao extends BaseMapper<UserInfoEntity> {
}
服务层/控制层
直接在service,controller中使用UserInfoDao即可–复杂的sql可以直接在xml里面定义对应的sql不影响
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
@Autowired
private UserInfoDao userInfoDao;
// mybatisplus直接调用userInfoDao的查询方法
@RequestMapping("/getList2")
public Collection<UserInfoEntity> getList2(){
List<UserInfoEntity> userInfoEntityList = userInfoDao.selectList(null);
return userInfoEntityList;
}
// mybatisplus直接调用userInfoDao的保存方法
@RequestMapping("/save1")
public UserInfoEntity InsertUserInfoEntity(){
UserInfoEntity userInfoEntity = new UserInfoEntity();
userInfoEntity.setName("测试名字").setPosition("测试位置").setDegree("1").setFrequency("2");
int count = userInfoDao.insert(userInfoEntity);
return userInfoEntity;
}
// mybatisplus直接调用userInfoDao的删除方法
@RequestMapping("/delete1/{id}")
public UserInfoEntity deleteUserInfoEntity(@PathVariable("id") String id){
UserInfoEntity userInfoEntity = new UserInfoEntity();
UserInfoEntity userInfoEntity1 = userInfoDao.selectById(id);
userInfoDao.deleteById(id);
return userInfoEntity1;
}
// 正常xml写sql查询方法
@RequestMapping("/getList3")
public Collection<UserInfoEntity> getList3(){
List<UserInfoEntity> userInfoEntities = userInfoDao.selectList();
return userInfoEntities;
}
}
MyBatis-Plus的QueryWrapper条件构造器
| 查询方式 | 方法说明 |
|---|---|
| setSqlSelect | 设置 SELECT 查询字段 |
| where | WHERE 语句,拼接 + WHERE 条件 |
| and | AND 语句,拼接 + AND 字段=值 |
| or | OR 语句,拼接 + OR 字段=值 |
| eq | 等于= |
| allEq | 基于 map 内容等于= |
| ne | 不等于<> |
| gt | 大于> |
| ge | 大于等于>= |
| lt | 小于< |
| le | 小于等于<= |
| like | 模糊查询 LIKE |
| notLike | 模糊查询 NOT LIKE |
| in | IN查询 |
| notIn | NOT IN 查询 |
| isNull | NULL 值查询 |
| isNotNull | IS NOT NULL |
| groupBy | 分组 GROUP BY |
| having | HAVING 关键词 |
| orderBy | 排序 ORDER BY |
| orderByAsc | ASC 排序 ORDER BY |
| orderByDesc | DESC 排序 ORDER BY |
| exists | EXISTS 条件语句 |
| notExists | NOT EXISTS 条件语句 |
| between | BETWEEN 条件语句 |
| notBetween | NOT BETWEEN 条件语句 |
| addFilter | 自由拼接 SQLlast拼接在最后,例如:last(“LIMIT 1”) |
本文介绍了如何在Maven项目中集成MyBatis-Plus,包括依赖配置、实体映射、持久层设计、服务层调用及QueryWrapper的使用技巧。详细讲解了主键配置、SQL查询方法和条件构造器,适合快速开发和复杂查询的实战指南。
4万+

被折叠的 条评论
为什么被折叠?



