实体映射文件:

----------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ibatis.dao.IStudentDAO">

 <select id="selectAllStudents" resultType="Student">

select*fromstudent

</select>

<selectid="selectStudentById"parameterType="int"resultType="Student">

select*

fromstudent

wheres_id=#{s_id}

</select>

<insertid="insertStudent"parameterType="Student">

insertintostudent(s_id,s_name,s_age)

values(#{s_id},#{s_name},#{s_age})

</insert>

<deleteid="deleteStudentById"parameterType="int">

deletefromstudent

wheres_id=#{s_id}

</delete>

<updateid="updateStudentById"parameterType="Student">

updatestudent

sets_name=#{s_name},

s_age=#{s_age}

wheres_id=#{s_id}

</update>

</mapper>

-------------------------------------------------

parameterType 传入参数类型

resultType 返回结果类型

-------------------------------------------------

调用:

public class IStudentDAOIbatisImpl implements IStudentDAO {

 SqlSession sqlSession = null;

staticSqlSessionFactorysf;

static{

Readerreader=null;

try{

reader=Resources.getResourceAsReader("com/ibatis/SqlMapConfig.xml");

}catch(IOExceptione){

e.printStackTrace();

}

sf=newSqlSessionFactoryBuilder().build(reader);

}

publicvoidaddStudent(Studentstu){

try{

sqlSession=sf.openSession();

sqlSession.insert("insertStudent",stu);

sqlSession.commit();//这里一定要提交,不然数据进不去数据库中

}finally{

sqlSession.close();

}

 }

 public void addStudentBySequence(Student stu) { }

 public void deleteStudentById(int id) {

try{

sqlSession=sf.openSession();

intreturnValue=sqlSession.delete("deleteStudentById",id);

System.out.println(returnValue);//受影响行数

sqlSession.commit();//这里一定要提交,不然数据进不去数据库中

}finally{

sqlSession.close();

}

 }

 public List<Student> queryAllStudents() {

List<Student>students;

try{

sqlSession=sf.openSession();

students=sqlSession.selectList("selectAllStudents");

}finally{

sqlSession.close();

}

returnstudents;

 }

 public Student queryStudentById(int id) {

Studentstu;

try{

sqlSession=sf.openSession();

stu=(Student)sqlSession.selectOne("selectStudentById",id);

}finally{

sqlSession.close();

}

returnstu;

 }

 public List<Student> queryStudentsByName(String name) {

//TODOAuto-generatedmethodstub

returnnull;

 }

 public void updateStudent(Student stu) {

try{

sqlSession=sf.openSession();

intreturnValue=sqlSession.update("updateStudentById",stu);

sqlSession.commit();

System.out.println(returnValue);

}finally{

sqlSession.close();

}

 }

}