代码如下:
1、mybatis配置文件
2、StudentMapper.java类
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 ListfindAll(); @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);}
3、测试
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); Liststudents = 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(); } }}