MyBatis单表增删改查--接口实现
我是用xml实现了单表的增删改查之后才改用接口的,所以我这里就不把那些配置拷贝过来了,其实东西都一样。
1:接口
这个更有意思了。把具体的操作写在了注解里面。
package com.zhao.mapper; 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 com.zhao.entity.Student; public interface StudentMapper { @Select(value="select * from student") public List<Student> queryStudent(); @Select(value="select * from student where stu_id=#{stu_id}") public Student selectStudentById(int stu_id); @Delete(value="delete from student where stu_id=#{stu_id}") public void deleteStudentById(int stu_id); @Insert(value="insert into student(stu_name,stu_gender) values(#{stu_name},#{stu_gender})") public void insertStudent(Student student); @Update(value="update student set stu_name=#{stu_name},stu_gender=#{stu_gender} where stu_id=#{stu_id}") public void updateStudentById(Student student); }
2:测试代码
依旧是增删改查,接口实现是session获取接口对象,然后调用接口相应的方法。至于底层的东西 mybatis就帮我们解决了,我们不需要在这里用具体的实现类,具体的实现类也不好用。这样多方法,只写法方法签名,然后把数据表的操作写注解了,直接用就可以了。
package com.zhao.mapper; import static org.junit.Assert.*; import java.io.InputStream; import java.util.List; 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 com.zhao.entity.Student; import com.zhao.entity.StudentTest; public class StudentMapperTest { private SqlSessionFactory factory; @Before public void before() { try { /* * 1: Reader reader * =Resources.getResourceAsReader("Configuration.xml"); factory = * new SqlSessionFactoryBuilder().build(reader); */ // 2: InputStream inputStream = StudentTest.class.getClassLoader().getResourceAsStream("Configuration.xml"); factory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } @Test public void testQueryStudent() { SqlSession session = factory.openSession(); try { StudentMapper mapper = session.getMapper(StudentMapper.class); List<Student> students = mapper.queryStudent(); for (Student student : students) { System.out.println(student); } session.commit(); } finally { session.close(); } } @Test public void testSelectStudentById() { SqlSession session = factory.openSession(); try { StudentMapper mapper = session.getMapper(StudentMapper.class); Student student = mapper.selectStudentById(2); System.out.println(student); session.commit(); } finally { session.close(); } } @Test public void testDeleteStudentById() { SqlSession session = factory.openSession(); try { StudentMapper mapper = session.getMapper(StudentMapper.class); mapper.deleteStudentById(2); session.commit(); } finally { session.close(); } } @Test public void testInsertStudent() { SqlSession session = factory.openSession(); try { StudentMapper mapper = session.getMapper(StudentMapper.class); Student student = new Student("zaza", "男"); mapper.insertStudent(student); session.commit(); } finally { session.close(); } } @Test public void testUpdateStudentById() { SqlSession session = factory.openSession(true); try { StudentMapper mapper = session.getMapper(StudentMapper.class); Student student = new Student(7,"zaza", "m"); mapper.updateStudentById(student); session.commit(); } finally { session.close(); } } }