zoukankan      html  css  js  c++  java
  • Spring Boot与Mybatis数据交互

      之前用的SSH框架用习惯了后,现在用SSM框架感觉把以前的好多东西都给忘了,就说这个业务层和数据库的数据交互这块,这几天不停地再网上恶补了一些。

    一、顺序传参法 

    Mapper层: 
    传入需要的参数

    public interface GoodsMapper {
    
        public Goods selectBy(String name,int num);         
    }
    

      Mapper.xml: 
    *使用这种方式,在SpringBoot中,参数占位符用#{arg0},#{arg1},#{arg…} 

    总结:这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错。

    二、@Param注解传参法 
    Mapper层:

    public interface GoodsMapper {
    
        public Goods selectBy(@Param("name")String name,@Param("num")int num);
    
    }
    

      Mapper.xml: 
    *#{}里面的名称对应的是注解@Param括号里面修饰的名称。

    <select id="selectBy" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from goods
        where good_name=#{name} and good_num=#{num}
    </select>
    

      总结:这种方法在参数不多的情况还是比较直观的,推荐使用。

    三、使用Map封装参数 

    Mapper层: 
    将要传入的多个参数放到Map集合

    Mapper.xml: 
    *#{}里面的名称对应的是Map里面的key名称

    总结:这种方法适合传递多个参数,且参数易变能灵活传递的情况。

    Service层: 
    带上要传入的参数

    Service接口实现层: 
    封装Map集合:

    控制层

    public R forbidden(@RequestParam Map<String, Object> params) {
    		// 把修改时间添加上
    		Map<String, Object> map1 = new HashMap<String, Object>();
    		String param = params.get("params").toString();
    		map1 = JSON.parseObject(param);
    		String wxNumber = map1.get("wxNumber") == null ? null : map1.get("wxNumber").toString();
    		String isForbidden = map1.get("isForbidden") == null ? null : map1.get("isForbidden").toString();
    		ManagementMainService.getWxNumberUpdateForbidden(isForbidden, new Date(), wxNumber);
    
    		return R.ok("成功");
    	}
    

      

  • 相关阅读:
    .net core 学习小结之 配置介绍(config)以及热更新
    .net core 学习小结之环境配置篇
    powershell下载网站图片
    Powershell 脚本输出前十条消耗内存的进程到excel
    Linux 自学shell
    使用bat脚本进行开机启动批处理
    Git 创建分支并合并主分支
    Git 的使用及其一些基本用法
    点击按钮复制文本到剪切板
    关于一些基本排序的实现
  • 原文地址:https://www.cnblogs.com/zmmfeng/p/11280578.html
Copyright © 2011-2022 走看看