2 - 基本CURD

dao

package dao;

import model.Student;

import java.util.List;

public interface StudentMapper {

    List selectAllStudent();

    int insertStudent(Student student);

    int updateStudent(Student student);

    int deleteById(int id);
}

mapple.xml





    

    
      insert into `student`(`name`,`age`)
      values(#{name},#{age})
      
          select last_insert_id();
      
    

    
        update `student` set `name` = #{name},`age` = #{age}
        where `id` = #{id}
    

    
        delete from `student` where `id` = #{id}
    

测试

import dao.StudentMapper;
import model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public class AppTest {

    SqlSessionFactory factory = null;

    @Before
    public void prepare() throws IOException{
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
        inputStream.close();
    }

    @Test
    public void test(){

        SqlSession session=  factory.openSession();
        try{
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);

            List list = studentMapper.selectAllStudent();
            Iterator iterator = list.iterator();
            while(iterator.hasNext()){
                Student student = iterator.next();
                System.out.println(student);
            }

        }finally{
            session.commit();
            session.close();
        }
    }

    @Test
    public void testInsert(){
        SqlSession sqlSession = factory.openSession();
        try{
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = new Student("young",20);
            int affectedRows = studentMapper.insertStudent(student);
            if(affectedRows==1){
                System.out.println("插入成功!");
                System.out.println(student);//查看从数据库返回id填充student的id属性
            }else{
                System.out.println("插入失败!");
            }
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
    }
    @Test
    public void testUpdate(){
        SqlSession sqlSession = factory.openSession();
        try{
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = new Student(10000,"Tom",21);
            int affectedRows = studentMapper.updateStudent(student);
            if(affectedRows != 0){
                System.out.println("更新成功!");
                System.out.println(student);
            }else{
                System.out.println("更新失败");
            }
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Test
    public void testDelete(){
        SqlSession sqlSession = factory.openSession();
        try{
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            int id  = 10000;
            int affectedRows  =  studentMapper.deleteById(id);
            if(affectedRows == 1){
                System.out.println("删除成功!");
            }else{
                System.out.println("删除失败!");
            }
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
    }

}

注意

  1. 插入、删除、修改要获取数据库中受影响的记录数,以便验证操作是否成功
  2. 插入一条数据最好返回从数据库返回生成的ID来给model的id赋值,以便后续使用

你可能感兴趣的:(2 - 基本CURD)