简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具, 在 MyBatis 的基础上只做增强不做改变, 为简化开发, 提高效率而生
特性
无侵入: 只做增强不做改变, 引入它不会对现有工程产生影响, 如丝般顺滑
损耗小: 启动即会自动注入基本 CURD, 性能基本无损耗, 直接面向对象操作
强大的 CRUD 操作: 内置通用 Mapper, 通用 Service, 仅仅通过少量配置即可实现单表大部分 CRUD 操作, 更有强大的条件构造器, 满足各类使用需求
支持 Lambda 形式调用: 通过 Lambda 表达式, 方便的编写各类查询条件, 无需再担心字段写错
支持多种数据库: 支持 MySQL,MariaDB,Oracle,DB2,H2,HSQL,SQLite,Postgre,SQLServer2005,SQLServer 等多种数据库
支持主键自动生成: 支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence), 可自由配置, 完美解决主键问题
支持 xml 热加载: Mapper 对应的 xml 支持热加载, 对于简单的 CRUD 操作, 甚至可以无 xml 启动
支持 ActiveRecord 模式: 支持 ActiveRecord 形式调用, 实体类只需继承 Model 类即可进行强大的 CRUD 操作
支持自定义全局通用操作: 支持全局通用方法注入( Write once, use anywhere )
支持关键词自动转义: 支持数据库关键词 (order,key......) 自动转义, 还可自定义关键词
内置代码生成器: 采用代码或者 Maven 插件可快速生成 Mapper , Model , Service , Controller 层代码, 支持模板引擎, 更有超多自定义配置等您来使用
内置分页插件: 基于 MyBatis 物理分页, 开发者无需关心具体操作, 配置好插件之后, 写分页等同于普通 List 查询
内置性能分析插件: 可输出 Sql 语句以及其执行时间, 建议开发测试时启用该功能, 能快速揪出慢查询
内置全局拦截插件: 提供全表 delete , update 操作智能分析阻断, 也可自定义拦截规则, 预防误操作
内置 Sql 注入剥离器: 支持 Sql 注入剥离, 有效预防 Sql 注入攻击
安装: Maven+jdk8+spring4.0
1. 引入 maven 依赖
- <!-- mp 依赖
- mybatisPlus 会自动的维护 Mybatis 以及 MyBatis-spring 相关的依赖
- -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus</artifactId>
- <version>3.0.3</version>
- </dependency>
2. 配置 dataSource, 实现继承 BaseMapper, 配置 mapper 文件包位置, 然后就可使用内置的 CRUD 了
applicationContext.xml
在数据源等配置基础上添加如下配置
- <!--
- 配置 mybatis 扫描 mapper 接口的路径 mapper 为 EmployeeMapper 所在包的位置
- -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="mapper"></property>
- </bean>
- 3.
- //Beans
- @TableName("tbl_employee")// 设置对象名和表明对应
- public class Employee {
- //value 指定与数据库列名一致, type 指定主键策略为自增, 即不用手动插入 id
- @TableId(value = "id",type =IdType.AUTO)
- private Integer id ;
- @TableField("last_name")// 设置驼峰命名和数据库命令对应, 也可以在配置文件中进行全局配置.
- // 有说法称: my 已经默认开启了这一对应关系, 但是我的没有生效, 所以又手动配置了
- private String lastName;
- private String email ;
- private Integer gender;
- private Integer age ;
- // 数据库中不存在的列, 但存在类中, 注解后不会注入到 sql
- @TableField(exist = false)
- private Double salary ;
- setter(){}
- getter(){}
- }
- //Mapper
- public interface EmployeeMapper extends BaseMapper<Employee> {
- // 不需要实现, 这时候就可以调用 baseMapper 的增删改了
- }
- //Controller
- // 加载配置文件
- private ApplicationContext IoC =new ClassPathXmlApplicationContext("applicationContext.xml");
- private EmployeeMapper employeeMapper=
- IoC.getBean("employeeMapper", EmployeeMapper.class);
- @Test
- public void testCommonInsert() {
- // 初始化 Employee 对象
- Employee employee = new Employee();
- employee.setLastName("MP");
- employee.setEmail("mp@tom.com");
- employee.setGender(1);
- employee.setAge(18);
- Integer result = employeeMapper.insert(employee);
- System.out.println("result:" + result );
- // 获取当前数据在数据库中的主键值
- Integer key = employee.getId();
- System.out.println("key:" + key );
- }
到此已经实现了通用 CRUD 的操作.
4. 附一下 BaseMapper 的包含的方法
- package com.baomidou.mybatisplus.core.mapper;
- import com.baomidou.mybatisplus.core.conditions.Wrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import org.apache.ibatis.annotations.Param;
- public interface BaseMapper<T> {
- int insert(T var1);
- int deleteById(Serializable var1);
- int deleteByMap(@Param("cm") Map<String, Object> var1);
- int delete(@Param("ew") Wrapper<T> var1);
- int deleteBatchIds(@Param("coll") Collection<? extends Serializable> var1);
- int updateById(@Param("et") T var1);
- int update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);
- T selectById(Serializable var1);
- List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> var1);
- List<T> selectByMap(@Param("cm") Map<String, Object> var1);
- T selectOne(@Param("ew") Wrapper<T> var1);
- Integer selectCount(@Param("ew") Wrapper<T> var1);
- List<T> selectList(@Param("ew") Wrapper<T> var1);
- List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);
- List<Object> selectObjs(@Param("ew") Wrapper<T> var1);
- IPage<T> selectPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);
- IPage<Map<String, Object>> selectMapsPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);
- }
来源: https://www.cnblogs.com/jin-nuo/p/9734599.html