• mybatis单框架通用mapper使用(二)


    mybatis单框架通用mapper使用(二)

    1 查询

    1.1 简单查询

    1.1.1 查多条
    a 用法
    接口引用.select(实体类对象引用);
    //里面实体类对象,里面不为null值的部分就会作为条件被查询,多个条件使用and进行拼接起来
    //传入为null就是查询全部的值
    
    • 1
    • 2
    • 3
    b 测试代码
      @Test
        public void t2(){
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper mapper = sqlSession.getMapper(SongsMapper.class);
            Songs s=new Songs();
            s.setName("晴天");
            s.setSinger_name("周杰伦");
            //不为null才会查 动态sql,实体类的属性作为条件,多个条件用and分隔
            List<Songs> songs = mapper.select(s);
            songs.forEach(System.out::println);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    c 测试代码运行结果

    在这里插入图片描述

    1.1.2 查单条
    a 根据属性查
    a.1 用法
    接口引用.selectOne(实体类对象引用);
    
    • 1
    a.2 测试代码
    @Test
        public void t2(){
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper mapper = sqlSession.getMapper(SongsMapper.class);
            Songs s=new Songs();
            s.setName("稻香");
            s.setSinger_name("周杰伦");
            Songs song01 = mapper.selectOne(s);
            System.out.println(song01);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    a.3 测试代码运行截图

    在这里插入图片描述

    b 根据Id查询
    b.1 用法
    接口引用.selectByPrimaryKey(id值);
    //使用这个必须要给主键加上@id注解的
    
    • 1
    • 2
    b.2 测试代码
     @Test
        public void t2(){
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper mapper = sqlSession.getMapper(SongsMapper.class)
            //根据id查找 因为加了注解@id(需要在实体类的id里面加@Id)
            Songs songs = mapper.selectByPrimaryKey(777);
            System.out.println(songs);
       } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    b.3 测试代码运行截图

    在这里插入图片描述

    1.2 复杂查询

    1.2.1 等值比较
    a 用法
    条件对象.andEqualTo("实体类属性名",需要查询的值);
    
    • 1
    b 测试代码
      @Test
        public void t1(){
            //方法不要和它自带的方法一样
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper mapper = sqlSession.getMapper(SongsMapper.class);
            //创建条件构造器,基于哪个实体类来设计条件
            Example ex=new Example(Songs.class);
           //创建条件对象1 一个Criteria就是一个括号
          Example.Criteria c= ex.createCriteria();
          c.andEqualTo("singer_name","林俊杰");
          List<Songs> songs = mapper.selectByExample(ex);
          songs.forEach(System.out::println);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    c 测试运行截图

    在这里插入图片描述

    1.2.2 条件里面既有或也有并且
    a 思路
    使用andIn(值的集合)来替代多个or的拼接
    然后条件查询对象用完andIn()后直接调用其他方法即可
    
    • 1
    • 2
    b 测试代码
    @Test
    public void t2(){
        //方法不要和它自带的方法一样
        //查询歌手周杰伦和张学友并且发行时间在2000-1-1之后的歌曲
        SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
        SqlSession sqlSession = sf.openSession();
        SongsMapper sm = sqlSession.getMapper(SongsMapper.class);
        //创建条件构造器,基于哪个实体类来设计条件
        Example ex=new Example(Songs.class);
        //创建条件对象1 一个Criteria就是一个括号
        Example.Criteria c1= ex.createCriteria();
        ArrayList arr=new ArrayList();
        arr.add("张学友");
        arr.add("周杰伦");
        c1.andIn("singer_name",arr);
        c1.andGreaterThanOrEqualTo("releaseDate", "2000-01-01");
        List<Songs> songs = sm.selectByExample(ex);
        songs.forEach(System.out::println);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    c 测试代码运行截图

    在这里插入图片描述

    2 修改

    2.1 用法

    接口引用.updateByPrimaryKeySelective(实体类);
    
    • 1

    2.2 测试代码

    public void t2() {
            //方法不要和它自带的方法一样
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper sm = sqlSession.getMapper(SongsMapper.class);
            //带selective是动态sql(不为null)
            Songs s=new Songs();
            s.setId(1002);
            s.setName("葫芦娃");
            int i = sm.updateByPrimaryKeySelective(s);
            sqlSession.commit();
    //        System.out.println("添加后: "+s);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3 测试代码运行截图

    2.3.1 修改前

    在这里插入图片描述

    2.3.2 测试代码运行的截图

    在这里插入图片描述

    2.3.3 修改后

    在这里插入图片描述

    3 添加

    3.1 用法

    接口引用.insertSelective(s);
    /*注意:为空的数据库就不能去添加*/
    
    • 1
    • 2

    3.2 测试代码

    3.2.1 在实体类中 需要设置主键回填
    package entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import javax.persistence.*;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Songs{
        //IDENTITY是自增长
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)//主键回填
        private Integer id;
        private String singer_name;
        private String album;
        private String albumImg;
        private String name;
        private String releaseDate;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.2.2 测试

      @Test
        public void t2() {
            //方法不要和它自带的方法一样
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper sm = sqlSession.getMapper(SongsMapper.class);
            //带selective是动态sql
            /*添加*/
            Songs s=new Songs(null,"凤凰传奇","策马崩腾",null,"天籁","2022-1-1");
            //为空的就不加
            sm.insertSelective(s);
            sqlSession.commit();
            System.out.println("添加后: "+s);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.3 测试代码运行截图

    在这里插入图片描述

    4 删除

    4.1 用法

    接口引用.deleteByPrimaryKey(键名);
    
    • 1

    4.2 测试代码

    @Test
        public void t2() {
            //方法不要和它自带的方法一样
            SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
            SqlSession sqlSession = sf.openSession();
            SongsMapper sm = sqlSession.getMapper(SongsMapper.class);
            //带selective是动态sql
            sm.deleteByPrimaryKey(1003);
            sqlSession.commit();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4.3 测试代码运行截图

    4.3.1 删除前

    在这里插入图片描述

    4.3.2 测试代码运行截图

    在这里插入图片描述

    4.3.3 删除后

    在这里插入图片描述

  • 相关阅读:
    Vue-basic 06.数据代理
    JS截取url上面的参数
    前端培训丁鹿学堂:node入门之url模块和querystring模块
    IT创业项目 - 跟淘宝商城合作网赚项目,赚多少你说了算!
    液位检测仪在线监测系统解决方案
    基于Hadoop协同过滤的电子商务商品推荐(购买组合)系统
    随身WIFI刷真Linux(Debian)系统搭配拓展坞做超低功耗服务器
    观影《铁拳男人》有感
    LeetCode //C - 18. 4Sum
    LabVIEW计算相机图像传感器分辨率以及镜头焦距
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/127795129