CRUD操作
crud是指在做计算处理时的增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。crud主要被用在描述软件系统中数据库或者持久层的基本操作功能。
CRUD
注意几点:🔻
SQL语句返回值类型。【完整的类名或者别名】
传入SQL语句的参数类型 。【万能的Map,可以多尝试使用】
命名空间中唯一的标识符
接口中的方法名与映射文件中的SQL语句ID 一一对应
id 就是对应namespace中的方法名
parameterType 参数类型
resultType sql语句执行的返回值类型
增删改都需要提交事务
Select
信息全查
在映射器接口中添加对应的方法:
public List<Student> GetAll();
在xml映射器中绑定该方法,添加sql语句:
<select id="GetAll" resultType="Student">
select * from student
</select>
测试:
@Test
public void GetAll(){
SqlSession sqlSession = MyBatisUtils.GetSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> list = studentMapper.GetAll();
for (Student student : list){
System.out.println(student);
}
sqlSession.close();
}
根据ID查学生
在映射器接口中添加对应的方法:
public Student GetId(int id);
在xml映射器中绑定该方法,添加sql语句:
<select id="GetId" resultType="Student" parameterType="int">
select * from student where stu_i