一、Mybatis-Plus
1.·MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具
2.·MP在MyBatis 的基础上只做增强不做改变
3.·为简化开发、提高效率而生
二、Mybatis Plus特性
1.@无侵入
2.·@损耗小
3.@强大的CRUD
4.@多种数据库
5.·@内置分页
6.·@内置主键生成策略
三、Mybatis Plus开发套路
- Maven依赖mybatis-plus(v3.1.0去掉mybatis与mybatis-spring)
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.1.0</version> </dependency>
- SessionFactory设置为MybatisSqlSessionFactoryBean
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!--关联数据源--> <property name="dataSource" ref="dataSource"/> <!--配置文件地址--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--mapper XML文件保存目录--> <property name="mapperLocations" value="classpath:mappers/*.xml"/> <!-- mapper resource="goods.xml" --> </bean>
- 实体类增加注解描述映射关系
//@TableName设置实体与表的对应关系 @TableName("t_goods_detail") public class GoodsDetail { //@TableId用于设置主键属性 //IdType.AUTO是指自动利用Mysql底层的自动生成功能产生主键 //主键生成后会自动回填到gdId属性 @TableId(type = IdType.AUTO) private Integer gdId; private Integer goodsId;//goods_id //如果属性与字段名对应不上,则需要使用@TableField进行说明 @TableField("gd_pic_url") private String picUrl;//gd_pic_url // @TableField("gd_order") private Integer gdOrder; public Integer getGdId() { return gdId; } public void setGdId(Integer gdId) { this.gdId = gdId; } public Integer getGoodsId() { return goodsId; } public void setGoodsId(Integer goodsId) { this.goodsId = goodsId; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public Integer getGdOrder() { return gdOrder; } public void setGdOrder(Integer gdOrder) { this.gdOrder = gdOrder; } }
4.Mapper接口继承BaseMapper
//BaseMapper定义了CRUD的方法声明,在运行时会自动根据实体的注解生成对应的SQL语句 public interface GoodsDetailMapper extends BaseMapper<GoodsDetail> { /** * 自定义SQL分页查询 * @param page 第一个对象固定为分页对象 * @param param 第二个参数Map向SQL传递多参数,但要使用@Param()定义前缀 * @return 返回分页结果 */ public IPage<Map> selectMap(IPage page ,@Param("p") Map param); }
5.xml文件
p对应参数@parm("p")
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itlaoqi.ssm.mapper.GoodsDetailMapper"> <!--使用mybatis-plus分页插件后,必须在引用参数时增加#{前缀.xxx}--> <select id="selectMap" parameterType="java.util.Map" resultType="java.util.LinkedHashMap"> select d.* , g.title from t_goods_detail d , t_goods g where d.goods_id = g.goods_id and d.gd_order = #{p.order} and d.goods_id = #{p.goodsId} </select> </mapper>
四、QueryWrapper查询构造器
@GetMapping("/qw") @ResponseBody public String testQueryWrapper(){ /*Mybatis使用QueryWrapper对象简化查询过程*/ QueryWrapper<GoodsDetail> queryWrapper = new QueryWrapper<GoodsDetail>(); /** * eq 代表精准匹配 * between 范围匹配 * like 模糊匹配 */ queryWrapper.eq("goods_id", 739); queryWrapper.between("gd_order", 100, 501); queryWrapper.likeLeft("gd_pic_url", "ED"); List<GoodsDetail> list = goodsDetailMapper.selectList(queryWrapper); for (GoodsDetail gd : list) { System.out.println(gd.getGoodsId() + ":" + gd.getPicUrl()); } return "SUCCESS"; }
五、Mybatis-Plus分页查询
@GetMapping("/p") @ResponseBody public IPage testPagination(Integer p , Integer r){ QueryWrapper<GoodsDetail> queryWrapper = new QueryWrapper<GoodsDetail>(); queryWrapper.eq("gd_order", 100); IPage page = new Page(p,r); IPage pageResult = goodsDetailMapper.selectPage(page, queryWrapper); return pageResult; }