后台(36)——MyBatis的原始Dao开发方式

    xiaoxiao2021-03-25  44

    探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制


    Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南


    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理


    版权声明

    本文原创作者:谷哥的小弟作者博客地址:http://blog.csdn.net/lfdfhl

    使用Mybatis开发Dao,通常有两个方法:原始Dao开发方式和Mapper接口开发方式。 在本篇文章中,我们在上一篇博客的基础上来一起完成原始Dao开发方式。

    StudentDto

    /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com.dao; import java.io.IOException; import java.util.List; import cn.com.bean.Student; public interface StudentDto { public Student findStudentById(int id) throws IOException; public List<Student> findStudentByName(String name) throws IOException; public void insertStudent(Student student) throws IOException; public void deleteStudent(int id) throws IOException; public void updateStudent(Student student) throws IOException; }

    在此定义了StudentDto,接下来再看它的实现。

    StudentDtoImpl

    /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com.daoimpl; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import cn.com.bean.Student; import cn.com.dao.StudentDto; public class StudentDtoImpl implements StudentDto{ private SqlSessionFactory sqlSessionFactory; public StudentDtoImpl(SqlSessionFactory sqlSessionFactory){ this.sqlSessionFactory=sqlSessionFactory; } @Override public Student findStudentById(int id) throws IOException { SqlSession sqlSession=sqlSessionFactory.openSession(); Student student = sqlSession.selectOne("student.findStudentById", id); sqlSession.close(); return student; } @Override public List<Student> findStudentByName(String name) throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); List<Student> list = sqlSession.selectList("student.findStudentByName", name); sqlSession.close(); return list; } @Override public void insertStudent(Student student) throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.insert("student.insertStudent",student); sqlSession.commit(); sqlSession.close(); } @Override public void deleteStudent(int id) throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.delete("student.deleteStudent", id); sqlSession.commit(); sqlSession.close(); } @Override public void updateStudent(Student student) throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.update("student.updateStudent", student); sqlSession.commit(); sqlSession.close(); } }

    请务必注意:SqlSession是线程不安全的,所以最好是在方法体内把SqlSession当做局部变量使用。

    TestCRUD

    /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com.test; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import cn.com.bean.Student; import cn.com.dao.StudentDto; import cn.com.daoimpl.StudentDtoImpl; public class TestCRUD { private SqlSessionFactory sqlSessionFactory; @Before public void intiSqlSessionFactory() throws Exception { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void findStudentById() throws IOException{ StudentDto studentDto=new StudentDtoImpl(sqlSessionFactory); Student student = studentDto.findStudentById(2); System.out.println(student); } @Test public void findStudentByName() throws IOException { StudentDto studentDto = new StudentDtoImpl(sqlSessionFactory); List<Student> students = studentDto.findStudentByName("结"); for(Student student:students){ System.out.println(student); } } @Test public void deleteStudent() throws IOException{ StudentDto studentDto=new StudentDtoImpl(sqlSessionFactory); studentDto.deleteStudent(3); } @Test public void updateStudent() throws IOException{ StudentDto studentDto=new StudentDtoImpl(sqlSessionFactory); Student student=new Student(); student.setId(4); student.setName("小小木希"); student.setGender("female"); student.setBirthday(new Date()); studentDto.updateStudent(student); } }

    除了以上的代码以外,SqlMapConfig.xml和StudentMapper.xml无需修改。 至此,我们在MyBatis中使用传统的DAO的方式就已经全部展示完了。

    原始Dao开发方式的问题总结

    DAT接口实现类的方法中存在大量冗余的代码调用sqlsession对象的方法时存在硬编码由于sqlsession对象的方法的输入参数使用泛型,故在调用sqlsession的方法时传入的变量即使有错在编译阶段也不报错,这不利于程序员开发的。

    最后还是附上项目结构图,如下所示:

    转载请注明原文地址: https://ju.6miu.com/read-36536.html

    最新回复(0)