代碼以下: java
一、mybatis配置文件 apache
<mappers> <mapper class="org.zsm.myBatis.day02.inner.IStudentOperation"/> </mappers>
二、StudentMapper.java類 session
package org.zsm.myBatis.day02.inner; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.zsm.myBatis.day02.domain.Student; public interface IStudentOperation { @Select("select * from student where id = #{id}") public Student getById(int id); @Select("select * from student") public List<Student> findAll(); @Insert("insert into student(name,gender,major,grade) values(#{name},#{gender},#{major},#{grade})") public void insertStudent(Student student); @Update("update student set name=#{name},gender=#{gender},major=#{major},grade=#{grade} where id = #{id}") public void updateStudent(Student student); @Delete("delete from student where id = #{id}") public void deleteStudent(int id); }
三、測試 mybatis
package org.zsm.myBatis.day02.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.zsm.myBatis.day02.domain.Student; import org.zsm.myBatis.day02.inner.IStudentOperation; import org.zsm.myBatis.day02.util.SqlSessionFactoryUtil; public class Test02 { private SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory(); /** * 測試新增 */ @Test public void insert() { SqlSession session = null; try { Student student = new Student(); student.setName("王剛"); student.setGender("男"); student.setMajor("軟件開發"); student.setGrade("2010"); session = factory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); studentOperation.insertStudent(student); session.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } } /** * 查詢單條記錄 */ @Test public void getById(){ SqlSession session = null; try { session = factory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); Student student = studentOperation.getById(1); System.out.println(student.getName()+" "+student.getGender()+" "+student.getMajor()+" "+student.getGrade()); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } } /** * 查詢全部記錄 */ @Test public void findAll(){ SqlSession session = null; try { session = factory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); List<Student> students = studentOperation.findAll(); System.out.println(students.size()); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } } /** * 更新記錄 */ @Test public void update(){ SqlSession session = null; try { session = factory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); Student student = studentOperation.getById(5); student.setName("王剛剛"); studentOperation.updateStudent(student); session.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } } /** * 刪除記錄 */ @Test public void delete(){ SqlSession session = null; try { session = factory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); studentOperation.deleteStudent(5); session.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } } }