目录
一.添加
二.相关注解
三.修改
四.删除
五.查询
六.条件构造器
七.分页查询
八.全局配置
一.添加
1.配置文件开启SQL日志打印
# 开启SQL日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.测试添加方法:
@Testpublic void testAdd(){Student student = new Student(null,"尚尚","ss@163.com","m",20) ;studentMapper.insert(student);// MyBatisPlus插入成功后,可直接直接获取主键的值System.out.println(student.getId());}
3.解决使用Mybatis-plus出现数据库id很大或者为负数的情况请参考下面这篇文章:
(137条消息) 使用Mybatis-plus出现数据库id很大或者为负数的情况_程序员青戈的博客-CSDN博客
4.直接运行上面的测试函数
二.相关注解
我们将表名改为tb_student,id字段名改为sid,name字段名改为sname,此时由于名字不同无法完成映射,我们可以通过相关注解为实体类对象指定表名和列名。
@TableName
作用:指定类为哪个表的实体类
位置:类上方
@TableId
作用:指定实体类的属性为主键
位置:属性上方
属性:
- value:主键字段名
- type:主键策略
@TableField
作用:在属性和列名不同的情况下,指定映射关系
位置:非主键属性上方
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_student")
public class Student {@TableId(value = "id", type = IdType.AUTO)private Integer id;@TableField("sname")private String name;private String email;private String gender;private Integer age;
}
然后运行一下之前写的测试代码。
三.修改
根据主键进行更新
//修改@Testpublic void testUpdate(){//创建实体类对象Student student = new Student();//设置需要更新的属性student.setName("钱学森");//设置需要修改的数据idstudent.setId(1);//根据主键进行更新,没有设置的值则忽略studentMapper.updateById(student);}
四.删除
// 根据id删除@Testpublic void testDeleteById(){studentMapper.deleteById(4);}// 根据id批量删除@Testpublic void testDeleteBatch(){ArrayList<Integer> ids = new ArrayList();//删除id为2和3的记录ids.add(2);ids.add(3);studentMapper.deleteBatchIds(ids);}// 根据字段条件删除@Testpublic void testDeleteMap(){Map<String,Object> map = new HashMap();// 删除name为尚尚且gender为男的数据// 键为数据库列名,而不是对象属性名map.put("sname","尚尚");map.put("gender","男");studentMapper.deleteByMap(map);}
五.查询
// 根据id查询@Testpublic void testFindById1() {Student student = studentMapper.selectById(1);System.out.println(student);}// 根据id批量查询@Testpublic void testFindBatch() {//创建主键集合List<Integer> ids = new ArrayList<>();ids.add(1);ids.add(2);ids.add(3);List<Student> students = studentMapper.selectBatchIds(ids);students.forEach(System.out::println);}// 根据字段条件查询@Testpublic void testFindMap() {Map<String,Object> map = new HashMap<>();//查询name为小雨,age为19的数据map.put("sname","小雨");map.put("age",19);List<Student> students = studentMapper.selectByMap(map);students.forEach(System.out::println);}
六.条件构造器
Mybatis-Plus通过QueryWrapper对象让用户自由的构建SQL条件,简单便捷,没有额外的负担,
能够有效提高开发效率。
条件参数说明:
用法如下:
如果是并列关系,那么and函数可省略。直接就像条件构造器2和条件构造器3那样用.继续往下写就行。
// 条件构造器1@Testpublic void testFindWrapper1() {// 查询年龄小于20或年龄大于22的人QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.lt("age",20).or().gt("age",22) ;List<Student> students = studentMapper.selectList(queryWrapper);students.forEach(System.out::println);}// 条件构造器2@Testpublic void testFindWrapper2() {// 查询性别为女,且年龄小于等于20的数据QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.eq("gender","女").le("age",20);List<Student> students = studentMapper.selectList(queryWrapper);students.forEach(System.out::println);}// 条件构造器3@Testpublic void testFindWrapper3() {// 查询名字包含"小"的学生,按照年龄升序排序QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.like("sname","小").orderByAsc("age");List<Student> students = studentMapper.selectList(queryWrapper);students.forEach(System.out::println);}
注:构造时使用的是数据库字段,不是Java属性
七.分页查询
MP分页查询要配置分页插件。
1.在配置类或启动类配置分页插件
@SpringBootApplication
@MapperScan("com.first.mpdemo1.mapper")
public class Mpdemo1Application {public static void main(String[] args) {SpringApplication.run(Mpdemo1Application.class, args);}// 注册插件@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//不同的数据库对应不同的分页语句,所以要注明数据库类型interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}
2.测试分页查询
// 分页查询@Testpublic void testFindPage() {//创建分页条件,从第0条数据开始,获取两条数据Page page = new Page(2,2);// 分页查询IPage<Student> iPage = studentMapper.selectPage(page, null);// 打印分页数据System.out.println("结果集:"+iPage.getRecords());System.out.println("总页数:"+iPage.getPages());System.out.println("总条数:"+iPage.getTotal());System.out.println("当前页:"+iPage.getCurrent());System.out.println("每页条数:"+iPage.getSize());}
八.全局配置
假如数据库的所有表都以 tb_ 开头,主键都是自增的。如果针对每一个实体类都要添加相关注解比较麻烦,可以在SpringBoot配置文件中进行全局配置,该配置在所有的实体类中都生效。
application.yml:
mybatis-plus:#全局配置global-config:db-config:#主键类型id-type: AUTO# 设置表名前缀table-prefix: tb_# 是否使用驼峰转下划线命名,默认开启table-underline: true
配置之后就可以如下写了
@Data
@NoArgsConstructor
@AllArgsConstructor
//@TableName("tb_student")
public class Student extends Model<Student> {//@TableId(value = "sid", type = IdType.AUTO)@TableId(value = "sid")private Integer id;@TableField("sname")private String name;private String email;private String gender;private Integer age;
}
驼峰转下划线命名的意思:studentTable变成student_table,因为实体类一般用驼峰命名法,而
数据库一般是用下划线的形式命名。