开发者

MyBatis传入多个参数时parameterType的写法

目录
  • MyBATis传入多个参数时parameterType写法
    • 方法1:对象
    • 方法2:Map
    • 方法3:@Param()
  • MyBatis传入多个参数 批量更新
    • Service调用
    • Mapper 方法
  • 总结

    MyBatis传入多个参数时parameterType写法

    方法1:对象

    1.保证类里有构造函数

    public Student(Integer SID, String sname, String ssex, Integer sage) {
        this.SID = SID;
        Swww.devze.comname = sname;
        Ssex = ssex;
        Sage = sage;
      }

    2.接口里方法传对象

    public int insertStudent(Student student);

    3.Student Mapper.XML里

    <insert id="insertStudent" parameterType="com.tulun.maventest.pojo.Student" >
        insert into student(SID, Sname, Sage, Ssex) values (#{SID}, #{Sname} ,#{Sage}, #{Ssex} )
      </insert>

    4.MyBatisDemo的insertStudent() 里

    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
     Student student=new Student(10,"小明","男",24);
     System.out.println(mapper.insertStudent(student));
     sqlSession.commit();//事务

    方法2:Map

    开发者_JAVA入门

    原理是将参数放到HashMap里,传一个Map对象,通过键值对的形式获取

    1.xml文件

    <update id="updateSnameById" parameterType="Map" >
        update Student set sname = #{sname} where sid = #{sid}
      </update>

    2.接口

    public int updateSnameById(Map map);

    3.test.Java里

    Map<String,String> map=new HashMap<String,String>();
            map.put("sid","4");
            map.put("sname","刘能");
            System.out.println(mapper.updateSnameById(map)); 
            sqlSession.commit();//

    方法3:@Param()

    在接口里面该方法的参数前面加注解

    http://www.devze.com

    1.接口

    public int updateSnameById(@Parampython(value="sid")Integer sid,@Param(value="sname") String sname);

    2.xml文件

    <update id="updateSnameById" parameterType="com.tulun.maventest.pojo.Student" >
        update Student set sname = #{sname} where sid = #{sid}
      </update>

    3.test.java

    System.out.println(mapper.updateSnameById(4, "刘心晶"));
    sqlSession.commit();//事务

    MyBatis传入多个参数 批量更新

    Service调用

    Map<String, Object> params = new HashMap<>();
    List<Long> ids = new ArrayList&lpythont;>();
    Long donwCnt = 20L;
    params.put("ids", ids);
    params.put("downCnt", donwCnt);
    boolean result = userMapper.batchUpdateByIds(params);

    Mapper 方法

    boolean batchUpdateByIds(Map<String, Object> params) throws Exception;

    Xml内容

    <update id="batchUpdateByIds" parameterType="java.util.Map" >
      update user
      <set>
        down_cnt = #{downCnt}
        where id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")" >
          #{item}
        </foreach>
      </set>
    </pythonupdate>

     

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜