Mybatis增删改查总结(注解与配置文件两种方式)

前提:Mybatis环境搭建完成

直接上代码吧(我这里是配置文件方式来实现的):

一、配置文件方式:

数据库表格:

Mybatis增删改查总结(注解与配置文件两种方式)_第1张图片

StudentDao.xml:



 
    
    

    
    
        insert into student(name,sex,birthday) values(#{name},#{sex},#{birthday});
    

    
    
        update student set name = #{name},sex = #{sex},birthday=#{birthday} where id=#{id};
    

    
    
        delete from student where id = #{sid};
    

    
    

    
    

    
    

测试类:

public class MybatisTest {
    private InputStream in;
    private SqlSession sqlSession;
    private StudentDao studentDao;

    //用于测试方法测试之前执行
    @Before
    public void init() throws IOException {
        //1.读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.使用工厂生产SqlSession对象
        sqlSession = factory.openSession();
        ///4.使用SqlSession创建Dao接口的代理对象
        studentDao = sqlSession.getMapper(StudentDao.class);
    }

    //用于测试方法之后执行
    @After
    public void destroy() throws IOException {
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }
    
    @Test
    public void findAll() {
        //执行查询所有对象方法
        List students = studentDao.findAll();
        for (Student student : students) {
            System.out.println(student);
        }
    }

    @Test
    public void testSave(){
        Student student = new Student();
        student.setName("mybatis");
        student.setSex("女");
        student.setBirthday(new Date());

        //5.执行保存方法
        studentDao.saveStudent(student);
    }

    @Test
    public void testUpdate(){
        Student student = new Student();
        student.setName("coco");
        student.setId(25);
        student.setSex("女");
        student.setBirthday(new Date());

        //执行保存方法
        studentDao.updateStudent(student);
    }

    @Test
    public void testDelete(){
        //执行删除方法
        studentDao.deleteStudent(22);
    }

    @Test
    public void testSelectOne(){
        //执行查询一个方法
        Student student = studentDao.findById(25);
        System.out.println(student);
    }

    @Test
    public void testfindByName(){
        //执行根据名字模糊查询方法
        List students = studentDao.findByName("%王%");
        for (Student student : students) {
            System.out.println(student);
        }
    }

    @Test
    public void testfindTotal(){
        //执行函数统计查询方法
        int total = studentDao.findTotal();
        System.out.println(total);
    }
}

StudentDao:

* 持久层接口
 */
public interface StudentDao {
    /**
     * 查找所有操作
     * @return
     */
    List findAll();

    /**
     * 保存学生
     */
    void saveStudent(Student student);

    /**
     * 更新操作
     * @param student
     */
    void updateStudent(Student student);

    /**
     * 根据id删除学生
     * @param id
     */
    void deleteStudent(Integer id);

    /**
     * 根据id查询学生
     * @param id
     */
    Student findById (Integer id);

    /**
     * 根据名称进行模糊查询
     * @return
     * @param s
     */
    List findByName(String s);

    /**
     * 查询总学生数
     * @return
     */
    int findTotal();
}

二、注解方式:

注意:注解方式与配置文件方式除了需要修改的部分有代码展示,其他与上面配置一致;且配置文件代码与注解方式代码不能同             时存在,所以需要把studentDao.xml删除。

StudentDao修改:

 * 持久层接口
 */
public interface StudentDao {
    /**
     * 查找所有操作
     * @return
     */
    @Select("select * from student")
    List findAll();

    /**
     * 保存学生
     */
    @Insert("insert into student(name,sex,birthday) values(#{name},#{sex},#{birthday})")
    void saveStudent(Student student);

    /**
     * 更新操作
     * @param student
     */
    @Update("update student set name = #{name},sex = #{sex},birthday=#{birthday} where id=#{id}")
    void updateStudent(Student student);

    /**
     * 根据id删除学生
     * @param id
     */
    @Delete("delete from student where id = #{sid}")
    void deleteStudent(Integer id);

    /**
     * 根据id查询学生
     * @param id
     */
    @Select("select * from student where id = #{sid};")
    Student findById (Integer id);

    /**
     * 根据名称进行模糊查询
     * @return
     * @param s
     */
    @Select("select * from student where name like #{name}")
    List findByName(String s);

    /**
     * 查询总学生数
     * @return
     */
    @Select("select count(*) from student")
    int findTotal();
}

SqlMapConfig.xml(改为注解模式):

    


    
    
        
    

对于细节配置不清楚的可以查看我的入门总结:

https://blog.csdn.net/chlei_/article/details/100187907

你可能感兴趣的:(Mybatis)