Mybatis-plus的使用

Mybatis-plus

步骤:
    1.坐标
         <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
         </dependency>
        注意:mp坐标添加后,mybatis坐标移除
    2.编写注解配置实体类与关系表映射关系(truncate清空表以及主键)
        @TableName(value = "关联表名称")=========================》修饰在类
        @TableField(value = "关联字段名称")======================》修饰在属性
                    exist = "忽略字段"
        @TableId(type="指定主键生成策略,默认雪花算法")=============》修饰在属性
                    AUTO(0),
                    NONE(1),
                    INPUT(2),
                    ASSIGN_ID(3),
                    ASSIGN_UUID(4);


    3.使用
        BaseMapper===========》公共的数据访问层
        IService/ServiceImp==》公共的业务层

    4.测试代码使用MyBatisSqlSessionFactoryBuilder
1.导入依赖(移除mybatis依赖)
<!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>3.1.1</version>
    </dependency>
2.编写注解配置实体类与关系表映射关系
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "student") //对应表
public class Student {

    @TableId(value = "stu_id",type = IdType.AUTO) //主键
    private int stuId;
    @TableField(value = "stu_name") //字段
    private String stuName;
    @TableField(value = "stu_hobby")
    private String stuHobby;
    @TableField(value = "stu_age")
    private String stuAge;
    @TableField(value = "is_del")
    @TableLogic(value = "0",delval = "1") //逻辑删除
    private int isDel;


    public Student(String stuName, String stuHobby) {
        this.stuName = stuName;
        this.stuHobby = stuHobby;
    }


}
3.使用
public interface StudentMapper extends BaseMapper<Student> {
	//可以写自己特殊的增删改查
}
4.测试代码使用MyBatisSqlSessionFactoryBuilder
public class Test02 {
    InputStream stream =null;
    SqlSession sqlSession = null;
    StudentMapper mapper = null;

    @Before
    public void beforeTest(){
        try {
            stream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory  build = new MybatisSqlSessionFactoryBuilder().build(stream); //使用mybatis开头
            sqlSession = build.openSession(true);
            mapper = sqlSession.getMapper(StudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public  void  afterTest(){

        try {
            sqlSession.close();
            stream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //添加
@Test
    public void  insertTest(){

    Student student = new Student("吉","酒色财气");

    int insert = mapper.insert(student);
    System.out.println(insert);
    System.out.println("主键回填"+student.getStuId());


}

    //修改
    @Test
    public void  updateStuId(){

        Student student = new Student();

        student.setStuId(1);
        student.setStuHobby("酒色财气");
        int i = mapper.updateById(student);
        System.out.println(i);

    }

    //条件修改
    @Test
    public void  updateStu(){

        QueryWrapper<Student> QueryWrapper = new QueryWrapper<>();

        QueryWrapper.eq("stu_name","李四");

        Student student = new Student();
        student.setStuHobby("酒色财气");


        int i = mapper.update(student,QueryWrapper);
        System.out.println(i);

    }

    //删除
    @Test
    public void  deleteTest(){

        Student student = new Student("吉","酒色财气");

        int i = mapper.deleteById(2);
        System.out.println(i);



    }

    //查询
    @Test
    public void  selectTestById(){

        Student stu = mapper.selectById(6);
        System.out.println(stu);
    }

    //查询list
    @Test
    public void  selectList(){

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_name","吉鑫");
        List<Student> students = mapper.selectList(queryWrapper);
        students.forEach(System.out::println);
    }


    //分页
    @Test
    public void  pageTest(){

        //定义分页规则
        Page<Student> page = new Page<>();
        page.setSize(3);
        page.setCurrent(2);

        //查询条件
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_name","吉鑫");

        //分页查询
        IPage<Student> studentIPage = mapper.selectPage(page, null);

        long pages = studentIPage.getPages();
        long current = studentIPage.getCurrent();

        System.out.println(pages);
        System.out.println(current);

        studentIPage.getRecords().forEach(System.out::println);


    }

}
5. LambdaQueryWrapper &逻辑删除
 /**
     * 物理删除:业务数据从数据库中丢弃,执行的是delete操作
     * 逻辑删除:为数据设置是否可用状态字段,删除时设置状态字段为不可用状态,
     *         数据保留在数据库中,执行的是update操作
     *         实现步骤:
     *          步骤1:修改数据库表添加`deleted`列,比如`0`代表正常,`1`代表删除,可以在添加列的同时设置其默认值为`0`正常。
     *          步骤2:实体类添加属性以及注解
     *          @TableLogic(value="0",delval="1")
     *          private Integer deleted;
     *              value为正常数据的值,delval为删除数据的值
     * */
代码
public class Test03<show1> {
    InputStream stream = null;
    SqlSession sqlSession = null;
    StudentMapper mapper = null;

    @Before
    public void  beforeTest(){

        try {
            stream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory build = new MybatisSqlSessionFactoryBuilder().build(stream);
            sqlSession = build.openSession(true);
             mapper = sqlSession.getMapper(StudentMapper.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void afterTest(){

        try {
            sqlSession.close();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //LambdaQueryWrapper查询
    @Test
    public void  show1(){

        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(Student::getStuAge,18);

        List<Student> list = mapper.selectList(lambdaQueryWrapper);

        list.forEach(System.out::println);

    }

    //模拟动态查询1
    @Test
    public void  show2(){

        Integer num1 = null;
        Integer num2 = 25;

        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (num1 !=null){
            lambdaQueryWrapper.gt(Student::getStuAge,num1);
        }

        if (num2 != null){
            lambdaQueryWrapper.lt(Student::getStuAge,num2);
        }

        List<Student> list = mapper.selectList(lambdaQueryWrapper);

        list.forEach(System.out::println);

    }

    //模拟动态查询2
    @Test
    public void  show3(){
        Integer num1 = null;
        Integer num2 = null;
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(num1!=null,Student::getStuAge,num1);
        lambdaQueryWrapper.gt(num2!=null,Student::getStuAge,num2);

        List<Student> list = mapper.selectList(lambdaQueryWrapper);

        list.forEach(System.out::println);

    }



    //投影查询-字段查询
    @Test
    public void  show5(){

        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Student::getStuAge,Student::getStuName);

        List<Student> list = mapper.selectList(lambdaQueryWrapper);

        list.forEach(System.out::println);

    }

    //投影查询-聚合查询
    @Test
    public void  show6(){

        QueryWrapper<Student> QueryWrapper = new QueryWrapper<>();

        QueryWrapper.select("count(*) as count");

        List<Map<String, Object>> maps = mapper.selectMaps(QueryWrapper);


        maps.forEach(System.out::println);

    }

    //分组查询
    @Test
    public void  show7(){

        QueryWrapper<Student> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.select("count(stu_age) as count ,sum(stu_age) as sum ,max(stu_age) as max");
        QueryWrapper.groupBy("stu_age");

        List<Map<String, Object>> maps = mapper.selectMaps(QueryWrapper);


        maps.forEach(System.out::println);

    }

    //逻辑删除
       
    @Test
    public void  show8(){

         mapper.deleteById(5);

        List<Student> list = mapper.selectList(null);
        list.forEach(System.out::println);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值