MyBatis-03-笔记

本文介绍了MyBatis传统方式实现DAO层的详细步骤,包括创建接口、实现类以及各个CRUD操作的具体代码。同时,讲解了如何配置和使用LOG4J来输出MyBatis执行的SQL信息,以便于开发中进行问题排查。
摘要由CSDN通过智能技术生成

MyBatis传统方式实现Dao层

1 Dao 层传统实现方式

  • 分层思想:控制层(controller)、业务层(service)、持久层(dao)。

  • 调用流程
    在这里插入图片描述

  • 持久层具体实现代码

    • 接口StudentMapper
      package com.itheima.mapper;
      /*
          持久层接口
       */
      public interface StudentMapper {
          //查询全部
          public abstract List<Student> selectAll();
      
          //根据id查询
          public abstract Student selectById(Integer id);
      
          //新增数据
          public abstract Integer insert(Student stu);
      
          //修改数据
          public abstract Integer update(Student stu);
      
          //删除数据
          public abstract Integer delete(Integer id);
      }
      
    • 实现类StudentMapperImp
      package com.itheima.mapper.impl;
      
      /*
          持久层实现类
       */
      public class StudentMapperImpl implements StudentMapper {
      
          /*
              查询全部
           */
          @Override
          public List<Student> selectAll() {
              List<Student> list = null;
              SqlSession sqlSession = null;
              InputStream is = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.执行映射配置文件中的sql语句,并接收结果
                  list = sqlSession.selectList("StudentMapper.selectAll");
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //5.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      
              //6.返回结果
              return list;
          }
      
          /*
              根据id查询
           */
          @Override
          public Student selectById(Integer id) {
              Student stu = null;
              SqlSession sqlSession = null;
              InputStream is = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.执行映射配置文件中的sql语句,并接收结果
                  stu = sqlSession.selectOne("StudentMapper.selectById",id);
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //5.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      
              //6.返回结果
              return stu;
          }
      
          /*
              新增功能
           */
          @Override
          public Integer insert(Student stu) {
              Integer result = null;
              SqlSession sqlSession = null;
              InputStream is = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.执行映射配置文件中的sql语句,并接收结果
                  result = sqlSession.insert("StudentMapper.insert",stu);
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //5.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      
              //6.返回结果
              return result;
          }
      
          /*
              修改功能
           */
          @Override
          public Integer update(Student stu) {
              Integer result = null;
              SqlSession sqlSession = null;
              InputStream is = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.执行映射配置文件中的sql语句,并接收结果
                  result = sqlSession.update("StudentMapper.update",stu);
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //5.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      
              //6.返回结果
              return result;
          }
      
          /*
              删除功能
           */
          @Override
          public Integer delete(Integer id) {
              Integer result = null;
              SqlSession sqlSession = null;
              InputStream is = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.执行映射配置文件中的sql语句,并接收结果
                  result = sqlSession.delete("StudentMapper.delete",id);
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //5.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      
              //6.返回结果
              return result;
          }
      }
      

2 LOG4J的配置和使用

  • 在日常开发过程中,排查问题时难免需要输出 MyBatis 真正执行的 SQL 语句、参数、结果等信息,我们就可以借助 LOG4J 的功能来实现执行信息的输出。

  • 使用步骤:
    在这里插入图片描述

  • 演示
    • log4j.properties
      # Global logging configuration
      # ERROR WARN INFO DEBUG
      log4j.rootLogger=DEBUG, stdout
      # Console output...
      log4j.appender.stdout=org.apache.log4j.ConsoleAppender
      log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
      log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
      
    • 核心配置文件MyBatisConfig.xml
      <?xml version="1.0" encoding="UTF-8" ?>
      <!--MyBatis的DTD约束-->
      <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
      
      <!--configuration 核心根标签-->
      <configuration>
      
          <!--引入数据库连接的配置文件-->
          <properties resource="jdbc.properties"/>
      
          <!--配置LOG4J-->
          <settings>
              <setting name="logImpl" value="log4j"/>
          </settings>
      
          <!--起别名-->
          <typeAliases>
              <typeAlias type="com.itheima.bean.Student" alias="student"/>
              <!--<package name="com.itheima.bean"/>-->
          </typeAliases>
      
          <!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
          <environments default="mysql">
              <!--environment配置数据库环境  id属性唯一标识-->
              <environment id="mysql">
                  <!-- transactionManager事务管理。  type属性,采用JDBC默认的事务-->
                  <transactionManager type="JDBC"></transactionManager>
                  <!-- dataSource数据源信息   type属性 连接池-->
                  <dataSource type="POOLED">
                      <!-- property获取数据库连接的配置信息 -->
                      <property name="driver" value="${driver}" />
                      <property name="url" value="${url}" />
                      <property name="username" value="${username}" />
                      <property name="password" value="${password}" />
                  </dataSource>
              </environment>
          </environments>
      
          <!-- mappers引入映射配置文件 -->
          <mappers>
              <!-- mapper 引入指定的映射配置文件   resource属性指定映射配置文件的名称 -->
              <mapper resource="StudentMapper.xml"/>
          </mappers>
      </configuration>
      
    • 配置后当调用实现类StudentMapperImp中的方法时,在控制台能偶看到入参及结果的信息的打印。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值