1 mapper的配置文件
<?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="org.mybatis.example.mapper.StudentMapper">
<select id="selectByName" resultType="org.mybatis.example.bean.Student">
select * from student where sname = #{name,jdbcType=VARCHAR} limit 1
</select>
<insert id="insertStudent">
insert into student(sname,sage,ssex) values(#{sname},#{sage},#{ssex})
</insert>
<delete id="deleteStudent">
delete from student where sname = #{name, jdbcType=VARCHAR}
</delete>
<update id="updateStudent">
update student set sname = #{sname},sage = #{sage}, ssex=#{ssex}
where sid = #{sid}
</update>
</mapper>
2 mapper对应的接口文件
package org.mybatis.example.mapper;
import org.mybatis.example.bean.Student;
public interface StudentMapper {
Student selectByName(String name);
void insertStudent(Student student);
void deleteStudent(String name);
void updateStudent(Student student);
}
3 单元测试
package org.mybatis.example;
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.BeforeClass;
import org.junit.Test;
import org.mybatis.example.bean.Student;
import org.mybatis.example.mapper.StudentMapper;
import java.io.InputStream;
public class StudentTest {
static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void beforeClass() throws Exception {
String config = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void selectStudentByName() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectByName("joker");
System.out.println(student);//Student{sid=1, sname='joker', sage=18, ssex='男'}
sqlSession.close();
}
@Test
public void insertStudent() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student();
student.setSname("tom");
student.setSage(20);
student.setSsex("男");
studentMapper.insertStudent(student);
System.out.println(studentMapper.selectByName("tom"));//Student{sid=2, sname='tom', sage=20, ssex='男'}
sqlSession.close();
}
@Test
public void updateStudent() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectByName("tom");
System.out.println(student);//Student{sid=2, sname='tom', sage=20, ssex='男'}
student.setSage(30);
studentMapper.updateStudent(student);
System.out.println(student);//Student{sid=2, sname='tom', sage=30, ssex='男'}
sqlSession.close();
}
@Test
public void deleteStudent() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
studentMapper.deleteStudent("tom");
System.out.println(studentMapper.selectByName("tom"));//null
sqlSession.close();
}
}