mybatis使用接口实现dao层

1.工具类与jar包在前面已经介绍,不累述。

2.在项目中新建dao包,包含sqltools接口及其实现类

3.sqltools接口:

package dao;
import com.gyf.model.student;
public interface sqltools {
    //对数据库插入数据
    public void  save(student student);
    //查找
    public student findUserId(int id);
    //删除
    public void deleteUser(int id);
    //更新
    public void updateStudent(student student);
}

4.sqltools接口实现类:

package dao;

import com.gyf.model.student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class sqltoolsImp  implements sqltools{
    private SqlSessionFactory ssf;

    public sqltoolsImp(SqlSessionFactory ssf) {
        this.ssf = ssf;
    }


    @Override
    public void save(student student) {
        SqlSession session=ssf.openSession();
        session.insert("insertData",student);
        session.commit();
        session.close();
    }

    @Override
    public student findUserId(int id) {
        SqlSession session=ssf.openSession();
        student student=session.selectOne("selectStudent",id);
        session.close();
        return student;

    }

    @Override
    public void deleteUser(int id) {
        SqlSession session=ssf.openSession();
        session.delete("deleteData",id);
        session.close();

    }

    @Override
    public void updateStudent(student student) {
        SqlSession session=ssf.openSession();
        session.update("updateData",student);
        session.commit();
        session.close();

    }
}

4.数据库映射文件

.




    



    
    
      SELECT LAST_INSERT_ID()
    
        insert into students (sno,name,age,school,pass)
        value (#{sno},#{name},#{age},#{school},#{pass});
    
    
        delete from students where id =#{id}
    
    
    update students set  sno=#{sno},name=#{name},age=#{age},school=#{school},pass=#{pass}
    where id=#{id};
    

5.





    
        
        
            
            
            
            
        
    




5.测试类

package test;
import com.gyf.model.student;
import dao.sqltools;
import dao.sqltoolsImp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
public class Demo{
    SqlSessionFactory sessionFactory;
    @Before
    public void before() throws IOException
    {
        InputStream is= Resources.getResourceAsStream("SqlMapConfig.xml");
         sessionFactory=new SqlSessionFactoryBuilder().build(is);

    }
    @Test
    public void Test()
    {
        sqltools sqltools=new sqltoolsImp(sessionFactory);
        student student=sqltools.findUserId(32760);
        System.out.println(student);
        student student3=new student("256","李四",25,"eggr","255");
        sqltools.save(student3);
        sqltools.deleteUser(32762);
        sqltools sqltools=new sqltoolsImp(sessionFactory);
        student student4=new student("256","张四",25,"eggr","255");
        student4.setId(32762);
        sqltools.updateStudent(student4);
    }



}

6.结论:

本次文档只为介绍mybatis入门,其功能虽然已经完善,但技术早已更新,

只为介绍面向接口编程的思想,希望能帮助读者。

你可能感兴趣的:(mybatis)