Mybatis-plus整合资料

本文介绍了如何在Maven项目中集成MyBatis-Plus,包括依赖配置、实体映射、持久层设计、服务层调用及QueryWrapper的使用技巧。详细讲解了主键配置、SQL查询方法和条件构造器,适合快速开发和复杂查询的实战指南。

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 查询字段
whereWHERE 语句,拼接 + WHERE 条件
andAND 语句,拼接 + AND 字段=值
orOR 语句,拼接 + OR 字段=值
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
like模糊查询 LIKE
notLike模糊查询 NOT LIKE
inIN查询
notInNOT IN 查询
isNullNULL 值查询
isNotNullIS NOT NULL
groupBy分组 GROUP BY
havingHAVING 关键词
orderBy排序 ORDER BY
orderByAscASC 排序 ORDER BY
orderByDescDESC 排序 ORDER BY
existsEXISTS 条件语句
notExistsNOT EXISTS 条件语句
betweenBETWEEN 条件语句
notBetweenNOT BETWEEN 条件语句
addFilter自由拼接 SQLlast拼接在最后,例如:last(“LIMIT 1”)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值