MyBatis details

1. Typealias (alias)

It refers to replacing the full name of a class with a short name in the configuration file. Aliases are divided into system aliases and custom aliases, regardless of case

First add the configuration in config.xml

        <typeAliases>
		<typeAlias type="com.kgc.mybatis.pojo.Person" alias="Person"></typeAlias>
	</typeAliases>

After configuration, the address in the parameterType attribute in mapper.xml can be replaced by the shorthand Person.

2,typeHandler

Can complete the java data type and database field type mapping conversion work.

>Step 1: rewrite the typeHandler method. According to my wishes, I write the fixed upper case to the database, and read it and convert it to lower case.

package com.kgc.mybatis.typeHandlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;


public class MyTypeHandler implements TypeHandler<String> {

	public String getResult(ResultSet arg0, String arg1) throws SQLException {
		return arg0.getString(arg1).toLowerCase();
	}

	public String getResult(ResultSet arg0, int arg1) throws SQLException {
		return arg0.getString(arg1).toLowerCase();
	}

	public String getResult(CallableStatement arg0, int arg1)
			throws SQLException {
		return arg0.getString(arg1).toLowerCase();
	}

	public void setParameter(PreparedStatement arg0, int arg1, String arg2,
			JdbcType arg3) throws SQLException {
		arg0.setString(arg1, arg2.toUpperCase());
	}

}

>Step 2: add configuration in config.xml as well

        <typeHandlers>
		<typeHandler handler="com.kgc.mybatis.typeHandlers.MyTypeHandler" javaType="String" jdbcType="VARCHAR"/>
	</typeHandlers>

>Step 3: call the rewritten method through JDBC type = "varchar" after configuration

Here are two examples of write and read

Write to database:

	<insert id="addPerson1" parameterType="Person" useGeneratedKeys="true" keyProperty="pid">
		insert into t_person values(null,#{pname,jdbcType=VARCHAR},#{age},#{address,jdbcType=VARCHAR})
	</insert>

Read from database:

	<resultMap type="Person" id="Person3" >
		<id property="pid" />
		<result property="pname" column="person_name" jdbcType="VARCHAR"/>
		<result property="age"/>
		<result property="address" column="person_address" jdbcType="VARCHAR"/>
	</resultMap>

3.resultMap

By default, mapper uses resultType to return data type, and resultMap defines mapping relationship manually

 

	<resultMap type="Person" id="Person3" >
		<id property="pid" />
		<result property="pname" column="person_name" jdbcType="VARCHAR"/>
		<result property="age"/>
		<result property="address" column="person_address" jdbcType="VARCHAR"/>
	</resultMap>

type="Person" id="Person3" type="Person" call the original return type, id="Person3" define the information return data type

< result property="pname" column="person_name" JDBC type = "varchar" / > define the mapping relationship, property="pname" represents the property name in Person, and column="person_name" represents the field name in the database table.

 

Tags: Java Database SQL xml

Posted on Fri, 10 Jan 2020 12:01:00 -0500 by avvishnu