Mybatis

Mybatis,第1张

Mybatis 修改 *** 作

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

      方法一:取别名


        select sid,stu_num,stu_name,stu_gender,stu_age
        from tb_students
    

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进行 *** 作:

  1. 如果 *** 作方法只有一个简单类型或者字符串类型的参数,在Mapper配置中可以通过 #{str}直接获取。
  2. 如果 *** 作方法有一个对象类型的参数,在Mapper配置中可以直接通过#{attrName}获取对象的指定属性值(attrName必须是参数对象的属性)
  3. 如果 *** 作方法有一个Map类型的参数,在Mapper配置中可以直接通过#{key}获取key对应的value
  4. 如果 *** 作方法有多个参数,该如何处理?

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5683701.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存