Mybatis-plus的使用
一、簡介
Mybatis-plus的基于mybatis的,簡化了單表mybatis的操作。
注意:它并沒有提升性能,只是簡化了開發(fā)過程。
二、在springboot中的基本使用
1、導(dǎo)入依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
2、添加相應(yīng)的數(shù)據(jù)庫配置(application.properties)
# 數(shù)據(jù)庫驅(qū)動:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 數(shù)據(jù)庫連接地址
spring.datasource.url=jdbc:mysql://localhost:3306/books?useUnicode=true&characterEncoding=utf-8
# 數(shù)據(jù)庫用戶名&密碼:
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、編寫實體類
// 如果表名和實體類的名稱一致,如果屬性名和數(shù)據(jù)庫表中字段名一致,可以不配置任何實體相關(guān)內(nèi)容
@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、實體類配置
當(dāng)實體類與表名不一致,字段名與屬性名不一致時,需要相應(yīng)的配置
@TableName 一般用來配置實體類對應(yīng)的表名,resultMap,以及忽略的屬性名(3.3.1以上的版本)等。
用在類上。
@TableId一般用來配置主鍵的增長方式,并且配置表中的主鍵字段的名稱。相當(dāng)于id標(biāo)簽
@TableField 一般用來配置表中列的名稱。相當(dāng)于result標(biāo)簽
@Version樂觀鎖標(biāo)記。
@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);
// 根據(jù)id刪除
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
// 根據(jù)where條件刪除
int delete(@Param("ew") Wrapper<T> wrapper);
// 批量根據(jù)ids刪除
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
// 根據(jù)id修改
int updateById(@Param("et") T entity);
// 根據(jù)where條件修改
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
// 根據(jù)id查詢對象
T selectById(Serializable id);
// 根據(jù)一組ids查詢集合
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
// 根據(jù)添加查詢單個對象
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
// 根據(jù)添加查詢數(shù)量
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
// 根據(jù)添加查詢集合
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);
構(gòu)造查詢條件Wrapper
具體參考:https://mybatis.plus/guide/wrapper.html#in文章來源:http://www.zghlxwxcb.cn/news/detail-484448.html
public List<User> findAll(int age){
QueryWrapper wrapper = new QueryWrapper();
wrapper.gt("u_age", age);
return userDAO.selectList(wrapper);
}
五、關(guān)聯(lián)查詢的實現(xiàn)
mybatis-plus對于關(guān)聯(lián)查詢本身需要mybatis來支持,所以可以寫mapper.xml來實現(xiàn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-484448.html
@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") // 關(guān)聯(lián)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>
到了這里,關(guān)于Mybatis-plus的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!