做一个电子商务网站在哪里做,网站建设与维护大学生总结,收银系统软件一套多少钱,网站建设管理自查工作总结场景
Mybatis-Plus(简称MP)是一个Mybatis的增强工具#xff0c;只是在Mybatis的基础上做了增强却不做改变#xff0c;MyBatis-Plus支持所有Mybatis原生的特性#xff0c;
所以引入Mybatis-Plus不会对现有的Mybatis构架产生任何影响。MyBatis 增强工具包#xff0c;简化 C…场景
Mybatis-Plus(简称MP)是一个Mybatis的增强工具只是在Mybatis的基础上做了增强却不做改变MyBatis-Plus支持所有Mybatis原生的特性
所以引入Mybatis-Plus不会对现有的Mybatis构架产生任何影响。MyBatis 增强工具包简化 CRUD 操作。
启动加载 XML 配置时注入单表 SQL 操作 为简化开发工作、提高生产率而生。
MybatisPlus
MyBatis-Plus
特性
无侵入 只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑
损耗小 启动即会自动注入基本 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 操作智能分析阻断也可自定义拦截规则预防误操作
注
博客霸道流氓气质-CSDN博客
实现
1、新建SpringBoot项目引入mybatisplus的依赖 dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version/dependency
注意这里不再需要引入Mybatis的依赖mp已经包含。
这里还需要连接mysql所以添加mysql的驱动 dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency
其它使用的依赖完整如下 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency!--MySQL驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency
2、在application.yml配置文件中添加mp的配置项
mybatis-plus:# 指定 Mapper XML 文件的位置使用 classpath 通配符指定路径mapper-locations: classpath:/mapper/**/*.xml# 指定实体类的包路径用于自动扫描并注册类型别名type-aliases-package: com.badao.demo.entityglobal-config:db-config:id-type: input# 驼峰下划线转换将数据库字段的下划线命名规则转换为 Java 实体类属性的驼峰命名规则db-column-underline: true# 刷新 mapperrefresh-mapper: trueconfiguration:# 将 Java 实体类属性的驼峰命名规则转换为数据库字段的下划线命名规则map-underscore-to-camel-case: true# 查询结果中包含空值的列在映射的时候不会映射这个字段call-setters-on-nulls: true# 开启 sql 日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
这里着重修改mapper文件的位置以及实体类的包路径修改为对应。
更多配置参考官网说明
使用配置 | MyBatis-Plus 3、添加插件配置文件
mp支持很多插件比如这里引入分页插件新建配置类
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MybatisPlusConfig {/*** 加载分页插件*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}
更多配置见官方文档
插件主体 | MyBatis-Plus 4、新建表t_user
DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user (id int NOT NULL AUTO_INCREMENT,user_id int NOT NULL,name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,age int NOT NULL,PRIMARY KEY (id) USING BTREE
) ENGINE InnoDB AUTO_INCREMENT 9 CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT DYNAMIC;SET FOREIGN_KEY_CHECKS 1;
5、新建实体类User
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;Data
Builder
AllArgsConstructor
NoArgsConstructor
TableName(value t_user)
public class User implements Serializable {private static final long serialVersionUID -5514139686858156155L;private Integer id;private Integer userId;private String name;private Integer age;TableField(exist false)private String address;}
这里让实体类与表名不一致通过注解TableName(value t_user)对应。
并且实体类新增一个表中没有的字段address,通过注解TableField(exist false)来声明。
更多注解使用方法参考官方文档
注解 | MyBatis-Plus 6、 Mapper层
BaseMapperT是mybatis-plus设计的一个接口里面包含了单表的CRUD。
新建mapper并使其继承BaseMapper则可直接调用CRUD的方法。
Repository
public interface UserMapper extends BaseMapperUser {}
那么可以直接调用 如果需要在Service层可以这样声明
import com.badao.demo.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;public interface UserService extends IServiceUser {}
Service实现
Service
public class UserServiceImpl extends ServiceImplUserMapper,User implements UserService {AutowiredUserMapper userMapper;
}
调用示例 Autowiredprivate UserService userService;Testvoid test1() {User byId userService.getById(5);System.out.println(byId);}
这块可具体参考官方文档说明
CRUD 接口 | MyBatis-Plus 7、如果要实现条件构造器查询和更新 LambdaQueryWrapperUser wrapper new LambdaQueryWrapper();wrapper.eq(User::getName,222);ListUser users userMapper.selectList(wrapper);System.out.println(users);
具体参考官方文档说明
条件构造器 | MyBatis-Plus 8、要实现自定义sql
通过xml方式
mapper中新建方法
Repository
public interface UserMapper extends BaseMapperUser {Integer insertOne(User u);}
新建xmlxml中新建方法
mapper namespacecom.badao.demo.mapper.UserMapper resultMap idresultMap typecom.badao.demo.entity.User id columnid propertyid jdbcTypeINTEGER /result columnuser_id propertyuserId jdbcTypeINTEGER /result columnname propertyname jdbcTypeVARCHAR /result columnage propertyage jdbcTypeINTEGER //resultMapinsert idinsertOneinsert into t_user (user_id,name,age) values (#{userId},#{name},#{age})/insert
/mapper
以上通过xml方法还可以传递条件具体参考官方文档说明。
通过注解的方式并传递条件自定义sql
mapper中新建方法并添加注解Select
Repository
public interface UserMapper extends BaseMapperUser {Integer insertOne(User u);Select(SELECT * FROM t_user ${ew.customSqlSegment})ListUser selectOver25(Param(Constants.WRAPPER) Wrapper wrapper);
}
方法调用 LambdaQueryWrapperUser wrapper new LambdaQueryWrapper();wrapper.gt(User::getAge,25);ListUser users userMapper.selectOver25(wrapper);System.out.println(users);
更多用法参考官方文档说明 9、mp还有很多其他用法官方文档写的很清楚详细自行阅读。