首页 文章 精选 留言 我的

精选列表

搜索[管理后台],共10000篇文章
优秀的个人博客,低调大师

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

探索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的方法时传入的变量即使有错在编译阶段也不报错,这不利于程序员开发的。 最后还是附上项目结构图,如下所示:

优秀的个人博客,低调大师

FastAdmin 极速后台框架六周岁啦

FastAdmin 致力于服务开发者,努力为开发者节省更多的时间,让大家有时间读书、健身、开源、投资、帮朋友、陪家人。 FastAdmin 开源社区创立于 2017 年,今天 FastAdmin 六岁啦,开源六年来我们收获了非常多的惊喜,在 FastAdmin 开源过程中,我们结识了很多很有趣的开发者朋友,现已有 87 位开发者朋友为 FastAdmin 贡献了代码,有 128 位插件作者参与了 FastAdmin 插件市场的建设,有无数热心的朋友为 FastAdmin 出谋划策。 FastAdmin 是无数开发者朋友们努力的成果,FastAdmin 的成长离不开大家的支持,框架凝聚了无数开发者朋友们的智慧,朋友们自发的参与到开源社区中,参与到生态的建设中。 FastAdmin 会更加努力做好开源,为国内的开源社区贡献一份自己的力量,也欢迎大家参与开源社区,感谢支持 FastAdmin,感谢支持国内的开源社区。 FastAdmin 六周年活动页https://www.fastadmin.net/act/sixthanniversary.html FastAdmin 官网https://www.fastadmin.net/

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册