利用花生壳做网站百度上如何做优化网站
目录
- mybatis-plus集合
- 1、简介
- 2、特性
- 3、开始使用
- 4、QueryWrapper的使用
- 5、补充
mybatis-plus集合
1、简介
MyBatis-Plus (简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis-plus只是增强了mybatis的功能,只是增加了JDBC基础CURD的功能的baseMapper类,封装了一些基础的CURD的mapper接口和实现方法,减少了一些基础的代码编译
2、特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
- 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
- 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
- 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
3、开始使用
-
创建springBoot
-
写好实体类,引入lombok
@NoArgsConstructor @AllArgsConstructor @Data @Component public class User implements Serializable{@TableId(type = IdType.AUTO)private Long id;private String username;@TableField(exist = false)private String password1;private String password;private Long phonenumber;private String email; }
@TableId注解识别主键和主键类型
@TableField注解识别字段和字段类型
@TableField(exist = false)标识不是表中元素
主键用Long类型,mybatis-plus主键用的是雪花算法(通过时间戳来生成唯一主键)
int类型没用这么多位数,所以别用
用
@TableId(type = IdType.AUTO)
注释来解释主键自增,可以不用雪花算法 -
写好mapper接口和mapper映射实现xml文件,引入mybatis-plus
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency>
-
application启动类增加
@MapperScan("com.hai.boot.mapper")
注释,也可以在mapper接口类中增加@Mapper
注释,让springboot来管理mapper -
写mapper接口类
package com.hai.boot.mapper;import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hai.boot.pojo.User; @Repository public interface UserMapper extends BaseMapper<User>{ // @Select("select * from user where username = #{username}") // User getByUserName(String username);}
继承
extends BaseMapper<T>
,T为实体类,BaseMapper类里写好了一些基础的CURD接口,就不需要我们写mapper实体xml文件方法和mapper接口了public interface BaseMapper<T> extends Mapper<T> {/*** 插入一条记录** @param entity 实体对象*/int insert(T entity);/*** 根据 ID 删除** @param id 主键ID*/int deleteById(Serializable id);/*** 根据实体(ID)删除** @param entity 实体对象* @since 3.4.4*/int deleteById(T entity);/*** 根据 columnMap 条件,删除记录** @param columnMap 表字段 map 对象*/int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);/*** 根据 entity 条件,删除记录** @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)*/int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 删除(根据ID或实体 批量删除)** @param idList 主键ID列表或实体列表(不能为 null 以及 empty)*/int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList);/*** 根据 ID 修改** @param entity 实体对象*/int updateById(@Param(Constants.ENTITY) T entity);/*** 根据 whereEntity 条件,更新记录** @param entity 实体对象 (set 条件值,可以为 null)* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)*/int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);/*** 根据 ID 查询** @param id 主键ID*/T selectById(Serializable id);/*** 查询(根据ID 批量查询)** @param idList 主键ID列表(不能为 null 以及 empty)*/List<T> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList);/*** 查询(根据 columnMap 条件)** @param columnMap 表字段 map 对象*/List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);/*** 根据 entity 条件,查询一条记录* <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>** @param queryWrapper 实体对象封装操作类(可以为 null)*/default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {List<T> list = this.selectList(queryWrapper);// 抄自 DefaultSqlSession#selectOneif (list.size() == 1) {return list.get(0);} else if (list.size() > 1) {throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());} else {return null;}}/*** 根据 Wrapper 条件,判断是否存在记录** @param queryWrapper 实体对象封装操作类* @return 是否存在记录*/default boolean exists(Wrapper<T> queryWrapper) {Long count = this.selectCount(queryWrapper);return null != count && count > 0;}/*** 根据 Wrapper 条件,查询总记录数** @param queryWrapper 实体对象封装操作类(可以为 null)*/Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 根据 entity 条件,查询全部记录** @param queryWrapper 实体对象封装操作类(可以为 null)*/List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 根据 Wrapper 条件,查询全部记录** @param queryWrapper 实体对象封装操作类(可以为 null)*/List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 根据 Wrapper 条件,查询全部记录* <p>注意: 只返回第一个字段的值</p>** @param queryWrapper 实体对象封装操作类(可以为 null)*/List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 根据 entity 条件,查询全部记录(并翻页)** @param page 分页查询条件(可以为 RowBounds.DEFAULT)* @param queryWrapper 实体对象封装操作类(可以为 null)*/<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/*** 根据 Wrapper 条件,查询全部记录(并翻页)** @param page 分页查询条件* @param queryWrapper 实体对象封装操作类*/<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); }
-
写service服务层的接口类,
IService<T>
同上mapper层,这样就可以直接在controller层上用接口就行了public interface UserService extends IService<User>{ // User getByUserName(String username); }
-
写service服务层的实体类
@Service public class UserSerrviceImpl extends ServiceImpl<UserMapper, User> implements UserService { // @Autowired // private UserMapper userMapper; // @Override // public User getByUserName(String username) { // User user = userMapper.getByUserName(username); // return user;} }
注意:
BaseMapper类和IService类不影响基本的mybatis功能,可以自定义mapper和以前一样
4、QueryWrapper的使用
mybatis-plus为了实现有条件性实现CURD功能,如:模糊查询功能。增加了Wrapper类来实现。
mybatis以前是通过example实体类的createCriteria()方法来添加条件,plus中就减少了example实体类的创建
实现模糊查询:直接在控制层写
QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(seacrch),"tittle", seacrch);
List<Blog> list = blogService.list(queryWrapper);
5、补充
更多细节查看官网:https://baomidou.com