在Mybatis-Plus中有大量配置,其中有一部分是Mybatis原生的配置,另一部分是Mybatis-Plus的配置

基本配置

configLocation

MyBatis配置文件位置,如果有单独单独的MyBatis配置,请将其路径配置到configLocation中。MyBatisConfiguration的具体内容请参考MyBatis官方文档

Spring Boot
mybatis-plus.config-location=classpath:mybatis-config.xml

在这里插入图片描述

Spring MVC:

在这里插入图片描述

mapperLocations

MyBatis Mapper所对应的XML文件位置,如果在Mapper中有自定义方法(XML中有自定义实现),需要进行该配置,告诉Mapper所对应的XML文件位置。

Spring Boot

*代表可以扫面所有依赖中的classpath下的xml文件

# 指定Mapper.xml文件的路径
mybatis-plus.mapper-locations = classpath*:mybatis/*.xml

在这里插入图片描述

Spring MVC
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:mybatis/*.xml"/>
</bean>

typeAliasesPackage

MyBaits别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在Mapper对应的XML文件中可以直接使用类名,而不用使用全限定的类名(即XML中调用的时候不用包含包名)。

Spring Boot
# 实体对象的扫描包
mybatis-plus.type-aliases-package=com/zg/mp/pojo

在这里插入图片描述

Spring MVC
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="typeAliasesPackage"
value="com.baomidou.mybatisplus.samples.quickstart.entity"/>
</bean>

进阶配置

本部分 (Configuration )的配置大都为MyBatis原生支持的配置,这意味着您可以通过MyBatis XML配置文件的形式进行配置。

mapUnderscoreToCamelCase

类型:Boolean
默认值:true
是否开启自动驼峰命名规则( camel case )映射,即从经典数据库列名A_COLUMN(下划线命名)到经典Java属性名aColumn(驼峰命名)的类似映射。

注意︰
此属性在MyBatis 中原默认值为false,在MyBatis-Plus中,此属性也将用于生成最终的SQL的select body如果您的数据库命名符合规则无需使用@TableField 注解指定数据库字段名
关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在

#关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.map-underscore-to-camel-case=false
cacheEnabled

类型:boolean
默认值︰true
全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为true。

mybatis-plus.configuration.cache-enabled=false

DB策略配置

idType
Spring Boot

类型:com. baomidou.mybatisplus.annotation.IdType
默认值:ID_WORKER

全局默认主键类型,设置后,即可省略实体对象中的@Tableld(type = ldType.AUTO)配置。

#配置全局id自增长的策略
mybatis-plus.global-config.db-config.id-type=auto

在这里插入图片描述

Spring MVC
<!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合-->
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="globalConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig">
<bean
class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
<property name="idType" value="AUTO"/>
</bean>
</property>
</bean>
</property>
</bean>
tablePrefix

类型:string
默认值:null
表名前缀,全局配置后可省略@TableName()配置。
在MP插件中,会默认将dao层实现的对象名首字母转换为小写,作为表名进行映射,所以才需要加@TableName("tb_user")前缀

Spring Boot
mybatis-plus.global-config.db-config.table-prefix=tb_

在这里插入图片描述

Spring MVC:
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="globalConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig">
<bean
class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
<property name="idType" value="AUTO"/>
<property name="tablePrefix" value="tb_"/>
</bean>
</property>
</bean>
</property>
</bean>

条件构造器

在MP中,Wrapper接口的实现类关系如下:
在这里插入图片描述
可以看到,AbstractWrapper和AbstractChainWrapper是重点实现,接下来我们重点学习AbstractWrapper以及其子类。
QueryWrapper(LambdaQueryWrapper)和UpdateWrapper(LambdaUpdateWrapper)的父类用于生成sql的where 条件, entity属性也用于生成sql的where 条件注意: entity生成的where条件与使用各个api生成的where 条件没有任何关联行为

allEq

说明:

allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)

params : key 为数据库字段名, value 为字段值 null2IsNull : 为 true 则在 map 的 value 为
null 时调用 isNull 方法,为 false 时则忽略 value 为 null 的

@Test
    public void testAllEq(){
        Map<String,Object> params = new HashMap<>();
        params.put("name","李四");
        params.put("age","20");
        params.put("password",null);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //SELECT id,user_name,password,name,age,email FROM tb_user WHERE password IS NULL AND name = ? AND age = ?
    //    wrapper.allEq(params);
        //SELECT id,user_name,password,name,age,email FROM tb_user WHERE name = ? AND age = ?
    //    wrapper.allEq(params,false);//为false表示isnull不作为条件去做查询
        // SELECT id,user_name,password,name,age,email FROM tb_user WHERE age = ? 
        wrapper.allEq((k,v)->(k.equals("age") || k.equals("id")),params);
        //意思为传入的params是否能够作为查询的条件取决于lambda中的的结果
        List<User> users = this.userMapper.selectList(wrapper);
       
        for (User user : users) {
            System.out.println(user);
        }
    }
基本比较操作

eq :等于=
ne : 不等于<>
gt :大于>
ge :大于等于>=
lt :小于<
le : 小于等于<=
between : BETWEEN 值1 AND值2
notBetween : NOT BETWEEN 值1AND值2
in :字段IN (value.get(O), value.get(1),…)
notIn: 字段 NOT IN (v0, v1, …)

package cn.itcast.mp;
import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testEq() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE password = ?
AND age >= ? AND name IN (?,?,?)
wrapper.eq("password", "123456")
.ge("age", 20)
.in("name", "李四", "王五", "赵六");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}
模糊查询

like:LIKE ‘%值%’ --》例: like(“name”, “王”) —> name like ‘%王%’
notLike:NOT LIKE ‘%值%’ --》例: notLike(“name”, “王”) —> name not like ‘%王%’
likeLeft:LIKE ‘%值’ --》例: likeLeft(“name”, “王”) —> name like ‘%王’
likeRight:LIKE ‘值%’ --》例: likeRight(“name”, “王”) —> name like ‘王%’

package cn.itcast.mp;
import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE ?
//Parameters: %曹%(String)
wrapper.like("name", "曹");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}
排序

orderBy:排序:ORDER BY 字段, … —》例: orderBy(true, true, “id”, “name”) —> order by id ASC,name ASC
orderByAsc:排序:ORDER BY 字段, … ASC --》例: orderByAsc(“id”, “name”) —> order by id ASC,name ASC
orderByDesc:排序:ORDER BY 字段, … DESC --》例: orderByDesc(“id”, “name”) —> order by id DESC,name DESC

package cn.itcast.mp;
import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user ORDER BY age DESC
wrapper.orderByDesc("age");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}
逻辑查询

or:拼接 OR -->主动调用 or 表示紧接着下一个方法不是用 and 连接!(不调用 or 则默认为使用 and 连接)
and:AND 嵌套 —>例: and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”)) —> and (name = ‘李白’ and status<> ‘活着’)

package cn.itcast.mp;
import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name = ? OR
age = ?
wrapper.eq("name","李四").or().eq("age", 24);
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}
select

在MP查询中,默认查询所有的字段,如果有需要也可以通过select方法进行指定字段。

package cn.itcast.mp;
import cn.itcast.mp.mapper.UserMapper;
import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,name,age FROM tb_user WHERE name = ? OR age = ?
wrapper.eq("name", "李四")
.or()
.eq("age", 24)
.select("id", "name", "age");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐