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

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

探索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–滑动冲突的产生及其处理


版权声明


使用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的方法时传入的变量即使有错在编译阶段也不报错,这不利于程序员开发的。

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

这里写图片描述

相关文章
|
9月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
325 2
|
10月前
|
SQL Java 数据库连接
MyBatis-Plus高级用法:最优化持久层开发
MyBatis-Plus 通过简化常见的持久层开发任务,提高了开发效率和代码的可维护性。通过合理使用条件构造器、分页插件、逻辑删除和代码生成器等高级功能,可以进一步优化持久层开发,提升系统性能和稳定性。掌握这些高级用法和最佳实践,有助于开发者构建高效、稳定和可扩展的企业级应用。
572 13
|
12月前
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
325 10
|
12月前
|
前端开发 Java 数据库连接
表白墙/留言墙 —— 中级SpringBoot项目,MyBatis技术栈MySQL数据库开发,练手项目前后端开发(带完整源码) 全方位全步骤手把手教学
本文是一份全面的表白墙/留言墙项目教程,使用SpringBoot + MyBatis技术栈和MySQL数据库开发,涵盖了项目前后端开发、数据库配置、代码实现和运行的详细步骤。
265 0
表白墙/留言墙 —— 中级SpringBoot项目,MyBatis技术栈MySQL数据库开发,练手项目前后端开发(带完整源码) 全方位全步骤手把手教学
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
SQL Java 数据库连接
Spring Boot联手MyBatis,打造开发利器:从入门到精通,实战教程带你飞越编程高峰!
【8月更文挑战第29天】Spring Boot与MyBatis分别是Java快速开发和持久层框架的优秀代表。本文通过整合Spring Boot与MyBatis,展示了如何在项目中添加相关依赖、配置数据源及MyBatis,并通过实战示例介绍了实体类、Mapper接口及Controller的创建过程。通过本文,你将学会如何利用这两款工具提高开发效率,实现数据的增删查改等复杂操作,为实际项目开发提供有力支持。
1096 0
|
Java 数据库连接 mybatis
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
|
SQL Java 数据库连接
后端框架的学习----mybatis框架(7、使用注解开发)
这篇文章讲述了如何使用MyBatis框架的注解方式进行开发,包括在接口上使用注解定义SQL语句,并通过动态代理实现对数据库的增删改查操作,同时强调了接口需要在核心配置文件中注册绑定。
|
4月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
639 1
Spring boot 使用mybatis generator 自动生成代码插件
|
7月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
552 0