<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.2.0version>
dependency>
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
写一个接口继承BaseMapper
public interface CategoryMapper extends BaseMapper<Category> {
}
注意:类名、表名、类属性名、列名 尽量都小写比如"username"和数据库表列名"username"(mysql数据库不区分大小写)可以刚好对应上,如果你用"userName",那么数据库列名就要是"user_name"。
categoryMapper.insert(category);
1.自定义更新
public void updWrap() {
User user=new User();
user.setAddress("广州");
user.setAge(23);
userMapper.update(user,new UpdateWrapper<User>().eq("name_","a").or().eq("name_","b"));
}
2.按id进行更新
public void upd() {
User user=new User();
user.setId(10086);
user.setName("lll");
user.setAge(23);
userMapper.updateById(user);
}
1.全部查询(select * from category)
List<Category> categories = categoryMapper.selectList(null);
2.按条件查询(select * from category where flag=1)
QueryWrapper queryWrapper1=new QueryWrapper();
queryWrapper1.eq("flag",1);
categories = categoryMapper.selectList(queryWrapper1);
3.按范围查询(select * from category where id between 0 and 500)
QueryWrapper queryWrapper1=new QueryWrapper();
queryWrapper1.between("id",0,500);
categories = categoryMapper.selectList(queryWrapper1);
4.limit限制数量查询(select * from category limit 0,10)
limit “起始位置”,“条数”
例如 limit 2,5意思是从第3行开始取共5行数据
QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
String sql="limit "+start+",10";
queryWrapper.last(sql);
//从"start"开始。查询"10"条
products = productMapper.selectList(queryWrapper);
删除操作我没有使用过,别人博客:
设置逻辑删除
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
refresh: true # 刷新xml文件
db-config:
logic-delete-value: 1 #默认值1
logic-not-delete-value: 0 #默认值0
这里指定逻辑删除 删除标记 为1 表示删除 为0 表示正常
userMapper.deleteById(10086);