首页 文章 精选 留言 我的

精选列表

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

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

云数据库POLARDB优势解读系列文章之⑤——会话读一致性

作者 武藏等 POLARDB架构 我们知道,POLARDB是一个由多个节点构成的数据库集群,一个主节点,多个读节点。对外默认提供两个地址,一个是集群地址,一个是主地址,推荐使用集群地址,因为它具备读写分离功能可以把所有节点的资源整合到一起对外提供服务。 MySQL读写分离解决和引入的问题 用过MySQL的都知道,MySQL的主从复制简单易用,非常流行,通过把主库的Binlog异步地传输到备库并实时应用,一方面可以实现高可用,另一方面备库也可以提供查询,来减轻对主库的压力。 虽然备库可以提供查询,但存在两个问题,一是主库和备库一般提供两个不同的访问地址,应用程序端需要选择使用哪一个,对应用有侵入。二来MySQL的复制是异步的,即使是半同步也没办法做到100%强同步,因此备库的数据并不是最新的,有延迟,无法保证查询的一致性。 为了解决第一个问题,

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

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

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

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

用户登录
用户注册