栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-初学者2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-初学者2

一. MyBatis 的CRUD操作 1:添加操作

略(在1中末尾有体现)

2:删除操作

根据学号删除一条学生信息
步骤:
在StudentDAO中定义删除方法

在StudentMapper.xml中对接口方法进行"实现"(插件可以生成)

测试:在StudentDAO的测试类中添加测试方法

  try {

            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //SqlSessionFactory表示mybatis的会话工厂,工厂模式
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
            //SqlSession 对象是mybatis和数据库之间的连接,也就是会话,创建了连接,可以得到所有的mapper对象(映射关系)
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //通过SqlSession 对象调用getMapper方法获取DAO接口对象
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
            //调用被测试方法
            int i = studentDAO.deleteStudent("10001");
            //提交事务
            sqlSession.commit();
            System.out.println(i);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.修改操作

根据学号,修改其他字段信息


        update tb_students
        set
            stu_name = #{stuName},
            stu_gender = #{stuGender},
            stu_age = #{stuAge}
        where
            stu_num = #{stuNum}
    
@Test
    public void  testUpdateStudent(){
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
            int i = studentDAO.updateStudent(new Student(2,"10002","can","男",18));
            sqlSession.commit();
            assertEquals(1,i);//期望,单元测试的期望返回结果
            System.out.println(i);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

assertEquals(1,i)
如果两者一致, 程序继续往下运行.
如果两者不一致, 中断测试方法, 抛出异常信息 AssertionFailedError .

4.查询操作-查询所有
    在studentDAO接口中定义方法,返回多条,所以用List集合来装
    在studentMapper文件中写
    下面为两种实现方案:法一取别名,法二用resultMap















    
    
    
    
    




            select sid,
                   stu_num,
                   stu_name,
                   stu_gender,
                   stu_age
            from tb_students
                    limit #{start}, #{pageSize}
            
            

        

—注意:如果DAO操作方法,没有通过@Param指定参数别名,在SQL中也可以通过MyBatis自带的arg0 ,arg1… 或者 param1,param2…获取参数

单元测试

 @Test
    public void testlistStudentsByPage() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);

//            HashMap map = new HashMap();
//            map.put("start",0);
//            map.put("pageSize",2);
//            List studentList = studentDAO.listStudentsByPage(map);

            List studentList = studentDAO.listStudentsByPage(0,2);

            assertNotNull(studentList);
            sqlSession.commit();
            for (Student student : studentList
            ) {
                System.out.println(student);
            }
        } catch (IOException e) {

        }
    }
7.查询操作-查询总记录数

StudentDAO

 public int getCount();

在StudentMapper.xml配置sql时,使用 resultType 指定当前操作的返回类型为int(插件自动生成java.lang.Integer也没问题)