mybatis

87 阅读1分钟

逻辑删除

在数据库和对应实体类添加字段deleted

image.png

配置文件

#逻辑删除

mybatis-plus.global-config.db-config.logic-delete-value=1 # 逻辑已删除值(默认为 1)

mybatis-plus.global-config.db-config.logic-not-delete-value=0 # 逻辑未删除值(默认为 0)

条件构造器

image.png

常用条件

gt:大于,ge:大于等于,lt:小于,le:小于等于,eq:等于,ne:不等于

between在值1和值2之间,notBetween:不在值1和值2之间,like:’%值%’,notLike:’%值%’,

likeLeft:’%值’,likeRight:'值%' isNull:字段 IS NULL,isNotNull:字段 IS NOT NULL

in:字段 IN (v0, v1, …),notIn:字段 NOT IN (value.get(0), value.get(1), …)

乐观锁

在实体类version字段里加@Version注解,加入一个配置类 @Configuration public class MyConfig {

//乐观锁插件
@Bean
public OptimisticLockerInterceptor  optimisticLockerInterceptor() {
    return new OptimisticLockerInterceptor ();
}

标签

where

如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。

trim

prefix 加前缀

image.png

<trim prefix="WHERE" prefixOverrides="AND |OR ">

...

 # choose when otherwise 一起用
 
 when otherwise 如果有一个when成立 choose结束 都不成立执行otherwise
 
u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%') AND u.sex = #{sex, jdbcType=INTEGER} AND u.birthday = #{birthday, jdbcType=DATE} ```

bind

用于模糊查询

      ``<select id=``"listProduct" resultType=``"Product"``>

            ``<bind name=``"likename" value=``"'%' + name + '%'" />

            ``select * from   product_  where name like #{likename}

        ``</select>

foreach

<select id=``"selectPostIn" resultType=``"domain.blog.Post"``>

  ``SELECT *

  ``FROM POST P

  ``WHERE ID in

  ``<foreach item=``"item" index=``"index" collection=``"list"

      ``open=``"(" separator=``"," close=``")"``>

        ``#{item}

  ``</foreach>

</select>

ofType(一对多) , javaType(一对一)班级(一,班级实体类 添加List list mapper添加<collection property="accounts" ofType="account">学生(多 实体类添加 Class class mapper添加 <association property="user" column="uid" javaType="user">