MyBatis-07 更新数据


添加映射

<?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="com.cd.StudentMapper">

  <update id="updateStudent">
   update stu_tbl set name=#{name}, age=#{age} where id=#{id}
  </update>

</mapper>

更新

package com.cd;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {
    public static void main(String[] args) {
        String resource = "com/cd/mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = (InputStream) Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            Student stu = new Student();
            stu.setId(2);
            stu.setName("big tom");
            stu.setAge(100);

            session.update("com.cd.StudentMapper.updateStudent", stu);
            session.commit();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


原文链接:http://codingdict.com/