0
点赞
收藏
分享

微信扫一扫

对话人工智能 |新时代AI如何“落地“

林肯公园_97cc 2023-06-15 阅读 51

Mybatis-plus的使用

Mybatis-plus的使用

一、简介

二、在springboot中的基本使用

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
# 数据库驱动:
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
@SpringBootApplication
@MapperScan("com.qf.day15.dao")
public class Day15Application {
    public static void main(String[] args) {
        SpringApplication.run(Day15Application.class, args);
    }
}
// 如果表名和实体类的名称一致,如果属性名和数据库表中字段名一致,可以不配置任何实体相关内容
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
public interface UserDAO extends BaseMapper<User> {
}
@SpringBootTest
class Day15ApplicationTests {
    @Resource
    private UserDAO userDAO;

    @Test
    void contextLoads() {
        List<User> list = userDAO.selectList(null);
        System.out.println(list);
    }
}

三、常用配置

@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);
public List<User> findAll(int age){
    QueryWrapper wrapper = new QueryWrapper();
    wrapper.gt("u_age", age);
    return userDAO.selectList(wrapper);
}

五、关联查询的实现

@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>
举报

相关推荐

0 条评论