在MP中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是MP的配置,详情:https://mybatis.plus/config
#MyBatis配置文件位置,如果您有单独的MyBatis配置,请将其路径配置到configLocation中
mybatis-plus.config-location=classpath:mybatis-config.xml
SpringMVC的xml中写法:
如果你在Mapper中有自定义方法(XML中有自定义实现),需要进行配置,告诉Mapper所对应的XML文件位置。
mybatis-plus.mapper-locations=classpath*:mybatis/*.xml
SpringMVC的xml中写法:
Maven多模块项目的扫描路径需以classpath*:开头(即加载多个jar包下的XML文件)
mybatis-plus.type-aliases-package=cn.itcast.mp.pojo
SpringMVC的xml中写法:
本部分的配置大都为MyBatis原生支持的配置,这意味这您可以通过MyBatis XML配置文件的形式进行配置。
#关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.map-underscore-to-camel-case=false
mybatis-plus.configuration.camel-enabled-=false
mybatis-plus.global-config.db-config.id-type=auto
mybatis-plus.global-config.db-config.table-prefix=tb_
接下去,我们重点学习AbstractWrapper以及其子类
QueryWrapper wrapper=new QueryWrapper<>();
//设置条件
Map params=new HashMap<>();
params.put("name","曹操");
params.put("age","20");
params.put("password",null);
//SELECT * FROM tb_user WHRER password IS NULL AND name = ? AND age = ?
wrapper.allEq(params);
//SELECT * FROM tb_user WHRER AND name = ? AND age = ?
// wrapper.allEq(params,false);
List users=this.userMapper.selectList(wrapper);
for (User user:users){
System.out.println(user);
}
eq(=),ne(不等于),gt(大于),ge(大于等于),It(小于),Ie(小于等于)
between(BETWEEN 值1 AND 值2),notBetween(NOT BETWEEN 值1 AND 值2)
in--字段IN(value.get(0),value.get(1),...)
notIn--字段NOT IN(v0,v1,...)
like,noLike,likeLeft,likeRight
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE "%曹%"
wrapper.like("name","曹");
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name not LIKE "%曹%"
wrapper.noLike("name","曹");
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE "%曹"
wrapper.likeLeft("name","曹");
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE "曹%"
wrapper.likeRight("name","曹");
.or()
.and()
//SELECT id,name,age FROM tb_user WHERE name=? and age=?
wrapper.eq("name","李四")
.or()
.eq("age",24)
.select("id","name","age");
//SELECT id,name,age FROM tb_user WHERE name=? and age=?
wrapper.eq("name","李四")
.eq("age",24)
.select("id","name","age");