zoukankan      html  css  js  c++  java
  • Java MyBatis3(6)参数传递

    一、单个参数

     StudentParamsMapper

    package cn.cnki.ref.mapper;
    
    import cn.cnki.ref.pojo.Student;
    
    public interface  StudentParamsMapper {
        /**
         * 根据name查询
         * @param name
         * @return
         */
        public Student getByName(String name);
    }
    View Code

    StudentParamsMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper">
    
        <!-- 根据用户名和id同时查询 -->
        <select id="getStudentByIdAndName" resultType="cn.cnki.ref.pojo.Student">
          select * from student where  id=#{param1}  and  name=#{param2}
        </select>
    
    </mapper>
    View Code

    StudentParamsController

    package cn.cnki.ref.controller;
    
    import cn.cnki.ref.mapper.StudentParamsMapper;
    import cn.cnki.ref.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    public interface StudentParamsController {
        @RestController
        public class StudentParamsMapper {
            @Autowired
            private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
            @GetMapping("/studentparams/{name}")
            public Student selectCourseById(@PathVariable("name") String name) {
                Student student = StudentParamsMapper.getByName(name);
                return student;
            }
        }
    }
    View Code

     测试

    http://localhost:8080/studentparams/王五

    二、多个参数

    1.根据参数key值获取,获取规则为param1,param2,param3.........:

    StudentParamsMapper

    package cn.cnki.ref.mapper;
    
    import cn.cnki.ref.pojo.Student;
    
    public interface  StudentParamsMapper {
        /**
         * 根据用户名和id同时查询
         * @param id
         * @param name
         * @return
         */
        public Student getStudentByIdAndName(Integer id,String name);
    }
    View Code

    StudentParamsMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper">
    
        <!-- 根据用户名和id同时查询 -->
        <select id="getStudentByIdAndName" resultType="cn.cnki.ref.pojo.Student">
        select * from student where  id=#{0}  and  name=#{1}
        </select>
    
    </mapper>
    View Code

    StudentParamsController

    package cn.cnki.ref.controller;
    
    import cn.cnki.ref.mapper.StudentParamsMapper;
    import cn.cnki.ref.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    public interface StudentParamsController {
        @RestController
        public class StudentParamsMapper {
    
            @RequestMapping("/getStudentByIdAndName")
            public Student getStudentByIdAndName(@RequestParam("id") Integer id, @RequestParam("name") String name) {
                Student student = StudentParamsMapper.getStudentByIdAndName(id,name);
                return student;
            }
            
        }
    }
    View Code

    测试

    http://localhost:8080/getStudentByIdAndName?id=1&name=张三

    2.绑定参数名

    StudentParamsMapper

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper">
    
        <!-- 根据用户名和id同时查询 -->
        <select id="getStudentByIdAndNameParam" resultType="cn.cnki.ref.pojo.Student">
            select * from student where name=#{name} and id=#{id}
        </select>
    
    </mapper>
    View Code

    StudentParamsMapper.xml

    package cn.cnki.ref.mapper;
    
    import cn.cnki.ref.pojo.Student;
    import org.apache.ibatis.annotations.Param;
    
    public interface  StudentParamsMapper {
    
        /**
         * 根据用户名和id同时查询
         * @param id
         * @param name
         * @return
         */
        public Student getStudentByIdAndNameParam(@Param("id")Integer id, @Param("name")String name);
    
    }
    View Code

    StudentParamsController

    package cn.cnki.ref.controller;
    
    import cn.cnki.ref.mapper.StudentParamsMapper;
    import cn.cnki.ref.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    public interface StudentParamsController {
        @RestController
        public class StudentParamsMapper {
            @Autowired
            private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
            @RequestMapping("/getStudentByIdAndNameParam")
            public Student getStudentByIdAndNameParam(@RequestParam("id") Integer id, @RequestParam("name") String name) {
                Student student = StudentParamsMapper.getStudentByIdAndName(id,name);
                return student;
            }
    
        }
    }
    View Code

    测试

    http://localhost:8080/getStudentByIdAndNameParam?id=1&name=张三

    3.封装实体参数

    StudentParamsMapper

    package cn.cnki.ref.mapper;
    
    import cn.cnki.ref.pojo.Student;
    import org.apache.ibatis.annotations.Param;
    
    public interface  StudentParamsMapper {
    
        /**
         * 根据用户名和id同时查询
         * @param id
         * @param name
         * @return
         */
        public Student getStudentByIdAndNameByObjectParam(Student student);
        
    }
    View Code

    StudentParamsMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper">
    
        <!-- 根据用户名和id同时查询 -->
        <select id="getStudentByIdAndNameByObjectParam" resultType="cn.cnki.ref.pojo.Student">
            select * from student where name=#{name} and id=#{id}
        </select>
        
    </mapper>
    View Code

    StudentParamsController

    package cn.cnki.ref.controller;
    
    import cn.cnki.ref.mapper.StudentParamsMapper;
    import cn.cnki.ref.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    public interface StudentParamsController {
        @RestController
        public class StudentParamsMapper {
            @Autowired
            private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
            @RequestMapping("/getStudentByIdAndNameByObjectParam")
            public Student getStudentByIdAndNameByObjectParam(@RequestParam("id") Integer id, @RequestParam("name") String name) {
                Student student = new Student();
                student.setName(name);
                student.setId(id);
                Student studentQuery = StudentParamsMapper.getStudentByIdAndNameByObjectParam(student);
                return student;
            }
    
        }
    }
    View Code

    测试

    http://localhost:8080/getStudentByIdAndNameByObjectParam?id=1&name=张三

  • 相关阅读:
    .net core consul
    numpy
    Cordova各个插件使用介绍系列(七)—$cordovaStatusbar手机状态栏显示
    ionic 的缓存 和局部刷新
    ionic 项目中添加modal的步骤流程
    ionic 项目中创建侧边栏的具体流程分4步简单学会
    Cordova各个插件使用介绍系列(八)—$cordovaCamera筛选手机图库图片并显示
    python-16: time 模块 之一
    python-16:模块 包
    c-3:位运算:位运算基本用法
  • 原文地址:https://www.cnblogs.com/cnki/p/9343171.html
Copyright © 2011-2022 走看看