MyBatis-实现接口执行sql语句

以前都是通过写一个id 调用SqlSession方法,跟接口没什么关系,跟现在开发模式是不相符的,以下通过接口的实现类调用它的某个方法,执行sql语句:

创建StudentDaoImpl类,实现StudentDao接口:

package com.bjpowernode.dao.impl;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
/*
以前都是通过写一个id 调用SqlSession方法,跟接口没什么关系,跟现在开发模式是不相符的,以下通过接口的实现类调用它的某个方法,执行sql语句
 */

public class StudentDaoImpl  implements StudentDao {

    @Override
    public Student selectById(Integer id) {
        //获取SqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        String sqlId="com.bjpowernode.dao.StudentDao.selectById";
        //调用SqlSession的方法
        Student student = sqlSession.selectOne(sqlId,id);
        //关闭sqlSession对象
        sqlSession.close();
        return student;
    }

    @Override
    public List<Student> selectStudents() {
        //获取SqlSession
        SqlSession session= MybatisUtil.getSqlSession();
        //2.指定sqlId
        String sqlId="com.bjpowernode.dao.StudentDao.selectStudents";
        //3.执行sqlSession的方法,执行sql语句
        List<Student> students = session.selectList(sqlId);
        //4.关闭SqlSession对象
        session.close();

        return students;
    }

    @Override
    public int insertStudent(Student student) {
        //获取SqlSession
        SqlSession session= MybatisUtil.getSqlSession();
        //2.指定sqlId
        String sqlId="com.bjpowernode.dao.StudentDao.insertStudent";
        //3.执行sqlSession的方法,执行sql语句
        int rows = session.insert(sqlId, student);
        session.commit();

        System.out.println("insert的行数==="+rows);
        //4.关闭SqlSession对象
        session.close();

        return rows;
    }
}

测试类:MyTest2类:

package com.bjpowernode;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.dao.impl.StudentDaoImpl;
import com.bjpowernode.domain.Student;
import org.junit.Test;

import java.util.List;

public class MyTest2 {
    //查询一个学生
    @Test
    public void testSelectOne(){
        //接口类型  变量=new 接口实现类();
        StudentDao dao=new StudentDaoImpl();
        //使用dao的方法
        Student student=dao.selectById(1001);
        System.out.println("通过dao执行的方法,得到的对象="+student);

    }
    //查询所有
    @Test
    public void testSelectStudents(){
        //接口类型  变量=new 接口实现类();
        StudentDao dao=new StudentDaoImpl();
        //使用dao的方法
        List<Student> students=dao.selectStudents();
        students.forEach(stu-> System.out.println("stu="+stu));

    }
    //添加学生
    @Test
    public void testInserttudents(){
        //接口类型  变量=new 接口实现类();
        StudentDao dao=new StudentDaoImpl();
        //使用dao的方法

        Student student=new Student();
        student.setId(1009);
        student.setName("周强");
        student.setEmail("zhouqiang@.qq.com");
        student.setAge(20);

        dao.insertStudent(student);

    }
}

第一个测试结果:

第二个测试结果:

 

第三个测试结果:

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值