简介:增强版ORM框架 mybatis plus介绍
- 背景
- 写一个数据库表的crud接口,编写实体类-》编写Controller-》编写Service-》编写DAO-》-》编写XML文件
- 特别是管理后台,多数都是简单的CRUD,用普通的mybatis有些鸡肋
- 介绍
- 官网 https://baomidou.com/
- 是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生
- 是怎么增强的呢?已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,类似JPA但优于JPA
进阶版mybatis-plus-genarator
<!-- mybatis --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- 代码自动生成依赖 begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <!-- velocity --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <!-- 代码自动生成依赖 end-->
还需要一个数据库驱动
<!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency>
小操作汇总
一些简单的Mybatic操作示例
<?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="net.xdclass.mapper.CouponMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="net.xdclass.model.CouponDO"> <id column="id" property="id" /> <result column="category" property="category" /> <result column="publish" property="publish" /> <result column="coupon_img" property="couponImg" /> <result column="coupon_title" property="couponTitle" /> <result column="price" property="price" /> <result column="user_limit" property="userLimit" /> <result column="start_time" property="startTime" /> <result column="end_time" property="endTime" /> <result column="publish_count" property="publishCount" /> <result column="stock" property="stock" /> <result column="create_time" property="createTime" /> <result column="condition_price" property="conditionPrice" /> </resultMap> <!-- 通用查询结果列 --> <sql id="Base_Column_List"> id, category, publish, coupon_img, coupon_title, price, user_limit, start_time, end_time, publish_count, stock, create_time, condition_price </sql> <!--扣减库存--> <update id="reduceStock"> update coupon set stock=stock-1 where id = #{couponId} and stock>0 </update> </mapper>
lawyerDOIPage = lawyerMapper.selectPage(pageInfo, new QueryWrapper<LawyerDO>() .like(!StringUtils.isNullOrEmpty(name), "name", name) .like(!StringUtils.isNullOrEmpty(phone), "phone", phone) .like(!StringUtils.isNullOrEmpty(office), "professionalInstitutions", office) .like(!StringUtils.isNullOrEmpty(card), "practiceNumber", card) .between(!StringUtils.isNullOrEmpty(workingTime), "major", start, end) );
常见问题
有时候需要用到这个,启动类添加@MapperScan 后面是你包的位置!
@MapperScan(value = "me.zhengjie.modules.member.mapper")
本文作者为DBC,转载请注明。
博主无敌