StudentDao接口
package com.qfedu.dao;
import com.qfedu.pojo.Student;
public interface StudentDao {
public int insertStudent(Student student);
public int deleteStudent(String stuNum);
public int updateStudent(Student student);
}
Student类
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
private int stuId;
private String stuNum;
private String stuName;
private String stuGender;
private int stuAge;
}
StudentMapper.xml
insert into tb_students(sid,stu_num,stu_name,stu_gender,stu_age) values (#{stuId},#{stuNum},#{stuName},#{stuGender},#{stuAge}) delete from tb_students where stu_num=#{stuNum} update tb_students set stu_name=#{stuName}, stu_gender=#{stuGender}, stu_age=#{stuAge} where stu_num=#{stuNum}
以上id要和StudentDao中接口对应的方法名一致。
同样,StudentDao中的方法写了参数是Student,以上代码中的parameterTyper可以不写
StudentDaoTest类
public class StudentDaoTest {
@Test
public void updateStudent(){
try {
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSession sqlSession=builder.build(is).openSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
studentDao.updateStudent(new Student(0,"00001","zhangsna","女",21));
//这里我们要改掉以前控制台println来验证程序的方法,转而用assertEquals。因为print方式还要人为的去判断结果,反之则一步到位。
assertEquals(0,i);
sqlSession.commit();
System.out.println(i);
} catch (IOException e) {
e.printStackTrace();
}
}
}
查询-查所有
1.StudentDao接口
public interface StudentDao {
public int insertStudent(Student student);
public int deleteStudent(String stuNum);
public int updateStudent(Student student);
public List listStudents();
}
查所有的结果并不只有一条记录,且每一条对应一个Student,所以我们采用list集合。
2.StudentMapper.xml
方法一:取别名
- resultType指定查询结果封装的实体类
-
resultSets="java.util.list"指定当前操作返回的集合类型(可省略)
由于数据库的字段和java中对象的属性命名等有出入,所以并不能使用select * 的方式查寻,要列出字段并指定别名来匹配属性。
方法二:resultMap定义映射关系
resultMap是可以取任何名字,当然,建议取有意义的名字。
我们建议用第二中方式,因为resultMap可以多次使用。
3.测试类
public class StudentDaoTest {
@Test
public void testListStudents(){
try {
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSession sqlSession=builder.build(is).openSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
List list = studentDao.listStudents();
//assertNotNull(list);
for (Student student:list) {
System.out.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
一般我们在测试类名称后标注Test,在测试方法前表注test,前面的代码忘记标注了。
查询-根据学号1.StudentDao接口
public interface StudentDao {
public int insertStudent(Student student);
public int deleteStudent(String stuNum);
public int updateStudent(Student student);
public List listStudents();
public Student queryStudent(String stuNum);
}
2.StudentMapper.xml
3.单元测试
public class StudentDaoTest{
@Test
public void testQueryStudent(){
try {
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSession sqlSession=new SqlSessionFactoryBuilder().build(is).openSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
Student student=studentDao.queryStudent("00004");
System.out.println(student);
} catch (IOException e) {
e.printStackTrace();
}
}
}
查询-分页查询(多参数查询)
在MyBatis进行操作:
- 如果操作方法只有一个简单类型或者字符串类型的参数,在Mapper配置中可以通过 #{str}直接获取。
- 如果操作方法有一个对象类型的参数,在Mapper配置中可以直接通过#{attrName}获取对象的指定属性值(attrName必须是参数对象的属性)
- 如果操作方法有一个Map类型的参数,在Mapper配置中可以直接通过#{key}获取key对应的value
- 如果操作方法有多个参数,该如何处理?



