(Ii) Mybatis type converters, interface type parameter passing,

Mybatis type converter

First, understand when to use it, when the field type and java database field types can not match the default time conversion, such as the type of database is now INTEGER, and java which type is Boolean, true representation 1, false is 0, and this time you when executed sql statement to insert or query to get the result set type there will be a mismatch, this time we only need to write a type converter, and configures it java encounter INTEGER --- Boolean two types of time, it will help us to automatically convert, you insert the equivalent of the value of the database transfer is true, he will be converted into 1 and then insert the results returned by the database 1, it will be converted to true and then return to you. details as follows:

BooleanAndIntConvert.java

Convert Package; 

Import org.apache.ibatis.type.BaseTypeHandler; 
Import org.apache.ibatis.type.JdbcType; 

Import java.sql.CallableStatement Sets; 
Import java.sql.PreparedStatement; 
Import the java.sql.ResultSet; 
Import the java.sql .SQLException; 

// type converter 
public  class BooleanAndIntConvert the extends BaseTypeHandler <Boolean> {
     public   void setNonNullParameter (the PreparedStatement var1, int var2, Boolean var3, jdbcType var4) throws SQLException {
         IF (var3) {// turn into Boolean int, true then 1 inserted into the database, otherwise inserting 0 
            var1.setInt (var2, 1 ); 
        } the else{ 
            Var1.setInt (var2, 0 ); 
        } 
    } 
    // get the value of the column name in accordance with 
    public   Boolean getNullableResult (the ResultSet var1, var2 String) throws SQLException {
         int sexNum = var1.getInt (var2);
         IF (sexNum == . 1 ) / / 1 acquired from the database which is, returns true, otherwise it is to false
             return  to true ;
         return  to false ; 
    } 
    // the index value obtaining 
    public Boolean getNullableResult (the ResultSet var1, int var2) throws SQLException {
         int sexNum = var1.getInt (var2);
        IF (sexNum == . 1 )
             return  to true ;
         return  to false ; 
    } 
    // acquired by the stored procedure 
    public Boolean getNullableResult (CallableStatement var1, int var2) throws SQLException {
         int sexNum = var1.getInt (var2);
         IF (sexNum == . 1 )
             return  to true ;
         return  to false ; 
    } 
}

Specific logic is quite easy to understand, get, set method is invoked when the type mismatch, write their own code to match

After being re-config.xml file to configure, javaType which represents the type of java, jdbcType represented among the jdbc, note the type name not wrong, jdbcType enumerated types which are used to check the time

<typeHandlers>
<!--        类型转换器的配置-->
        <typeHandler handler="convert.BooleanAndIntConvert" javaType="Boolean" jdbcType="INTEGER"/>
    </typeHandlers>

至此,就完成了,然后书写代码查询名字为  哈哈  的person,

List<Person> persons = personMapping.selectPersonByName("哈哈哈");

输出如下,可以看到虽然数据库存的是0,但是我们获取到的却是false,这就类型转换器帮我们自动转换了。

"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=56514:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\learn\java\mybatis\out\production\mybatis;C:\迅雷下载\mybatis-3.5.2\mybatis-3.5.2\mybatis-3.5.2.jar;C:\迅雷下载\mysql-connector-java-8.0.16.jar entity.test
id:6
姓名:哈哈哈
爱好:跳舞
性别:false
id:7
姓名:哈哈哈
爱好:跳舞
性别:false

Process finished with exit code 0

接口方法传参类型:

首先区分两种传参的方法:

#{name} :如果传入字符串,会自动加上单引号;可防止sql注入

${name}:传入字符串不加单引号,多用于排序查询,或者一些需要拼接的参数,

在书写mapper文件之中的sql语句时,传参可以传入一个变量,一个对象,一个HashMap对象,前两种没什么说的,看下第三种

mapper文件当中,看到paramType="HashMap",之后sql语句里面直接写变量名就行

<!--    map传值-->
    <select id="selectPersonByNameAndSex" parameterType="HashMap" resultType="person">
        select * from person where `name` = #{name} or `sex` =#{sex}
    </select>

接口文件之中方法声明:

List<Person> selectPersonByNameAndSex(Map<String,Object> map);

具体使用时候,初始一个map对象,然后调用映射方法并传入,注意变量名要一致: 

Map<String, Object> personMap = new HashMap<>();
personMap.put("name","温鸿飞");   //设置变量的值和名字
personMap.put("sex", "false");
List<Person> persons = personMapping.selectPersonByNameAndSex(personMap);

Guess you like

Origin www.cnblogs.com/eenio/p/11317687.html