zoukankan      html  css  js  c++  java
  • 15mybatis核心配置文件2

    在前面的核心配置文件中,只讲解了部分标签,现在讲解剩余部分标签。

    typeHandler

    类型处理器:将数据库中的数据类型转换成Java类型。Mybatis本身提供了一些的默认类型转换器。

    自定义类型转换器:

    1. 定义转换类,并继承BaseTypeHandler<T>
    2. 重写4个方法
      • setNonNullParameter方法:将java数据类型转成数据库数据类型
      • getNullableResult方法:将数据库数据类型转成java数据类型
    3. 在MyBatis核心配置文件中注册

    代码:实现数据库的varchar毫秒值与Java中的Date转换

    1. 在handler包中定义DateTypeHandler,并继承BaseTypeHandler<Date>

    2. 重写4个方法

      public class DateTypeHandler extends BaseTypeHandler<Date> {
          @Override
          public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
              long time = parameter.getTime();
              ps.setLong(i, time);
          }
      
          @Override
          public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
              long aLong = rs.getLong(columnName);
              Date date = new Date(aLong);
              return date;
          }
      
          @Override
          public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
              long aLong = rs.getLong(columnIndex);
              Date date = new Date(aLong);
              return date;
          }
      
          @Override
          public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
              long aLong = cs.getLong(columnIndex);
              Date date = new Date(aLong);
              return date;
          }
      }
      
    3. 在核心配置文件注册类型处理器

      <!--类型处理器-->
      <typeHandlers>
          <typeHandler handler="com.handler.DateTypeHandler"/>
      </typeHandlers>
      

    plugins

    mybatis可以使用第三方的插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据

    开发步骤:

    1. 导入第三方坐标
    2. 在mybatis核心配置文件中添加插件
    3. 测试

    代码:

    1. 分页坐标(需要两个)

      <!--        分页插件-->
              <dependency>
                  <groupId>com.github.pagehelper</groupId>
                  <artifactId>pagehelper</artifactId>
                  <version>5.1.2</version>
              </dependency>
              <dependency>
                  <groupId>com.github.jsqlparser</groupId>
                  <artifactId>jsqlparser</artifactId>
                  <version>1.0</version>
              </dependency>
      
    2. 在核心配置文件添加插件(教程是这样写,但是出错;如果不写这些配置文件也可以!!)

      <!--    分页插件-->
          <plugins>
              <plugin interceptor="com.github.pagehelper.PageHelper">
                  <!--方言:指定数据库-->
                  <property name="dialect" value="mysql"/>
              </plugin>
          </plugins>
      
    3. 测试

      //  获取核心配置文件
      InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 获取session工厂对象
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
      // 获得session会话对象
      SqlSession sqlSession = sqlSessionFactory.openSession();
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      // 执行操作(参数就是配置文件中的id)
      
      // 分页设置
      PageHelper.startPage(1, 2);
      List<User> userList = mapper.findAll();
      for (User user: userList) {
          System.out.println(user);
      }
      // 分页信息
      PageInfo<User> userPageInfo = new PageInfo<>(userList);
      System.out.println(userPageInfo.getPageNum());
      System.out.println(userPageInfo.getPageSize());
      // 释放资源
      sqlSession.close();
      
  • 相关阅读:
    PowerDesigner_15连接Oracle11g,反向工程导出模型图
    angular学习
    GoEasy消息推送
    Spring 工作原理
    JAVA解析HTML,获取待定元素属性
    设计模式之工厂方法模式
    设计模式之单例模式
    通过Java代码获取系统信息
    centos7下NAT模式下设置静态ip
    关于在Spring项目中使用thymeleaf报Exception parsing document错误
  • 原文地址:https://www.cnblogs.com/mingriyingying/p/13639612.html
Copyright © 2011-2022 走看看