Mybatis-plus的使用

Mybatis-plus的使用

一、简介

Mybatis-plus的基于mybatis的,简化了单表mybatis的操作。

注意:它并没有提升性能,只是简化了开发过程。

二、在springboot中的基本使用

1、导入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>

2、添加相应的数据库配置(application.properties)

# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/books?useUnicode=true&characterEncoding=utf-8
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root
# mapper的路径
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml

3、在Application类上添加dao接口的路径扫描

@SpringBootApplication
@MapperScan("com.qf.day15.dao")
public class Day15Application {
    public static void main(String[] args) {
        SpringApplication.run(Day15Application.class, args);
    }
}

4、编写实体类

// 如果表名和实体类的名称一致,如果属性名和数据库表中字段名一致,可以不配置任何实体相关内容
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

5、编写dao接口

public interface UserDAO extends BaseMapper<User> {
}

6、测试使用

@SpringBootTest
class Day15ApplicationTests {
    @Resource
    private UserDAO userDAO;

    @Test
    void contextLoads() {
        List<User> list = userDAO.selectList(null);
        System.out.println(list);
    }
}
三、常用配置

1、实体类配置

当实体类与表名不一致,字段名与属性名不一致时,需要相应的配置

@TableName 一般用来配置实体类对应的表名,resultMap,以及忽略的属性名(3.3.1以上的版本)等。

用在类上。

@TableId一般用来配置主键的增长方式,并且配置表中的主键字段的名称。相当于id标签

@TableField 一般用来配置表中列的名称。相当于result标签

@Version乐观锁标记。

@Data
@TableName("tb_user")
public class User {
    @TableId(value = "u_id", type = IdType.AUTO)
    private Long id;
    @TableField("u_name")
    private String name;
    @TableField("u_age")
    private Integer age;
    @TableField("u_email")
    private String email;
}
四、常用方法
// 添加
int insert(T entity);
// 根据id删除
int deleteById(Serializable id);

int deleteByMap(@Param("cm") Map<String, Object> columnMap);
// 根据where条件删除
int delete(@Param("ew") Wrapper<T> wrapper);
// 批量根据ids删除
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
// 根据id修改
int updateById(@Param("et") T entity);
// 根据where条件修改
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
// 根据id查询对象
T selectById(Serializable id);
// 根据一组ids查询集合
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
// 根据添加查询单个对象
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
// 根据添加查询数量
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
// 根据添加查询集合
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
// 分页查询
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);

构造查询条件Wrapper

具体参考:https://mybatis.plus/guide/wrapper.html#in

public List<User> findAll(int age){
    QueryWrapper wrapper = new QueryWrapper();
    wrapper.gt("u_age", age);
    return userDAO.selectList(wrapper);
}
五、关联查询的实现

mybatis-plus对于关联查询本身需要mybatis来支持,所以可以写mapper.xml来实现。

@Data
@TableName("book_type")
public class BookType {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String name;
}

@Data
@TableName(value = "books", resultMap = "bookMap") // 关联xml中的resultMap配置
public class Book {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String name;
    private String author;
    @TableField("book_desc")
    private String bookDesc;
    @TableField("create_time")
    private Date createTime;
    private BookType type;
    @TableField("img_path")
    private String imgPath;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.day15.dao.BookDAO">
    <resultMap id="bookMap" type="com.qf.day15.entity.Book">
        <association property="type" column="type_id" select="com.qf.day15.dao.BookTypeDAO.selectById"></association>
    </resultMap>
</mapper>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值