zoukankan      html  css  js  c++  java
  • 课时3:属性文件丶全局参数丶别名丶类型转换器丶resultMap

    .1)如何配置连接数据库的参数(主配置文件中)

      1.首先创建一个properties的文件 把连接的参数写入

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/ssm
    username=root
    password=root

      2.在主配置文件中引入该配置文件

    <!--    引入-->
        <properties resource="mysql.properties"></properties>

      3.通过跟el表达式的形式填入value值

              <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>

    .2)如何设置全局参数

    1.下面有一张全局参数的键和值

    2.在主配置文件中配置

     <settings>
            <setting name="cacheEnabled" value="false"/>
        </settings>

         name代表要执行查找键 value 代表键的值

    .3)如何配置别名

        1.配置单个别名 (在主配置文件中)

    <typeAliases>
            <typeAlias type="org.hbz.entity.Student" alias="student"></typeAlias>
        </typeAliases>

        2.配置多个别名(在主配置中)

        <typeAliases> 
    <package name="org.hbz.entity"/>
        </typeAliases>

      如果设置了别名 只要要用到这个全限定类名了 都可以使用别名 并且不区分大小写

          name:代表全限定包名 而别名就是类本身

    3.除了自定义别名mybatis内置的一些别名如下

    .4)类型处理器(类型转换器)

      1.mybatis自带的一些类型处理器

      

      2.自定义mybatis类型处理器

        java-数据库(jdbc类型)

    实例:

    实体类Student boolean stuSex true:男 false :女

    数据库 student int stuSex 1:男 0::女

    自定义类型转换器(boolean-int) 步骤:

      (1) 创建转换器 

        需要实现TypeHandler接口

        通过阅读源码发现BaseTypeHandler是实现类 因此实现类型转换器有两种选择

        i:实现TypeHandler

        ii:继承BaseTypeHandler

    package org.hbz.converter;
    
    
    import org.apache.ibatis.type.BaseTypeHandler;
    import org.apache.ibatis.type.JdbcType;
    
    import java.sql.CallableStatement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class BooleanAndInt extends BaseTypeHandler<Boolean> {
    
        //set:java-->数据库
    
        /**
         *
         * @param preparedStatement PreparedStatement对象
         * @param i PreparedStatement对象操作参数的位置
         * @param o java值
         * @param jdbcType jdbc操作数据库的值
         * @throws SQLException
         */
        @Override
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, Boolean o, JdbcType jdbcType) throws SQLException {
                    if (o){//true编程1
                        preparedStatement.setInt(i,1);
                    }else{//false就是0
                        preparedStatement.setInt(i,0);
                    }
        }
        //get:数据库-->java
    
        /**
         *
         * @param resultSet 数据集
         * @param s 数据库列名
         * @return 返回转换后的值
         * @throws SQLException
         */
        @Override
        public Boolean getNullableResult(ResultSet resultSet, String s) throws SQLException {
            int bool=resultSet.getInt(s);
            return (bool==1?true:false);
        }
        //get:数据库-->java
    
        /**
         *
         * @param resultSet 数据集
         * @param i 下标
         * @return 转换后的数据
         * @throws SQLException
         */
        @Override
        public Boolean getNullableResult(ResultSet resultSet, int i) throws SQLException {
            int bool=resultSet.getInt(i);
            return (bool==1?true:false);
        }
        //get:数据库-->java
    
        /**
         *
         * @param callableStatement 存储过程对象
         * @param i 下标
         * @return 转换后的值
         * @throws SQLException
         */
        @Override
        public Boolean getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            int bool=callableStatement.getInt(i);
            return (bool==1?true:false);
        }
    }

       (2)配置转换器(主配置文件中)

     <typeHandlers>
     <typeHandler handler="org.hbz.converter.BooleanAndInt" javaType="boolean" jdbcType="Integer"></typeHandler>
        </typeHandlers>

      handler:代表映射转换类型的类 javaType:java的类型 jdbcType:数据库的类型

      (3)配置sql映射文件 之查询

     <select id="selectStudentConverter" parameterType="Integer" resultMap="selectConverter">
            select * from student where stuno=#{stuno}
        </select>

        1.resultMap和resultType的区别

          情况1:如果类中的属性类型和表中的字段类型能够合理的被识别(String-varchar) ,则使用resultType,否则使用resultMap

          情况2:如果类中的属名称和表中的字段名称能够合理的被识别(stuno-stuno) ,则使用resultType,否则使用resultMap

        2.配置resultMap(在sql映射文件中配置)

     <resultMap id="selectConverter" type="student">
    <!--        分为主键和非主键-->
            <id property="stuNo" column="stuno"></id>
            <result property="stuName" column="stuname"></result>
            <result property="stuAge" column="stuage"></result>
            <result property="graName" column="graname"></result>
            <result property="stuSex" column="stusex" javaType="boolean" jdbcType="INTEGER"></result>
        </resultMap>

          第一个id:代表通过这个id值可以找到这个map

          第二个id:代表主键

          result:代表非主键

          property:代表java中的属性名

          column:代表数据库的字段名

          javaType:java属性的类型

          jdbcType:数据库字段的类型 并且只能是大写

        3.测试查询操作

     

      (4)配置sql映射文件 之增改

    <!--    含有类型转换的操作-->
        <insert id="addStudenttConverte" parameterType="org.hbz.entity.Student">
            insert into student(stuName,stuAge,graName,stuSex) values(#{stuName},#{stuAge},#{graName},#{stuSex ,javaType=boolean,jdbcType=INTEGER })
        </insert>

        1.在需要转换的地方 #{实体的属性名称,javaType=实体的属性类型,jdbcType数据库的字段的类型}

        2.测试结果:

        3.#{xx}中存放的属性值 严格区分大小写

     

  • 相关阅读:
    洛谷P2089 烤鸡
    HDU-1000 A+B Problem
    《新标准C++程序设计》4.7-4.9(C++学习笔记17)
    《新标准C++程序设计》4.6(C++学习笔记16)
    面向对象程序设计寒假作业3
    《新标准C++程序设计》4.5(C++学习笔记15)
    《新标准C++程序设计》4.4(C++学习笔记14)
    《新标准C++程序设计》4.2-4.3(C++学习笔记13)
    洛谷题解P1047 校门外的树
    [lr] 矫正白平衡
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12439173.html
Copyright © 2011-2022 走看看