• mybatis的增删改查返回值小析(二)


    本文验证了通过mybatis访问数据库时的,增删改查的返回值情况。

    直接看代码。

    1、service层

    /**
         *@Author: Administrator on 2020/3/12 15:15
         *@param:
         *@return:
         *@Description:查询同步情况
         */
        @Override
        public PageInfo getSyncstatusPages(Syncstatus vo, int pageNo, int pageSize) {
            PageHelper.startPage(pageNo, pageSize);
    
    
           /* //查看增删改查的返回值
            //1新增:返回值自己定义,可以是void,int
            //1-1新增一条数据:插入成功,返回值为1
            int insert_success1 = yylfHttpServletMapper.insert("8", "2", "1");
            //1-2新增多条数据:插入成功,返回值为插入的数据条数,当有一条数据错误时,所有数据都会插入失败
            int insert_success2 = yylfHttpServletMapper.insert_duotiao("7");
            String insert_success3 = yylfHttpServletMapper.insert_duotiao_String("7");//不支持返回值为String类型
            //1-3新增一条数据:插入失败:主键冲突,会直接报异常
            int insert_failed = yylfHttpServletMapper.insert("1", "2", "1");
            //1-4插入null:属性为null,如果表中所有字段允许为null,插入一条所有值均为null的数据
            Syncstatus syncstatus1 = null;
            yylfHttpServletMapper.insertSyncstatus(syncstatus1);
            //1-5插入一个没有赋值的对象:属性为null,如果表中所有字段允许为null,插入一条所有值均为null的数据
            Syncstatus syncstatus2 = new Syncstatus();
            yylfHttpServletMapper.insertSyncstatus(syncstatus2);*/
    
    
            /*//2删除:返回值自己定义,可以是void,int
            //2-1删除成功:没有数据:返回值为0
            int delete_success1 = yylfHttpServletMapper.delete("0");
            //2-2删除成功:有多条数据:返回值为删除的数据条数
            int delete_success2 = yylfHttpServletMapper.delete_systemcode("2");
            //2-3删除失败:例如有外键:报异常
            int delete_fail = yylfHttpServletMapper.delete("1");*/
    
            //3更新:返回值自己定义,可以是void,int
            //3-1更新成功:没有数据,返回值为0
            int update_no = yylfHttpServletMapper.update_no("0");
            //3-2更新成功:有多条数据,返回更新的数据条数
            int update_duotiao = yylfHttpServletMapper.update_duotiao_systemcode("2");
            //3-3更新失败:例如有外键,报异常
            //int update_fail = yylfHttpServletMapper.update_fail("1");
    
            //4查询
            //4-1 没数:String 类型返回null
            Object object = yylfHttpServletMapper.select("0");
            //4-1 没数:集合 类型返回[]空集合
            Syncstatus syncstatus3 = new Syncstatus();
            syncstatus3.setStatus("7");
            List<Syncstatus> page0 = yylfHttpServletMapper.getSyncstatusList(syncstatus3);
            //4-1 没数:int 类型返回null,如果定义为int会报错。因为没数时返回null,可以将返回类型改为String
            String i = yylfHttpServletMapper.select_int(0);
            //4-1:当返回值为对象时,若返回值为空,则返回null
            //4-2 有数
            List<Syncstatus> pages = yylfHttpServletMapper.getSyncstatusList(vo);
            return new PageInfo<Syncstatus>(pages);
        }

    2、mapper

    /**
     * @author zs
     * @date 2019/10/18 16:03
     */
    @Repository
    public interface YylfHttpServletMapper {
    
        //删除预约临分未到期数据
        void deleteZrjunearnedfac(Date startdate);
        //保存预约临分未到期数据
        void addZrjunearnedfac(List<Zrjunearnedfac> zrjunearnedfacList);
        //删除据提取信息
        void deleteSyncstatus(String uuid);
        //保存数据提取信息
        void insertSyncstatus(Syncstatus syncstatus);
        //查询数据同步信息
        List<Syncstatus> getSyncstatusList(@Param("vo")Syncstatus vo);
        //获取日志信息
        List<InterfaceLog> getYylfNewLog(@Param("uuid")String uuid, @Param("sendsystemcode")String sendsystemcode);
    
        //新增
        int insert(@Param("uuid")String uuid, @Param("systemcode")String systemcode, @Param("status")String status);
        int insert_duotiao(@Param("uuid")String uuid);
        String insert_duotiao_String(@Param("uuid")String uuid);
        //删除
        int delete(@Param("uuid")String uuid);
        int delete_systemcode(@Param("systemcode")String systemcode);
        //修改
        int update_no(@Param("uuid")String uuid);
        int update_duotiao_systemcode(@Param("systemcode")String systemcode);
        int update_fail(@Param("uuid")String uuid);
    
        //查询
        Object select(@Param("uuid")String uuid);
    
        String select_int(@Param("uuid")int uuid);
    
    
    }

    3、mapper.xml

    <insert id="insert" parameterType="java.util.Map">
        insert into aaa
          (uuid,systemcode,status)
        value
          (#{uuid,jdbcType=VARCHAR},#{systemcode,jdbcType=VARCHAR},#{status,jdbcType=VARCHAR})
      </insert>
    
      <insert id="insert_duotiao" parameterType="java.util.Map">
        insert into aaa
        (uuid,systemcode,status)
        value
        ('1','2','2'),('6','2','2')
      </insert>
    
      <insert id="insert_duotiao_String" parameterType="java.util.Map">
        insert into aaa
        (uuid,systemcode,status)
        value
        ('5','2','2'),('6','2','2')
      </insert>
    
      <delete id="delete">
        delete from aaa where uuid = #{uuid,jdbcType=VARCHAR}
      </delete>
    
      <delete id="delete_systemcode">
        delete from aaa where systemcode = #{systemcode,jdbcType=VARCHAR}
      </delete>
    
      <update id="update_no">
        UPDATE aaa SET status = '0' WHERE uuid = #{uuid,jdbcType=VARCHAR}
      </update>
      <update id="update_duotiao_systemcode">
        UPDATE aaa SET systemcode = '3' WHERE systemcode = #{systemcode,jdbcType=VARCHAR}
      </update>
      <update id="update_fail">
        UPDATE aaa SET uuid = '0' WHERE uuid = #{uuid,jdbcType=VARCHAR}
      </update>
    
      <select id="select">
        select uuid from aaa where uuid = '0'
      </select>
    
      <select id="select_int" resultType="String">
        select uuid from aaa where uuid = 0
      </select>
    
    <select id="getSyncstatusList" resultMap="syncstatusToRow">
        select  uuid,startdate as paydate ,date(createtimefordw) as syndate ,status,
        sum(case when tablename='zrjunearnedfac'   then zrjcount end) as zrjunearnedfacNum,
        sum(case when tablename='zrjoutstandingfac'   then zrjcount end) as zrjoutstandingfacNum
        from syncstatus where 1=1
        AND systemcode = 'YYLF'
        <if test="  vo.startdate != null and vo.enddate !=null "  >
          AND startdate between  #{vo.startdate} and #{vo.enddate}
        </if>
        <if test="vo.status != '' and vo.status != null "  >
          AND status =  #{vo.status}
        </if>
        group by uuid,startdate,createtimefordw,status
        order by startdate,date(createtimefordw) desc
      </select>

    4、sql语句

    -- 创建aaa表用来验证增删改查的返回值
    CREATE TABLE `reserve`.`aaa`  (
      `uuid` char(36) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `systemcode` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `status` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      
      PRIMARY KEY (`uuid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    -- 新增
    insert into aaa (uuid,systemcode,status)value ('1','2','2');
    insert into aaa (uuid,systemcode,status)value ('2','2','2');
    insert into aaa (uuid,systemcode,status)value ('3','2','2');
    insert into aaa (uuid,systemcode,status)value ('4','2','2');
    insert into aaa (uuid,systemcode,status)value ('5','2','2'),('6','2','2');
    
    -- 删除
    delete from aaa where uuid = '0';
    delete from aaa where uuid != '1';
    delete from aaa where uuid = '2';
    
    -- 修改
    UPDATE aaa SET systemcode = '3' WHERE uuid = '1';
    
    UPDATE aaa SET uuid = '0' WHERE uuid = '2';
    
    -- 查询
    select * from aaa;
    
    
    -- 创建bbb表用来关联aaa的uuid作外键
    CREATE TABLE `reserve`.`bbb`  (
      `uuid` char(36) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `systemcode` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `status` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      
      PRIMARY KEY (`uuid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    alter table bbb add constraint FK_T_POSITI_REFERENCE_T_COMPAN foreign key (uuid)references 
    aaa (uuid);
    
    insert into bbb (uuid,systemcode,status)value ('1','2','2');
    
    
    -- 创建ccc表用来验证插入的为null的数据
    CREATE TABLE `reserve`.`ccc`  (
      `uuid` char(36) CHARACTER SET utf8 COLLATE utf8_general_ci ,
      `systemcode` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci ,
      `status` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci 
      
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    insert into ccc (uuid,systemcode,status)value (NULL,NULL,NULL);
    
    select *from ccc;
    
    -- 创建ddd表用来验证查询结果为空时的返回值
    CREATE TABLE `reserve`.`ddd`  (
      `uuid` INT  ,
      `systemcode` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci ,
      `status` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci 
      
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    insert into ddd (uuid,systemcode,status)value (1,'2','2');
    
    select *from ddd;
    
     
    如果错过太阳时你流了泪,那你也要错过群星了。
    在所有的矛盾中,要优先解决主要矛盾,其他矛盾也就迎刃而解。
    不要做个笨蛋,为失去的郁郁寡欢,聪明的人,已经找到了解决问题的办法,或正在寻找。
  • 相关阅读:
    int和bigint的区别
    mysql 的存储引擎介绍
    js-DOM ~ 04. BOM:浏览器对象模型window. 、定时器、在线用户、祝愿墙、BOM的内置方法内置对象
    github初学者搭建自己的网站
    JS-DOM ~ 03. 子节点的操作、style.样式和属性、dom元素的创建方法及操作、14个例题、主要是利用js直接控制html属性
    JS-DOM ~ 02. 隐藏二维码、锁定、获取输入框焦点、for循环为文本赋值、筛选条件、全选和反选、属性的方法操作、节点的层次结构、nodeType
    JS-DOM ~ 01. 了解DOM,动手做一下就明白了!/鼠标事件(好吧 其实我卡了三天
    javascript 数组的部分常用属性用法
    javascript . 05 json的组成、for...in 遍历对象、简单数据类型与复杂数据类型的传值与传址、内置对象
    javascript . 04 匿名函数、递归、回调函数、对象、基于对象的javascript、状态和行为、New、This、构造函数/自定义对象、属性绑定、进制转换
  • 原文地址:https://www.cnblogs.com/szrs/p/15257883.html
Copyright © 2020-2023  润新知